vendor/friendsofsymfony/user-bundle/src/Controller/ResettingController.php line 67

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