Commit a347fbe5 authored by Anthony Gautreau's avatar Anthony Gautreau
Browse files

Import du fonctionnement utilisateur depuis Smile+

parent 5d61833f
Loading
Loading
Loading
Loading
Loading
+23 −0
Changes for api/src/Dto/UtilisateurInscriptionInput.php: 23 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Unicaen\Utilisateur\Dto;

use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

class UtilisateurInscriptionInput
{
    #[Assert\NotBlank]
    #[Assert\Email]
    #[Groups(['utilisateur:register'])]
    public string $email;

    #[Assert\NotBlank]
    #[Assert\Length(min: 4)]
    #[Groups(['utilisateur:register'])]
    public string $plainPassword;

    #[Assert\NotBlank]
    #[Groups(['utilisateur:register'])]
    public string $typeConnexion;
}
+20 −0
Changes for api/src/Dto/UtilisateurMeResource.php: 20 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Unicaen\Utilisateur\Dto;

use Symfony\Component\Serializer\Attribute\Groups;

class UtilisateurMeResource
{
    #[Groups(['user:me'])]
    public int $id;

    #[Groups(['user:me'])]
    public string $email;

    #[Groups(['user:me'])]
    public array $roles;

    #[Groups(['user:me'])]
    public array $privileges = [];
}
+199 −0
Changes for api/src/Entity/Utilisateur.php: 199 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Unicaen\Utilisateur\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Unicaen\Privilege\Entity\Role;
use Unicaen\Utilisateur\Dto\UtilisateurInscriptionInput;
use Unicaen\Utilisateur\Dto\UtilisateurMeResource;
use Unicaen\Utilisateur\Model\UnicaenUtilisateur;
use Unicaen\Utilisateur\Repository\UtilisateurRepository;
use Unicaen\Utilisateur\State\UtilisateurInscriptionProcessor;
use Unicaen\Utilisateur\State\UtilisateurMeStateProvider;
use Unicaen\Utilisateur\State\UtilisateurPasswordHasherProcessor;

#[ApiResource(
    operations: [
        new Get(),
        new Get(
            uriTemplate: '/me',
            normalizationContext: ['groups' => ['user:me']],
            output: UtilisateurMeResource::class,
            name: 'user_me',
            provider: UtilisateurMeStateProvider::class
        ),
        new GetCollection(),
        new Post(
            uriTemplate: '/register',
            normalizationContext: ['groups' => ['user:read']],
            denormalizationContext: ['groups' => ['user:register']],
            input: UtilisateurInscriptionInput::class,
            name: 'user_register',
            processor: UtilisateurInscriptionProcessor::class
        ),
        new Put(processor: UtilisateurPasswordHasherProcessor::class),
        new Patch(processor: UtilisateurPasswordHasherProcessor::class),
        new Delete(),
    ],
    normalizationContext: ['groups' => ['user:read']],
    denormalizationContext: ['groups' => ['user:create', 'user:update']],
)]
#[IsGranted('ROLE_USER')]
#[ORM\Entity(repositoryClass: UtilisateurRepository::class)]
#[ORM\Table(name: '`user`')]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_EMAIL', fields: ['email'])]
class Utilisateur implements UnicaenUtilisateur, PasswordAuthenticatedUserInterface
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[Assert\NotBlank]
    #[Assert\Email]
    #[Groups(['user:read', 'user:create', 'user:update'])]
    #[ORM\Column(length: 180)]
    private ?string $email = null;

    #[Assert\NotBlank(groups: ['user:create'])]
    #[Groups(['user:create', 'user:update'])]
    private ?string $plainPassword = null;

    /**
     * @var string The hashed password
     */
    #[ORM\Column]
    private ?string $password = null;

    #[Groups(['user:create', 'user:update'])]
    #[ORM\Column(length: 64)]
    private ?string $typeConnexion = null;

    /**
     * @var Collection<int, Role>
     */
    #[ORM\ManyToMany(targetEntity: Role::class)]
    private Collection $roles;

    public function __construct()
    {
        $this->roles = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): static
    {
        $this->email = $email;

        return $this;
    }

    /**
     * A visual identifier that represents this user.
     *
     * @see UserInterface
     */
    public function getUserIdentifier(): string
    {
        return (string) $this->email;
    }

    /**
     * @see PasswordAuthenticatedUserInterface
     */
    public function getPassword(): ?string
    {
        return $this->password;
    }

    public function setPassword(string $password): static
    {
        $this->password = $password;

        return $this;
    }

    public function getPlainPassword(): ?string
    {
        return $this->plainPassword;
    }

    public function setPlainPassword(?string $plainPassword): self
    {
        $this->plainPassword = $plainPassword;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function eraseCredentials(): void
    {
        // If you store any temporary, sensitive data on the user, clear it here
        // $this->plainPassword = null;
    }

    public function getTypeConnexion(): ?string
    {
        return $this->typeConnexion;
    }

    public function setTypeConnexion(string $typeConnexion): static
    {
        $this->typeConnexion = $typeConnexion;

        return $this;
    }

    public function getRoles(): array
    {
        $roles = $this->roles->map(fn (Role $role) => $role->getCode())->toArray();

        return array_unique($roles);
    }

    public function getRolesEntities(): Collection
    {
        return $this->roles;
    }

    public function addRole(Role $role): static
    {
        if (!$this->roles->contains($role)) {
            $this->roles->add($role);
        }

        return $this;
    }

    public function removeRole(Role $role): static
    {
        $this->roles->removeElement($role);

        return $this;
    }
}
+1 −2
Changes for api/src/Model/UnicaenUtilisateur.php: 1 added line, 2 removed lines.
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ namespace Unicaen\Utilisateur\Model;

use Symfony\Component\Security\Core\User\UserInterface;

interface UnicaenUser extends UserInterface
interface UnicaenUtilisateur extends UserInterface
{

}
+36 −0
Changes for api/src/Repository/UtilisateurRepository.php: 36 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Unicaen\Utilisateur\Repository;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use Unicaen\Utilisateur\Model\UnicaenUtilisateur;

/**
 * @extends ServiceEntityRepository<UnicaenUtilisateur>
 */
class UtilisateurRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, UnicaenUtilisateur::class);
    }

    /**
     * Used to upgrade (rehash) the user's password automatically over time.
     * Is it useful
     */
    public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
    {
        if (!$user instanceof UnicaenUtilisateur) {
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $user::class));
        }

        $user->setPassword($newHashedPassword);
        $this->getEntityManager()->persist($user);
        $this->getEntityManager()->flush();
    }
}
Loading