forked from KnpLabs/KnpUserBundle
/
ResettingController.php
188 lines (153 loc) · 6.15 KB
/
ResettingController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Controller;
use FOS\UserBundle\CompatibilityUtil;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\GetResponseNullableUserEvent;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\Form\Factory\FactoryInterface;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Mailer\MailerInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Util\TokenGeneratorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
* Controller managing the resetting of the password.
*
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
* @author Christophe Coevoet <stof@notk.org>
*
* @final
*/
class ResettingController extends AbstractController
{
private $eventDispatcher;
private $formFactory;
private $userManager;
private $tokenGenerator;
private $mailer;
/**
* @var int
*/
private $retryTtl;
/**
* @param int $retryTtl
*/
public function __construct(EventDispatcherInterface $eventDispatcher, FactoryInterface $formFactory, UserManagerInterface $userManager, TokenGeneratorInterface $tokenGenerator, MailerInterface $mailer, $retryTtl)
{
$this->eventDispatcher = CompatibilityUtil::upgradeEventDispatcher($eventDispatcher);
$this->formFactory = $formFactory;
$this->userManager = $userManager;
$this->tokenGenerator = $tokenGenerator;
$this->mailer = $mailer;
$this->retryTtl = $retryTtl;
}
/**
* Request reset user password: show form.
*/
public function requestAction(): Response
{
return $this->render('@FOSUser/Resetting/request.html.twig');
}
/**
* Request reset user password: submit form and send email.
*/
public function sendEmailAction(Request $request): Response
{
$username = $request->request->get('username');
$user = $this->userManager->findUserByUsernameOrEmail($username);
$event = new GetResponseNullableUserEvent($user, $request);
$this->eventDispatcher->dispatch($event, FOSUserEvents::RESETTING_SEND_EMAIL_INITIALIZE);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
if (null !== $user && !$user->isPasswordRequestNonExpired($this->retryTtl)) {
$event = new GetResponseUserEvent($user, $request);
$this->eventDispatcher->dispatch($event, FOSUserEvents::RESETTING_RESET_REQUEST);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
if (null === $user->getConfirmationToken()) {
$user->setConfirmationToken($this->tokenGenerator->generateToken());
}
$event = new GetResponseUserEvent($user, $request);
$this->eventDispatcher->dispatch($event, FOSUserEvents::RESETTING_SEND_EMAIL_CONFIRM);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
$this->mailer->sendResettingEmailMessage($user);
$user->setPasswordRequestedAt(new \DateTime());
$this->userManager->updateUser($user);
$event = new GetResponseUserEvent($user, $request);
$this->eventDispatcher->dispatch($event, FOSUserEvents::RESETTING_SEND_EMAIL_COMPLETED);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
}
return new RedirectResponse($this->generateUrl('fos_user_resetting_check_email', ['username' => $username]));
}
/**
* Tell the user to check his email provider.
*/
public function checkEmailAction(Request $request): Response
{
$username = $request->query->get('username');
if (empty($username)) {
// the user does not come from the sendEmail action
return new RedirectResponse($this->generateUrl('fos_user_resetting_request'));
}
return $this->render('@FOSUser/Resetting/check_email.html.twig', [
'tokenLifetime' => ceil($this->retryTtl / 3600),
]);
}
/**
* Reset user password.
*
* @param string $token
*/
public function resetAction(Request $request, $token): Response
{
$user = $this->userManager->findUserByConfirmationToken($token);
if (null === $user) {
return new RedirectResponse($this->container->get('router')->generate('fos_user_security_login'));
}
$event = new GetResponseUserEvent($user, $request);
$this->eventDispatcher->dispatch($event, FOSUserEvents::RESETTING_RESET_INITIALIZE);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
$form = $this->formFactory->createForm();
$form->setData($user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$event = new FormEvent($form, $request);
$this->eventDispatcher->dispatch($event, FOSUserEvents::RESETTING_RESET_SUCCESS);
$this->userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_profile_show');
$response = new RedirectResponse($url);
}
$this->eventDispatcher->dispatch(
new FilterUserResponseEvent($user, $request, $response),
FOSUserEvents::RESETTING_RESET_COMPLETED
);
return $response;
}
return $this->render('@FOSUser/Resetting/reset.html.twig', [
'token' => $token,
'form' => $form->createView(),
]);
}
}