ResetPasswordController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\ChangePasswordFormType;
  5. use App\Form\ResetPasswordRequestFormType;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Mailer\MailerInterface;
  13. use Symfony\Component\Mime\Address;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. use Symfony\Component\Routing\Attribute\Route;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. use SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait;
  18. use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface;
  19. use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
  20. #[Route('/reset-password')]
  21. class ResetPasswordController extends AbstractController
  22. {
  23. use ResetPasswordControllerTrait;
  24. public function __construct(
  25. private ResetPasswordHelperInterface $resetPasswordHelper,
  26. private EntityManagerInterface $entityManager
  27. ) {
  28. }
  29. /**
  30. * Display & process form to request a password reset.
  31. */
  32. #[Route('', name: 'app_forgot_password_request')]
  33. public function request(Request $request, MailerInterface $mailer, TranslatorInterface $translator): Response
  34. {
  35. $form = $this->createForm(ResetPasswordRequestFormType::class);
  36. $form->handleRequest($request);
  37. if ($form->isSubmitted() && $form->isValid()) {
  38. /** @var string $email */
  39. $email = $form->get('email')->getData();
  40. return $this->processSendingPasswordResetEmail($email, $mailer, $translator
  41. );
  42. }
  43. return $this->render('reset_password/request.html.twig', [
  44. 'requestForm' => $form,
  45. ]);
  46. }
  47. /**
  48. * Confirmation page after a user has requested a password reset.
  49. */
  50. #[Route('/check-email', name: 'app_check_email')]
  51. public function checkEmail(): Response
  52. {
  53. // Generate a fake token if the user does not exist or someone hit this page directly.
  54. // This prevents exposing whether or not a user was found with the given email address or not
  55. if (null === ($resetToken = $this->getTokenObjectFromSession())) {
  56. $resetToken = $this->resetPasswordHelper->generateFakeResetToken();
  57. }
  58. return $this->render('reset_password/check_email.html.twig', [
  59. 'resetToken' => $resetToken,
  60. ]);
  61. }
  62. /**
  63. * Validates and process the reset URL that the user clicked in their email.
  64. */
  65. #[Route('/reset/{token}', name: 'app_reset_password')]
  66. public function reset(Request $request, UserPasswordHasherInterface $passwordHasher, TranslatorInterface $translator, ?string $token = null): Response
  67. {
  68. if ($token) {
  69. // We store the token in session and remove it from the URL, to avoid the URL being
  70. // loaded in a browser and potentially leaking the token to 3rd party JavaScript.
  71. $this->storeTokenInSession($token);
  72. return $this->redirectToRoute('app_reset_password');
  73. }
  74. $token = $this->getTokenFromSession();
  75. if (null === $token) {
  76. throw $this->createNotFoundException('No reset password token found in the URL or in the session.');
  77. }
  78. try {
  79. /** @var User $user */
  80. $user = $this->resetPasswordHelper->validateTokenAndFetchUser($token);
  81. } catch (ResetPasswordExceptionInterface $e) {
  82. $this->addFlash('reset_password_error', sprintf(
  83. '%s - %s',
  84. $translator->trans(ResetPasswordExceptionInterface::MESSAGE_PROBLEM_VALIDATE, [], 'ResetPasswordBundle'),
  85. $translator->trans($e->getReason(), [], 'ResetPasswordBundle')
  86. ));
  87. return $this->redirectToRoute('app_forgot_password_request');
  88. }
  89. // The token is valid; allow the user to change their password.
  90. $form = $this->createForm(ChangePasswordFormType::class);
  91. $form->handleRequest($request);
  92. if ($form->isSubmitted() && $form->isValid()) {
  93. // A password reset token should be used only once, remove it.
  94. $this->resetPasswordHelper->removeResetRequest($token);
  95. /** @var string $plainPassword */
  96. $plainPassword = $form->get('plainPassword')->getData();
  97. // Encode(hash) the plain password, and set it.
  98. $user->setPassword($passwordHasher->hashPassword($user, $plainPassword));
  99. $this->entityManager->flush();
  100. // The session is cleaned up after the password has been changed.
  101. $this->cleanSessionAfterReset();
  102. return $this->redirectToRoute('app_main');
  103. }
  104. return $this->render('reset_password/reset.html.twig', [
  105. 'resetForm' => $form,
  106. ]);
  107. }
  108. private function processSendingPasswordResetEmail(string $emailFormData, MailerInterface $mailer, TranslatorInterface $translator): RedirectResponse
  109. {
  110. $user = $this->entityManager->getRepository(User::class)->findOneBy([
  111. 'email' => $emailFormData,
  112. ]);
  113. // Do not reveal whether a user account was found or not.
  114. if (!$user) {
  115. return $this->redirectToRoute('app_check_email');
  116. }
  117. try {
  118. $resetToken = $this->resetPasswordHelper->generateResetToken($user);
  119. } catch (ResetPasswordExceptionInterface $e) {
  120. // If you want to tell the user why a reset email was not sent, uncomment
  121. // the lines below and change the redirect to 'app_forgot_password_request'.
  122. // Caution: This may reveal if a user is registered or not.
  123. //
  124. // $this->addFlash('reset_password_error', sprintf(
  125. // '%s - %s',
  126. // $translator->trans(ResetPasswordExceptionInterface::MESSAGE_PROBLEM_HANDLE, [], 'ResetPasswordBundle'),
  127. // $translator->trans($e->getReason(), [], 'ResetPasswordBundle')
  128. // ));
  129. return $this->redirectToRoute('app_check_email');
  130. }
  131. $email = (new TemplatedEmail())
  132. ->from(new Address($_ENV['CONTACT_EMAIL'], $_ENV['CONTACT_NAME']))
  133. ->to((string) $user->getEmail())
  134. ->subject('Votre demande de réinitialisation de mot de passe')
  135. ->htmlTemplate('reset_password/email.html.twig')
  136. ->textTemplate('reset_password/email.txt.twig')
  137. ->context([
  138. 'resetToken' => $resetToken,
  139. ])
  140. ;
  141. $mailer->send($email);
  142. // Store the token object in session for retrieval in check-email route.
  143. $this->setTokenObjectInSession($resetToken);
  144. return $this->redirectToRoute('app_check_email');
  145. }
  146. }