[#202] Authentification — Connexion utilisateur (JWT) (!5)

| Numéro du ticket | Titre du ticket |
|------------------|-----------------|
|      #202            |        Authentification — Connexion utilisateur (JWT)         |

## Description de la PR
[#202] Authentification — Connexion utilisateur (JWT)

## Modification du .env

## Check list

- [x] Pas de régression
- [ ] TU/TI/TF rédigée
- [x] TU/TI/TF OK
- [x] CHANGELOG modifié

Reviewed-on: https://gitea.malio.fr/MALIO-DEV/Ferme/pulls/5
Reviewed-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr>
Co-authored-by: tristan <tristan@yuno.malio.fr>
Co-committed-by: tristan <tristan@yuno.malio.fr>
This commit is contained in:
tristan
2026-01-20 20:06:29 +00:00
committed by Autin
parent 42fafc5d39
commit 8f5730c3f6
34 changed files with 932 additions and 48 deletions
+1
View File
@@ -63,6 +63,7 @@ use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
provider: ReceptionReceiptProvider::class,
),
],
security: "is_granted('ROLE_USER')",
)]
class Reception
{
+104
View File
@@ -0,0 +1,104 @@
<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use App\State\MeProvider;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Attribute\Groups;
#[ORM\Entity]
#[ORM\Table(name: 'user', schema: 'public')]
#[ApiResource(
operations: [
new Get(
uriTemplate: '/me',
normalizationContext: ['groups' => ['user:read']],
security: "is_granted('ROLE_USER')",
provider: MeProvider::class
),
new GetCollection(
normalizationContext: ['groups' => ['user:read']],
security: "is_granted('PUBLIC_ACCESS')"
),
],
normalizationContext: ['groups' => ['user:read']],
paginationEnabled: false
)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;
#[ORM\Column(length: 180, unique: true)]
#[Groups(['user:read'])]
private string $username = '';
#[ORM\Column(type: 'json')]
private array $roles = [];
#[ORM\Column]
private string $password = '';
public function getId(): ?int
{
return $this->id;
}
public function getUsername(): string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function getUserIdentifier(): string
{
return $this->username;
}
public function getRoles(): array
{
$roles = $this->roles;
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function eraseCredentials(): void
{
// No-op: we don't store temporary sensitive data on the entity.
}
}
+1
View File
@@ -32,6 +32,7 @@ use Symfony\Component\Validator\Constraints as Assert;
denormalizationContext: ['groups' => ['weight:write']],
),
],
security: "is_granted('ROLE_USER')",
)]
#[UniqueEntity(fields: ['reception', 'type'], message: 'A weighing already exists for this type.')]
class Weight
+27
View File
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace App\State;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Entity\User;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
final readonly class MeProvider implements ProviderInterface
{
public function __construct(private Security $security) {}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object
{
$user = $this->security->getUser();
if (!$user instanceof User) {
throw new AccessDeniedException('User not authenticated.');
}
return $user;
}
}