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

Assignation des roles

parent 4a93d024
Loading
Loading
Loading
Loading
Loading
+6 −0
Changes for api/src/Entity/AbstractRole.php: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -162,7 +162,13 @@ abstract class AbstractRole implements RoleInterface

    public function addPrivilege(PrivilegeInterface $privilege) : void
    {
        if (!$this->privileges->contains($privilege)) {
            $this->privileges->add($privilege);

            if (!$privilege->hasRole($this)) {
                $privilege->addRole($this);
            }
        }
    }

    public function hasPrivilege(PrivilegeInterface $privilege) : bool
+16 −0
Changes for api/src/Entity/AbstractUser.php: 16 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ 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\Serializer\Attribute\Ignore;
use Symfony\Component\Serializer\Attribute\SerializedName;
use Symfony\Component\Validator\Constraints as Assert;
use Unicaen\Utilisateur\Dto\UtilisateurInscriptionInput;
@@ -161,6 +162,7 @@ abstract class AbstractUser implements UnicaenUserInterface, PasswordAuthenticat
     *
     * @return array
     */
    #[Ignore]
    public function getRoles(): array
    {
        $roles = $this->roles->map(fn (Role $role) => $role->getCode())->toArray();
@@ -175,8 +177,22 @@ abstract class AbstractUser implements UnicaenUserInterface, PasswordAuthenticat
        return $this->roles;
    }

    /**
     * @param iterable<\Unicaen\Utilisateur\Entity\Role> $roles
     */
    #[Groups(['user:create', 'user:update'])]
    #[SerializedName('roles')]
    public function setRolesEntities(iterable $roles): void
    {
        $this->roles->clear();
        foreach ($roles as $role) {
            $this->roles->add($role);
        }
    }

    public function addRole(RoleInterface $role): static
    {
        \trigger_error('addRole called for '.$role->getId());
        if (!$this->roles->contains($role)) {
            $this->roles->add($role);
        }
+6 −8
Changes for api/src/Entity/User.php: 6 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -40,7 +40,12 @@ use Unicaen\Utilisateur\State\UtilisateurPasswordHasherProcessor;
            processor: UtilisateurInscriptionProcessor::class
        ),
        new Put(processor: UtilisateurPasswordHasherProcessor::class),
        new Patch(processor: UtilisateurPasswordHasherProcessor::class),
        new Patch(
            denormalizationContext: [
                'groups' => ['user:update'],
            ],
            processor: UtilisateurPasswordHasherProcessor::class
        ),
        new Delete(),
    ],
    normalizationContext: ['groups' => ['user:read']],
@@ -53,15 +58,8 @@ use Unicaen\Utilisateur\State\UtilisateurPasswordHasherProcessor;
#[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
+0 −1
Changes for api/src/Model/UnicaenUserInterface.php: 0 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -8,7 +8,6 @@ interface UnicaenUserInterface extends UserInterface
{

    public function getRoles(): array;
    public function setRoles(array $roles): void;

    public function hasRole(RoleInterface $role): bool;
}
+1877 −1898

File changed.

Preview size limit exceeded, changes collapsed.

Loading