1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Controller\Auth; |
6
|
|
|
|
7
|
|
|
use App\Controller\BaseController; |
8
|
|
|
use App\Entity\User; |
9
|
|
|
use App\Form\Type\PasswordType; |
10
|
|
|
use App\Form\Type\UserEmailType; |
11
|
|
|
use App\Repository\ResettingRepository; |
12
|
|
|
use App\Service\Auth\ResettingService; |
13
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
16
|
|
|
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException; |
17
|
|
|
use Symfony\Component\RateLimiter\RateLimiterFactory; |
18
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
19
|
|
|
|
20
|
|
|
final class ResetPasswordController extends BaseController |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @Route("/password/reset", methods={"GET|POST"}, name="password_reset") |
24
|
|
|
*/ |
25
|
|
|
public function passwordReset(ResettingService $service, Request $request, RateLimiterFactory $passwordResetPageLimiter): Response |
26
|
|
|
{ |
27
|
|
|
$limiter = $passwordResetPageLimiter->create($request->getClientIp()); |
28
|
|
|
$form = $this->createForm(UserEmailType::class, []); |
29
|
|
|
$form->handleRequest($request); |
30
|
|
|
|
31
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
32
|
|
|
$service->sendResetPasswordLink($request); |
33
|
|
|
} elseif ($form->isSubmitted() && false === $limiter->consume(1)->isAccepted()) { |
34
|
|
|
// Don't allow too many invalid requests |
35
|
|
|
throw new TooManyRequestsHttpException(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->render('auth/passwords/password_reset.html.twig', [ |
39
|
|
|
'site' => $this->site($request), |
40
|
|
|
'form' => $form->createView(), |
41
|
|
|
]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @Route("/password/reset/{token}", methods={"GET|POST"}, name="password_reset_confirm") |
46
|
|
|
*/ |
47
|
|
|
public function passwordResetConfirm(ResettingRepository $repository, Request $request, string $token): Response |
48
|
|
|
{ |
49
|
|
|
/** @var User $user */ |
50
|
|
|
$user = $repository->findOneBy(['confirmation_token' => $token]); |
51
|
|
|
|
52
|
|
|
if (!$user) { |
|
|
|
|
53
|
|
|
// Token not found. |
54
|
|
|
return new RedirectResponse($this->generateUrl('security_login')); |
55
|
|
|
} elseif (!$user->isPasswordRequestNonExpired($user::TOKEN_TTL)) { |
56
|
|
|
// Token has expired. |
57
|
|
|
$this->addFlash('danger', 'message.token_expired'); |
58
|
|
|
|
59
|
|
|
return new RedirectResponse($this->generateUrl('password_reset')); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$form = $this->createForm(PasswordType::class, []); |
63
|
|
|
$form->handleRequest($request); |
64
|
|
|
|
65
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
66
|
|
|
$repository->setPassword($user, $form->getNormData()['password']); |
67
|
|
|
$this->addFlash('success', 'message.password_has_been_reset'); |
68
|
|
|
|
69
|
|
|
return $this->redirectToRoute('security_login'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->render('auth/passwords/password_change.html.twig', [ |
73
|
|
|
'site' => $this->site($request), |
74
|
|
|
'form' => $form->createView(), |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|