feat: Attribution de rôles multiples par utilisateur
Les utilisateurs Classeo étaient limités à un seul rôle, alors que dans la réalité scolaire un directeur peut aussi être enseignant, ou un parent peut avoir un rôle vie scolaire. Cette limitation obligeait à créer des comptes distincts par fonction. Le modèle User supporte désormais plusieurs rôles simultanés avec basculement via le header. L'admin peut attribuer/retirer des rôles depuis l'interface de gestion, avec des garde-fous : pas d'auto- destitution, pas d'escalade de privilèges (seul SUPER_ADMIN peut attribuer SUPER_ADMIN), vérification du statut actif pour le switch de rôle, et TTL explicite sur le cache de rôle actif.
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Administration\Application\Command;
|
||||
|
||||
use App\Administration\Application\Command\RemoveRole\RemoveRoleCommand;
|
||||
use App\Administration\Application\Command\RemoveRole\RemoveRoleHandler;
|
||||
use App\Administration\Domain\Event\RoleRetire;
|
||||
use App\Administration\Domain\Exception\DernierRoleNonRetirableException;
|
||||
use App\Administration\Domain\Exception\RoleNonAttribueException;
|
||||
use App\Administration\Domain\Model\User\Email;
|
||||
use App\Administration\Domain\Model\User\Role;
|
||||
use App\Administration\Domain\Model\User\User;
|
||||
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryUserRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RemoveRoleHandlerTest extends TestCase
|
||||
{
|
||||
private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440002';
|
||||
|
||||
private InMemoryUserRepository $userRepository;
|
||||
private RemoveRoleHandler $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->userRepository = new InMemoryUserRepository();
|
||||
|
||||
$clock = new class implements Clock {
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('2026-02-08 10:00:00');
|
||||
}
|
||||
};
|
||||
|
||||
$this->handler = new RemoveRoleHandler($this->userRepository, $clock);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itRemovesRoleFromUser(): void
|
||||
{
|
||||
$user = $this->createUserWithRoles(Role::PROF, Role::PARENT);
|
||||
$this->userRepository->save($user);
|
||||
|
||||
$result = ($this->handler)(new RemoveRoleCommand(
|
||||
userId: (string) $user->id,
|
||||
role: Role::PARENT->value,
|
||||
));
|
||||
|
||||
self::assertTrue($result->aLeRole(Role::PROF));
|
||||
self::assertFalse($result->aLeRole(Role::PARENT));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itRecordsRoleRetireEvent(): void
|
||||
{
|
||||
$user = $this->createUserWithRoles(Role::PROF, Role::PARENT);
|
||||
$this->userRepository->save($user);
|
||||
$user->pullDomainEvents();
|
||||
|
||||
($this->handler)(new RemoveRoleCommand(
|
||||
userId: (string) $user->id,
|
||||
role: Role::PARENT->value,
|
||||
));
|
||||
|
||||
$savedUser = $this->userRepository->get($user->id);
|
||||
$events = $savedUser->pullDomainEvents();
|
||||
|
||||
self::assertCount(1, $events);
|
||||
self::assertInstanceOf(RoleRetire::class, $events[0]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenRoleNotAssigned(): void
|
||||
{
|
||||
$user = $this->createUser(Role::PROF);
|
||||
$this->userRepository->save($user);
|
||||
|
||||
$this->expectException(RoleNonAttribueException::class);
|
||||
|
||||
($this->handler)(new RemoveRoleCommand(
|
||||
userId: (string) $user->id,
|
||||
role: Role::PARENT->value,
|
||||
));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function itThrowsWhenRemovingLastRole(): void
|
||||
{
|
||||
$user = $this->createUser(Role::PROF);
|
||||
$this->userRepository->save($user);
|
||||
|
||||
$this->expectException(DernierRoleNonRetirableException::class);
|
||||
|
||||
($this->handler)(new RemoveRoleCommand(
|
||||
userId: (string) $user->id,
|
||||
role: Role::PROF->value,
|
||||
));
|
||||
}
|
||||
|
||||
private function createUser(Role $role): User
|
||||
{
|
||||
return User::creer(
|
||||
email: new Email('user@example.com'),
|
||||
role: $role,
|
||||
tenantId: TenantId::fromString(self::TENANT_ID),
|
||||
schoolName: 'École Alpha',
|
||||
dateNaissance: null,
|
||||
createdAt: new DateTimeImmutable('2026-01-15 10:00:00'),
|
||||
);
|
||||
}
|
||||
|
||||
private function createUserWithRoles(Role $role, Role ...$additionalRoles): User
|
||||
{
|
||||
$user = $this->createUser($role);
|
||||
foreach ($additionalRoles as $r) {
|
||||
$user->attribuerRole($r, new DateTimeImmutable());
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user