feat: Gestion des utilisateurs (invitation, blocage, déblocage)
Permet aux administrateurs d'un établissement de gérer le cycle de vie des comptes utilisateurs : inviter de nouveaux membres, bloquer/débloquer des comptes actifs, et renvoyer des invitations en attente. Chaque mutation vérifie l'appartenance au tenant courant pour empêcher les accès cross-tenant. Le blocage est restreint aux comptes actifs uniquement et un administrateur ne peut pas bloquer son propre compte. Les comptes suspendus reçoivent une erreur 403 spécifique au login (sans déclencher l'escalade du rate limiting) et les tentatives sont tracées dans les métriques Prometheus.
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Administration\Infrastructure\Messaging;
|
||||
|
||||
use App\Administration\Domain\Event\UtilisateurInvite;
|
||||
use App\Administration\Domain\Model\ActivationToken\ActivationToken;
|
||||
use App\Administration\Domain\Model\User\Role;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Administration\Domain\Repository\ActivationTokenRepository;
|
||||
use App\Administration\Domain\Repository\UserRepository;
|
||||
use App\Shared\Domain\Clock;
|
||||
use App\Shared\Infrastructure\Tenant\TenantUrlBuilder;
|
||||
use Symfony\Component\Mailer\MailerInterface;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
use Symfony\Component\Mime\Email;
|
||||
use Twig\Environment;
|
||||
|
||||
/**
|
||||
* Sends an invitation email when a user is invited by an admin.
|
||||
*
|
||||
* Creates an activation token and emails the user with an activation link.
|
||||
*/
|
||||
#[AsMessageHandler(bus: 'event.bus')]
|
||||
final readonly class SendInvitationEmailHandler
|
||||
{
|
||||
public function __construct(
|
||||
private MailerInterface $mailer,
|
||||
private Environment $twig,
|
||||
private ActivationTokenRepository $tokenRepository,
|
||||
private UserRepository $userRepository,
|
||||
private TenantUrlBuilder $tenantUrlBuilder,
|
||||
private Clock $clock,
|
||||
private string $fromEmail = 'noreply@classeo.fr',
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(UtilisateurInvite $event): void
|
||||
{
|
||||
$user = $this->userRepository->get(UserId::fromString((string) $event->userId));
|
||||
|
||||
$token = ActivationToken::generate(
|
||||
userId: (string) $event->userId,
|
||||
email: $event->email,
|
||||
tenantId: $event->tenantId,
|
||||
role: $event->role,
|
||||
schoolName: $user->schoolName,
|
||||
createdAt: $this->clock->now(),
|
||||
);
|
||||
|
||||
$this->tokenRepository->save($token);
|
||||
|
||||
$roleEnum = Role::tryFrom($event->role);
|
||||
$roleLabel = $roleEnum?->label() ?? $event->role;
|
||||
|
||||
$activationUrl = $this->tenantUrlBuilder->build($event->tenantId, '/activate/' . $token->tokenValue);
|
||||
|
||||
$html = $this->twig->render('emails/invitation.html.twig', [
|
||||
'firstName' => $event->firstName,
|
||||
'lastName' => $event->lastName,
|
||||
'role' => $roleLabel,
|
||||
'activationUrl' => $activationUrl,
|
||||
]);
|
||||
|
||||
$email = (new Email())
|
||||
->from($this->fromEmail)
|
||||
->to($event->email)
|
||||
->subject('Invitation à rejoindre Classeo')
|
||||
->html($html);
|
||||
|
||||
$this->mailer->send($email);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user