feat: Activation de compte utilisateur avec validation token
L'inscription Classeo se fait via invitation : un admin crée un compte, l'utilisateur reçoit un lien d'activation par email pour définir son mot de passe. Ce flow sécurisé évite les inscriptions non autorisées et garantit que seuls les utilisateurs légitimes accèdent au système. Points clés de l'implémentation : - Tokens d'activation à usage unique stockés en cache (Redis/filesystem) - Validation du consentement parental pour les mineurs < 15 ans (RGPD) - L'échec d'activation ne consume pas le token (retry possible) - Users dans un cache séparé sans TTL (pas d'expiration) - Hot reload en dev (FrankenPHP sans mode worker) Story: 1.3 - Inscription et activation de compte
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Administration\Infrastructure\Persistence\InMemory;
|
||||
|
||||
use App\Administration\Domain\Exception\ActivationTokenNotFoundException;
|
||||
use App\Administration\Domain\Model\ActivationToken\ActivationToken;
|
||||
use App\Administration\Domain\Model\ActivationToken\ActivationTokenId;
|
||||
use App\Administration\Domain\Repository\ActivationTokenRepository;
|
||||
use Override;
|
||||
|
||||
final class InMemoryActivationTokenRepository implements ActivationTokenRepository
|
||||
{
|
||||
/** @var array<string, ActivationToken> Indexed by token value */
|
||||
private array $byTokenValue = [];
|
||||
|
||||
/** @var array<string, string> Maps ID to token value */
|
||||
private array $idToTokenValue = [];
|
||||
|
||||
#[Override]
|
||||
public function save(ActivationToken $token): void
|
||||
{
|
||||
$this->byTokenValue[$token->tokenValue] = $token;
|
||||
$this->idToTokenValue[(string) $token->id] = $token->tokenValue;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findByTokenValue(string $tokenValue): ?ActivationToken
|
||||
{
|
||||
return $this->byTokenValue[$tokenValue] ?? null;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function get(ActivationTokenId $id): ActivationToken
|
||||
{
|
||||
$tokenValue = $this->idToTokenValue[(string) $id] ?? null;
|
||||
|
||||
if ($tokenValue === null) {
|
||||
throw ActivationTokenNotFoundException::withId($id);
|
||||
}
|
||||
|
||||
$token = $this->byTokenValue[$tokenValue] ?? null;
|
||||
|
||||
if ($token === null) {
|
||||
throw ActivationTokenNotFoundException::withId($id);
|
||||
}
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function delete(ActivationTokenId $id): void
|
||||
{
|
||||
$tokenValue = $this->idToTokenValue[(string) $id] ?? null;
|
||||
|
||||
if ($tokenValue !== null) {
|
||||
unset($this->byTokenValue[$tokenValue]);
|
||||
}
|
||||
|
||||
unset($this->idToTokenValue[(string) $id]);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function deleteByTokenValue(string $tokenValue): void
|
||||
{
|
||||
$token = $this->byTokenValue[$tokenValue] ?? null;
|
||||
|
||||
if ($token !== null) {
|
||||
unset($this->idToTokenValue[(string) $token->id]);
|
||||
}
|
||||
|
||||
unset($this->byTokenValue[$tokenValue]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Administration\Infrastructure\Persistence\InMemory;
|
||||
|
||||
use App\Administration\Domain\Exception\UserNotFoundException;
|
||||
use App\Administration\Domain\Model\User\Email;
|
||||
use App\Administration\Domain\Model\User\User;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Administration\Domain\Repository\UserRepository;
|
||||
use Override;
|
||||
|
||||
final class InMemoryUserRepository implements UserRepository
|
||||
{
|
||||
/** @var array<string, User> Indexed by ID */
|
||||
private array $byId = [];
|
||||
|
||||
/** @var array<string, User> Indexed by email (lowercase) */
|
||||
private array $byEmail = [];
|
||||
|
||||
#[Override]
|
||||
public function save(User $user): void
|
||||
{
|
||||
$this->byId[(string) $user->id] = $user;
|
||||
$this->byEmail[strtolower((string) $user->email)] = $user;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function get(UserId $id): User
|
||||
{
|
||||
$user = $this->byId[(string) $id] ?? null;
|
||||
|
||||
if ($user === null) {
|
||||
throw UserNotFoundException::withId($id);
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function findByEmail(Email $email): ?User
|
||||
{
|
||||
return $this->byEmail[strtolower((string) $email)] ?? null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user