-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ajout de la fonctionnalité mot de passe oublié
- Loading branch information
Showing
29 changed files
with
855 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Application; | ||
|
||
use App\Domain\Mail; | ||
|
||
interface MailerInterface | ||
{ | ||
public function send(Mail $mail): void; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Application/User/Command/Mail/SendForgotPasswordMailCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Application\User\Command\Mail; | ||
|
||
use App\Application\AsyncCommandInterface; | ||
|
||
final class SendForgotPasswordMailCommand implements AsyncCommandInterface | ||
{ | ||
public ?string $email; | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Application/User/Command/Mail/SendForgotPasswordMailCommandHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Application\User\Command\Mail; | ||
|
||
use App\Application\CommandBusInterface; | ||
use App\Application\MailerInterface; | ||
use App\Application\User\Command\CreateTokenCommand; | ||
use App\Domain\Mail; | ||
use App\Domain\User\Enum\TokenTypeEnum; | ||
use App\Domain\User\Exception\UserNotFoundException; | ||
|
||
final readonly class SendForgotPasswordMailCommandHandler | ||
{ | ||
public function __construct( | ||
private CommandBusInterface $commandBus, | ||
private MailerInterface $mailer, | ||
) { | ||
} | ||
|
||
public function __invoke(SendForgotPasswordMailCommand $command): void | ||
{ | ||
$email = trim(strtolower($command->email)); | ||
|
||
try { | ||
$token = $this->commandBus->handle( | ||
new CreateTokenCommand( | ||
$email, | ||
TokenTypeEnum::FORGOT_PASSWORD->value, | ||
), | ||
); | ||
|
||
$this->mailer->send( | ||
new Mail( | ||
address: $email, | ||
subject: 'forgot_password.subjet', | ||
template: 'email/user/forgot-password.html.twig', | ||
payload: [ | ||
'token' => $token, | ||
], | ||
), | ||
); | ||
} catch (UserNotFoundException) { | ||
// Do nothing. | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Application\User\Command; | ||
|
||
use App\Application\CommandInterface; | ||
|
||
final class ResetPasswordCommand implements CommandInterface | ||
{ | ||
public ?string $password = ''; | ||
|
||
public function __construct( | ||
public readonly string $token, | ||
) { | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/Application/User/Command/ResetPasswordCommandHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Application\User\Command; | ||
|
||
use App\Application\PasswordHasherInterface; | ||
use App\Domain\User\Enum\TokenTypeEnum; | ||
use App\Domain\User\Exception\TokenExpiredException; | ||
use App\Domain\User\Exception\TokenNotFoundException; | ||
use App\Domain\User\Repository\TokenRepositoryInterface; | ||
use App\Domain\User\Specification\IsTokenExpired; | ||
use App\Domain\User\Token; | ||
|
||
final readonly class ResetPasswordCommandHandler | ||
{ | ||
public function __construct( | ||
private TokenRepositoryInterface $tokenRepository, | ||
private IsTokenExpired $isTokenExpired, | ||
private PasswordHasherInterface $passwordHasher, | ||
) { | ||
} | ||
|
||
public function __invoke(ResetPasswordCommand $command): void | ||
{ | ||
$token = $this->tokenRepository->findOneByTokenAndType( | ||
$command->token, | ||
TokenTypeEnum::FORGOT_PASSWORD->value, | ||
); | ||
|
||
if (!$token instanceof Token) { | ||
throw new TokenNotFoundException(); | ||
} | ||
|
||
if ($this->isTokenExpired->isSatisfiedBy($token)) { | ||
throw new TokenExpiredException(); | ||
} | ||
|
||
$password = $this->passwordHasher->hash($command->password); | ||
$token->getUser()->getPasswordUser()->setPassword($password); | ||
|
||
$this->tokenRepository->remove($token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Domain; | ||
|
||
final readonly class Mail | ||
{ | ||
public function __construct( | ||
public string $address, | ||
public string $subject, | ||
public string $template, | ||
public array $payload = [], | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Domain\User\Exception; | ||
|
||
final class TokenExpiredException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Domain\User\Exception; | ||
|
||
final class TokenNotFoundException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Domain\User\Specification; | ||
|
||
use App\Application\DateUtilsInterface; | ||
use App\Domain\User\Token; | ||
|
||
final class IsTokenExpired | ||
{ | ||
public function __construct( | ||
private readonly DateUtilsInterface $dateUtils, | ||
) { | ||
} | ||
|
||
public function isSatisfiedBy(Token $token): bool | ||
{ | ||
return $this->dateUtils->getNow() > $token->getExpirationDate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Infrastructure\Adapter; | ||
|
||
use App\Application\MailerInterface; | ||
use App\Domain\Mail; | ||
use Symfony\Bridge\Twig\Mime\TemplatedEmail; | ||
use Symfony\Component\Mailer\MailerInterface as SymfonyMailer; | ||
use Symfony\Component\Mime\Address; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
final readonly class Mailer implements MailerInterface | ||
{ | ||
public function __construct( | ||
private TranslatorInterface $translator, | ||
private SymfonyMailer $mailer, | ||
) { | ||
} | ||
|
||
public function send(Mail $mail): void | ||
{ | ||
$this->mailer->send( | ||
(new TemplatedEmail()) | ||
->to(new Address($mail->address)) | ||
->subject($this->translator->trans($mail->subject, [], 'emails')) | ||
->htmlTemplate($mail->template) | ||
->context($mail->payload), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.