Commit ddb0feac authored by Antony Le Courtes's avatar Antony Le Courtes
Browse files

Mise en place de HistoriqueInterface sur le User (creation, modification, destruction etc...)

parent a70b0d70
Loading
Loading
Loading
Loading
Loading
+3 −0
Changes for api/src/Entity/AbstractRole.php: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ namespace Unicaen\Utilisateur\Entity;
use ApiPlatform\Metadata\ApiResource;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Unicaen\Utilisateur\Model\RoleInterface;
use Unicaen\Utilisateur\Model\UserInterface;
use Unicaen\Privilege\Model\PrivilegeInterface;
@@ -21,9 +22,11 @@ abstract class AbstractRole implements RoleInterface
    protected ?int $id = null;

    #[ORM\Column(length: 255, unique: true)]
    #[Groups(['role:read'])]
    protected ?string $code = null;

    #[ORM\Column(length: 255)]
    #[Groups(['role:read'])]
    protected ?string $libelle = null;

    #[ORM\Column(length: 255, nullable: true)]
+2 −1
Changes for api/src/Entity/AbstractUser.php: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ abstract class AbstractUser implements UserInterface, PasswordAuthenticatedUserI
    #[ORM\Column(length: 180, unique: true)]
    protected string $email;

    #[Groups(['user:read', 'user:create', 'user:update', 'histoUser:read'])]
    #[Groups(['user:read', 'user:create', 'user:update'])]
    #[ORM\Column(length: 180, nullable: true)]
    protected ?string $displayName = null;

@@ -76,6 +76,7 @@ abstract class AbstractUser implements UserInterface, PasswordAuthenticatedUserI
    protected ?PerimetreUserRoleInterface $activePerimetreUserRole = null;

    #[ORM\OneToMany(targetEntity: UserRoleInterface::class, orphanRemoval: true, mappedBy: 'user', cascade: ['persist', 'remove'])]
    #[Groups(['user:read'])]
    protected Collection $roles;

    public function __construct()
+22 −10
Changes for api/src/Entity/User.php: 22 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -12,9 +12,11 @@ use ApiPlatform\Metadata\Put;
use ApiPlatform\OpenApi\Model\Operation;
use Unicaen\Utilisateur\Dto\UtilisateurInscriptionInput;
use Unicaen\Utilisateur\Dto\UtilisateurMeResource;
use Unicaen\Utilisateur\Model\RoleInterface;
use Unicaen\Utilisateur\Model\HistoriqueAwareInterface;
use Unicaen\Utilisateur\Model\HistoriqueAwareTrait;
use Unicaen\Utilisateur\Repository\UtilisateurRepository;
use Doctrine\ORM\Mapping as ORM;
use Unicaen\Utilisateur\State\HistoriserProcessor;
use Unicaen\Utilisateur\State\UtilisateurInscriptionProcessor;
use Unicaen\Utilisateur\State\UtilisateurMeStateProvider;
use Unicaen\Utilisateur\State\UtilisateurPasswordHasherProcessor;
@@ -22,7 +24,12 @@ use Unicaen\Utilisateur\State\UtilisateurPasswordHasherProcessor;
#[ApiResource(
    shortName: 'Utilisateur',
    operations: [
        new Get(),
        new Get(
            normalizationContext: [
                'groups' => ['user:read','histo:read','role:read'],
                'skip_null_values' => false,
            ],
        ),
        new Get(
            uriTemplate: '/me',
            normalizationContext: ['groups' => ['user:me']],
@@ -30,7 +37,12 @@ use Unicaen\Utilisateur\State\UtilisateurPasswordHasherProcessor;
            name: 'user_me',
            provider: UtilisateurMeStateProvider::class
        ),
        new GetCollection(),
        new GetCollection(
            normalizationContext: [
                'groups' => ['user:read','histo:read','role:read'],
                'skip_null_values' => false,
            ],
        ),
        new Post(
            uriTemplate: '/register',
            normalizationContext: ['groups' => ['user:read']],
@@ -41,15 +53,13 @@ use Unicaen\Utilisateur\State\UtilisateurPasswordHasherProcessor;
        ),
        new Put(processor: UtilisateurPasswordHasherProcessor::class),
        new Patch(
            denormalizationContext: [
                'groups' => ['user:update'],
            ],
            processor: UtilisateurPasswordHasherProcessor::class
            uriTemplate: '/{id}/historiser',
            processor: HistoriserProcessor::class,
        ),
        new Delete(),
    ],
    normalizationContext: ['groups' => ['user:read']],
    denormalizationContext: ['groups' => ['user:create', 'user:update']],
    normalizationContext: ['groups' => ['user:read', 'histo:read']],
    denormalizationContext: ['groups' => ['user:create', 'user:update', 'histo:read']],
    openapi: new Operation(
        tags: ['Unicaen Utilisateur'],
    ),
@@ -57,7 +67,9 @@ use Unicaen\Utilisateur\State\UtilisateurPasswordHasherProcessor;
)]
#[ORM\Entity(repositoryClass: UtilisateurRepository::class)]
#[ORM\Table(name: '`unicaen_utilisateur_user`')]
class User extends AbstractUser
class User extends AbstractUser implements HistoriqueAwareInterface
{
    use HistoriqueAwareTrait;


}
 No newline at end of file
+2 −0
Changes for api/src/Event/HistoriqueListener.php: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -37,6 +37,8 @@ class HistoriqueListener
        $user = $this->histoUserRepository->getHistoUser();
        $entity->setHistoCreation($now);
        $entity->setHistoCreateur($user);
        $entity->setHistoModification($now);
        $entity->setHistoModificateur($user);
    }

    /**
+68 −3
Changes for api/src/Model/HistoriqueAwareTrait.php: 68 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -5,17 +5,39 @@ namespace Unicaen\Utilisateur\Model;
use DateTime;
use DateTimeInterface;
use Exception;
use Doctrine\ORM\Mapping as ORM;
use RuntimeException;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Serializer\Attribute\SerializedName;

trait HistoriqueAwareTrait
{
    protected ?DateTimeInterface $histoCreation = null;
    protected ?DateTimeInterface $histoModification = null;
    protected ?DateTimeInterface $histoDestruction = null;
    #[ORM\ManyToOne(targetEntity: UserInterface::class)]
    #[ORM\JoinColumn(name: 'histo_createur_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    protected ?UserInterface $histoCreateur = null;

    #[ORM\Column(type: 'datetime', nullable: true)]
    #[Groups(['histo:read'])]
    protected ?DateTimeInterface $histoCreation = null;

    #[ORM\ManyToOne(targetEntity: UserInterface::class)]
    #[ORM\JoinColumn(name: 'histo_modificateur_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    protected ?UserInterface $histoModificateur = null;

    #[ORM\Column(type: 'datetime', nullable: true)]
    #[Groups(['histo:read'])]
    protected ?DateTimeInterface $histoModification = null;

    #[ORM\ManyToOne(targetEntity: UserInterface::class)]
    #[ORM\JoinColumn(name: 'histo_destructeur_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
    protected ?UserInterface $histoDestructeur = null;

    #[ORM\Column(type: 'datetime', nullable: true)]
    #[Groups(['histo:read'])]
    protected ?DateTimeInterface $histoDestruction = null;



    public function setHistoCreation(?DateTimeInterface $histoCreation): HistoriqueAwareInterface
    {
        $this->histoCreation = $histoCreation;
@@ -88,6 +110,49 @@ trait HistoriqueAwareTrait
        return $this->histoCreateur;
    }

    #[Groups(['histo:read'])]
    #[SerializedName('histoCreateur')]
    public function getHistoCreateurResume(): ?array
    {
        if ($this->histoCreateur === null) {
            return null;
        }

        return [
            'email' => $this->histoCreateur->getEmail(),
            'displayName' => $this->histoCreateur->getDisplayName(),
        ];
    }

    #[Groups(['histo:read'])]
    #[SerializedName('histoModificateur')]
    public function getHistoModificateurResume(): ?array
    {
        if ($this->histoModificateur === null) {
            return null;
        }

        return [
            'email' => $this->histoModificateur->getEmail(),
            'displayName' => $this->histoModificateur->getDisplayName(),
        ];
    }


    #[Groups(['histo:read'])]
    #[SerializedName('histoDesctructeur')]
    public function getHistoDestructeurResume(): ?array
    {
        if ($this->histoDestructeur === null) {
            return null;
        }

        return [
            'email' => $this->histoDestructeur->getEmail(),
            'displayName' => $this->histoDestructeur->getDisplayName(),
        ];
    }

    /** Historisation / Restauration **********************************************************************************/
//    TODO : a revoir si l'on garde les fonctions  Historiser et desistoriser : c'est des actions d'api qui vont le faire
    /**
Loading