1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProjetNormandie\UserBundle\Controller\ResetPassword; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Exception; |
7
|
|
|
use ProjetNormandie\UserBundle\Manager\UserManager; |
8
|
|
|
use ProjetNormandie\UserBundle\Security\Event\SecurityEventTypeEnum; |
9
|
|
|
use ProjetNormandie\UserBundle\Security\SecurityHistoryManager; |
10
|
|
|
use ProjetNormandie\UserBundle\Util\TokenGenerator; |
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
12
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\HttpKernel\Attribute\AsController; |
15
|
|
|
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
16
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
17
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
18
|
|
|
use Symfony\Component\Mime\Email; |
19
|
|
|
|
20
|
|
|
#[AsController] |
21
|
|
|
class SendPasswordResetLink extends AbstractController |
22
|
|
|
{ |
23
|
|
|
public function __construct( |
24
|
|
|
private readonly UserManager $userManager, |
25
|
|
|
private readonly TokenGenerator $tokenGenerator, |
26
|
|
|
private readonly MailerInterface $mailer, |
27
|
|
|
private readonly TranslatorInterface $translator, |
28
|
|
|
private readonly SecurityHistoryManager $securityHistoryManager, |
29
|
|
|
private readonly int $retryTtl = 7200 |
30
|
|
|
) { |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Request $request |
36
|
|
|
* @return JsonResponse |
37
|
|
|
* @throws TransportExceptionInterface |
38
|
|
|
* @throws Exception |
39
|
|
|
*/ |
40
|
|
|
public function __invoke(Request $request): JsonResponse |
41
|
|
|
{ |
42
|
|
|
$data = json_decode($request->getContent(), true); |
43
|
|
|
$email = $data['email']; |
44
|
|
|
$callBackUrl = $data['callBackUrl']; |
45
|
|
|
|
46
|
|
|
$user = $this->userManager->findUserByUsernameOrEmail($email); |
47
|
|
|
if ($user && (null === $user->getPasswordRequestedAt() || $user->isPasswordRequestExpired($this->retryTtl))) { |
48
|
|
|
$user->setConfirmationToken($this->tokenGenerator->generateToken()); |
49
|
|
|
$body = sprintf( |
50
|
|
|
$this->translator->trans('password_reset.message', [], 'email', $user->getLanguage()), |
51
|
|
|
$user->getUsername(), |
52
|
|
|
($request->server->get('HTTP_ORIGIN') ?? null) . |
53
|
|
|
str_replace('[token]', $user->getConfirmationToken(), $callBackUrl) |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$email = (new Email()) |
57
|
|
|
->to($user->getEmail()) |
58
|
|
|
->subject($this->translator->trans('password_reset.subject', [], 'email', $user->getLanguage())) |
59
|
|
|
->text($body) |
60
|
|
|
->html($body); |
61
|
|
|
|
62
|
|
|
$this->mailer->send($email); |
63
|
|
|
|
64
|
|
|
$user->setPasswordRequestedAt(new DateTime()); |
65
|
|
|
$this->userManager->updateUser($user); |
66
|
|
|
|
67
|
|
|
$this->securityHistoryManager->recordEvent($user, SecurityEventTypeEnum::PASSWORD_RESET_REQUEST, [ |
68
|
|
|
'email' => $user->getEmail() |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return new JsonResponse(['success' => true]); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|