Commit 71ca3079 authored by Anthony Gautreau's avatar Anthony Gautreau
Browse files

Adaptation du front privilege à utilisateur, Mise en place de classes...

Adaptation du front privilege à utilisateur, Mise en place de classes abstraites pour User et Role à la manière des libs unicaen
parent a347fbe5
Loading
Loading
Loading
Loading
Loading
+174 −0
Changes for api/src/Entity/AbstractRole.php: 174 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Unicaen\Utilisateur\Entity;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Unicaen\Utilisateur\Model\UnicaenUserInterface;
use Unicaen\Privilege\Model\PrivilegeInterface;

#[ORM\MappedSuperclass]
abstract class AbstractRole implements RoleInterface
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    protected ?int $id = null;

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

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

    #[ORM\Column(length: 255, nullable: true)]
    protected ?string $description = null;
    protected bool $default = false;
    protected bool $auto = false;
    protected ?RoleInterface $parent = null;
    protected ?string $ldapFilter = null;
    protected bool $accessibleExterieur = true;
    protected bool $displayed = true;

    protected Collection $users;
//    protected Collection $privileges;

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

    public function setId(?int $id) : void
    {
        $this->id = (int)$id;
    }

    public function getCode() : ?string
    {
        return $this->code;
    }

    public function setCode(?string $code) : void
    {
        $this->code = (string)$code;
    }

    public function getLibelle() : ?string
    {
        return $this->libelle;
    }

    public function setLibelle(?string $libelle) : void
    {
        $this->libelle = $libelle;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(?string $description): void
    {
        $this->description = $description;
    }


    public function isDefault() : bool
    {
        return $this->default;
    }

    public function setDefault(bool $default) : void
    {
        $this->default = (boolean) $default;
    }

    public function isAuto() : bool
    {
        return $this->auto;
    }

    public function setAuto(bool $auto) : void
    {
        $this->auto = $auto;
    }

    public function isAccessibleExterieur() : bool
    {
        return $this->accessibleExterieur;
    }

    public function setAccessibleExterieur(bool $accessibleExterieur) : void
    {
        $this->accessibleExterieur = $accessibleExterieur;
    }

    public function isDisplayed(): bool
    {
        return $this->displayed;
    }

    public function setDisplayed(bool $displayed): void
    {
        $this->displayed = $displayed;
    }

    public function getParent() : ?RoleInterface
    {
        return $this->parent;
    }

    public function setParent(?RoleInterface $parent = null) : void
    {
        $this->parent = $parent;
    }

    public function getLdapFilter() : ?string
    {
        return $this->ldapFilter;
    }

    public function setLdapFilter(?string $ldapFilter) : void
    {
        $this->ldapFilter = $ldapFilter;
    }

    public function getUsers() : Collection
    {
        return $this->users;
    }

    public function addUser(UnicaenUserInterface $user) : void
    {
        $this->users->add($user);
    }

    public function removeUser(UnicaenUserInterface $user) : void
    {
        $this->users->removeElement($user);
    }

//    public function getPrivileges() : Collection
//    {
//        return $this->privileges;
//    }

//    public function addPrivilege(PrivilegeInterface $privilege) : void
//    {
//        $this->privileges->add($privilege);
//    }
//
//    public function removePrivilege(PrivilegeInterface $privilege) : void
//    {
//        $this->privileges->removeElement($privilege);
//    }

    /** Fonctions magiques ********************************************************************************************/

    public function __toString() : string
    {
        return $this->getLibelle();
    }

}
 No newline at end of file
+38 −25
Changes for api/src/Entity/AbstractUser.php: 38 added lines, 25 removed lines.
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use ApiPlatform\OpenApi\Model\Operation;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
@@ -17,16 +18,16 @@ 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\Model\UnicaenUserInterface;
use Unicaen\Utilisateur\Repository\UtilisateurRepository;
use Unicaen\Utilisateur\State\UtilisateurInscriptionProcessor;
use Unicaen\Utilisateur\State\UtilisateurMeStateProvider;
use Unicaen\Utilisateur\State\UtilisateurPasswordHasherProcessor;

#[ApiResource(
    shortName: 'user',
    operations: [
        new Get(),
        new Get(
@@ -51,43 +52,50 @@ use Unicaen\Utilisateur\State\UtilisateurPasswordHasherProcessor;
    ],
    normalizationContext: ['groups' => ['user:read']],
    denormalizationContext: ['groups' => ['user:create', 'user:update']],
    openapi: new Operation(
        tags: ['unicaen_utilisateur_user Utilisateur'],
    ),
)]
#[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\MappedSuperclass]
abstract class AbstractUser implements UnicaenUserInterface, PasswordAuthenticatedUserInterface
{
    const PASSWORD_LDAP = 'ldap';
    const PASSWORD_SHIB = 'shib';

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;
    protected ?int $id = null;

    #[ORM\Column(length: 180, unique: true)]
    protected ?string $username = null;

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

    protected ?string $displayName = null;

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

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

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

    /**
     * @var Collection<int, Role>
     */
    #[ORM\ManyToMany(targetEntity: Role::class)]
    private Collection $roles;
    #[ORM\Column]
    protected ?string $passwordResetToken = null;

    #[ORM\ManyToOne(targetEntity: AbstractRole::class)]
    protected RoleInterface $activeRole;

    #[ORM\ManyToMany(targetEntity: AbstractRole::class)]
    protected Collection $roles;

    public function __construct()
    {
@@ -169,6 +177,11 @@ class Utilisateur implements UnicaenUtilisateur, PasswordAuthenticatedUserInterf
        return $this;
    }

    /**
     * Symfony a besoin que la methode getRoles() retourne un tableau de strings.
     *
     * @return array
     */
    public function getRoles(): array
    {
        $roles = $this->roles->map(fn (Role $role) => $role->getCode())->toArray();
@@ -181,7 +194,7 @@ class Utilisateur implements UnicaenUtilisateur, PasswordAuthenticatedUserInterf
        return $this->roles;
    }

    public function addRole(Role $role): static
    public function addRole(RoleInterface $role): static
    {
        if (!$this->roles->contains($role)) {
            $this->roles->add($role);
@@ -190,7 +203,7 @@ class Utilisateur implements UnicaenUtilisateur, PasswordAuthenticatedUserInterf
        return $this;
    }

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

+12 −0
Changes for api/src/Entity/Role.php: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Unicaen\Utilisateur\Entity;
use Unicaen\Utilisateur\Repository\UtilisateurRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: '`unicaen_utilisateur_role`')]
class Role extends AbstractRole
{

}
 No newline at end of file
+23 −0
Changes for api/src/Entity/User.php: 23 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Unicaen\Utilisateur\Entity;

use Unicaen\Utilisateur\Model\Role;
use Unicaen\Utilisateur\Repository\UtilisateurRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: UtilisateurRepository::class)]
#[ORM\Table(name: '`unicaen_utilisateur_user`')]
class User extends AbstractUser
{

    public function setRoles(array $roles): void
    {
        // TODO: Implement setRoles() method.
    }

    public function hasRole(RoleInterface $role): bool
    {
        // TODO: Implement hasRole() method.
        return in_array($role, $this->roles->toArray(), true);
    }
}
 No newline at end of file
+43 −0
Changes for api/src/Model/RoleInterface.php: 43 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Unicaen\Utilisateur\Entity;

use Doctrine\Common\Collections\Collection;
use Unicaen\Utilisateur\Model\UnicaenUserInterface;

interface RoleInterface
{
    public function __toString() : string;

    public function getId() : ?int;
    public function setId(?int $id) : void;
    public function getCode() : ?string;
    public function setCode(?string $code) : void;
    public function getLibelle() : ?string;
    public function setLibelle(?string $libelle) : void;
    public function getDescription() : ?string;
    public function setDescription(?string $description) : void;

    public function isDefault() : bool;
    public function setDefault(bool $default) : void;
    public function isAuto() : bool;
    public function setAuto(bool $auto) : void;
    public function isAccessibleExterieur() : bool;
    public function setAccessibleExterieur(bool $accessibleExterieur) : void;
    public function isDisplayed() : bool;
    public function setDisplayed(bool $accessibleExterieur) : void;

    public function getParent() : ?RoleInterface;
    public function setParent(?RoleInterface $parent = null) : void;
    public function getLdapFilter() : ?string;
    public function setLdapFilter(?string $ldapFilter) : void;

    public function getUsers() : Collection;
    public function addUser(UnicaenUserInterface $user) : void;
    public function removeUser(UnicaenUserInterface $user) : void;

//    public function getPrivileges();
//    public function addPrivilege(PrivilegeInterface $privilege) : void;
//    public function removePrivilege(PrivilegeInterface $privilege) : void;

}
 No newline at end of file
Loading