|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Controller; |
| 4 | + |
| 5 | +use App\Entity\User; |
| 6 | +use App\Form\ChangePasswordFormType; |
| 7 | +use App\Form\ResetPasswordRequestFormType; |
| 8 | +use Symfony\Bridge\Twig\Mime\TemplatedEmail; |
| 9 | +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 10 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
| 11 | +use Symfony\Component\HttpFoundation\Request; |
| 12 | +use Symfony\Component\HttpFoundation\Response; |
| 13 | +use Symfony\Component\Mailer\MailerInterface; |
| 14 | +use Symfony\Component\Mime\Address; |
| 15 | +use Symfony\Component\Routing\Annotation\Route; |
| 16 | +use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
| 17 | +use SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait; |
| 18 | +use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface; |
| 19 | +use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * @Route("/reset-password") |
| 23 | + */ |
| 24 | +class ResetPasswordController extends AbstractController |
| 25 | +{ |
| 26 | + use ResetPasswordControllerTrait; |
| 27 | + |
| 28 | + private $resetPasswordHelper; |
| 29 | + |
| 30 | + public function __construct(ResetPasswordHelperInterface $resetPasswordHelper) |
| 31 | + { |
| 32 | + $this->resetPasswordHelper = $resetPasswordHelper; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Display & process form to request a password reset. |
| 37 | + * |
| 38 | + * @Route("", name="app_forgot_password_request") |
| 39 | + */ |
| 40 | + public function request(Request $request, MailerInterface $mailer): Response |
| 41 | + { |
| 42 | + $form = $this->createForm(ResetPasswordRequestFormType::class); |
| 43 | + $form->handleRequest($request); |
| 44 | + |
| 45 | + if ($form->isSubmitted() && $form->isValid()) { |
| 46 | + return $this->processSendingPasswordResetEmail( |
| 47 | + $form->get('email')->getData(), |
| 48 | + $mailer |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + return $this->render('reset_password/request.html.twig', [ |
| 53 | + 'requestForm' => $form->createView(), |
| 54 | + ]); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Confirmation page after a user has requested a password reset. |
| 59 | + * |
| 60 | + * @Route("/check-email", name="app_check_email") |
| 61 | + */ |
| 62 | + public function checkEmail(): Response |
| 63 | + { |
| 64 | + // We prevent users from directly accessing this page |
| 65 | + if (!$this->canCheckEmail()) { |
| 66 | + return $this->redirectToRoute('app_forgot_password_request'); |
| 67 | + } |
| 68 | + |
| 69 | + return $this->render('reset_password/check_email.html.twig', [ |
| 70 | + 'tokenLifetime' => $this->resetPasswordHelper->getTokenLifetime(), |
| 71 | + ]); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Validates and process the reset URL that the user clicked in their email. |
| 76 | + * |
| 77 | + * @Route("/reset/{token}", name="app_reset_password") |
| 78 | + */ |
| 79 | + public function reset(Request $request, UserPasswordEncoderInterface $passwordEncoder, string $token = null): Response |
| 80 | + { |
| 81 | + if ($token) { |
| 82 | + // We store the token in session and remove it from the URL, to avoid the URL being |
| 83 | + // loaded in a browser and potentially leaking the token to 3rd party JavaScript. |
| 84 | + $this->storeTokenInSession($token); |
| 85 | + |
| 86 | + return $this->redirectToRoute('app_reset_password'); |
| 87 | + } |
| 88 | + |
| 89 | + $token = $this->getTokenFromSession(); |
| 90 | + if (null === $token) { |
| 91 | + throw $this->createNotFoundException('No reset password token found in the URL or in the session.'); |
| 92 | + } |
| 93 | + |
| 94 | + try { |
| 95 | + $user = $this->resetPasswordHelper->validateTokenAndFetchUser($token); |
| 96 | + } catch (ResetPasswordExceptionInterface $e) { |
| 97 | + $this->addFlash('reset_password_error', sprintf( |
| 98 | + 'There was a problem validating your reset request - %s', |
| 99 | + $e->getReason() |
| 100 | + )); |
| 101 | + |
| 102 | + return $this->redirectToRoute('app_forgot_password_request'); |
| 103 | + } |
| 104 | + |
| 105 | + // The token is valid; allow the user to change their password. |
| 106 | + $form = $this->createForm(ChangePasswordFormType::class); |
| 107 | + $form->handleRequest($request); |
| 108 | + |
| 109 | + if ($form->isSubmitted() && $form->isValid()) { |
| 110 | + // A password reset token should be used only once, remove it. |
| 111 | + $this->resetPasswordHelper->removeResetRequest($token); |
| 112 | + |
| 113 | + // Encode the plain password, and set it. |
| 114 | + $encodedPassword = $passwordEncoder->encodePassword( |
| 115 | + $user, |
| 116 | + $form->get('plainPassword')->getData() |
| 117 | + ); |
| 118 | + |
| 119 | + $user->setPassword($encodedPassword); |
| 120 | + $this->getDoctrine()->getManager()->flush(); |
| 121 | + |
| 122 | + // The session is cleaned up after the password has been changed. |
| 123 | + $this->cleanSessionAfterReset(); |
| 124 | + |
| 125 | + return $this->redirectToRoute('home'); |
| 126 | + } |
| 127 | + |
| 128 | + return $this->render('reset_password/reset.html.twig', [ |
| 129 | + 'resetForm' => $form->createView(), |
| 130 | + ]); |
| 131 | + } |
| 132 | + |
| 133 | + private function processSendingPasswordResetEmail(string $emailFormData, MailerInterface $mailer): RedirectResponse |
| 134 | + { |
| 135 | + $user = $this->getDoctrine()->getRepository(User::class)->findOneBy([ |
| 136 | + 'email' => $emailFormData, |
| 137 | + ]); |
| 138 | + |
| 139 | + // Marks that you are allowed to see the app_check_email page. |
| 140 | + $this->setCanCheckEmailInSession(); |
| 141 | + |
| 142 | + // Do not reveal whether a user account was found or not. |
| 143 | + if (!$user) { |
| 144 | + return $this->redirectToRoute('app_check_email'); |
| 145 | + } |
| 146 | + |
| 147 | + try { |
| 148 | + $resetToken = $this->resetPasswordHelper->generateResetToken($user); |
| 149 | + } catch (ResetPasswordExceptionInterface $e) { |
| 150 | + // If you want to tell the user why a reset email was not sent, uncomment |
| 151 | + // the lines below and change the redirect to 'app_forgot_password_request'. |
| 152 | + // Caution: This may reveal if a user is registered or not. |
| 153 | + // |
| 154 | + // $this->addFlash('reset_password_error', sprintf( |
| 155 | + // 'There was a problem handling your password reset request - %s', |
| 156 | + // $e->getReason() |
| 157 | + // )); |
| 158 | + |
| 159 | + return $this->redirectToRoute('app_check_email'); |
| 160 | + } |
| 161 | + |
| 162 | + $email = (new TemplatedEmail()) |
| 163 | + -> from( new Address( '[email protected]', 'Symfony Auth App')) |
| 164 | + ->to($user->getEmail()) |
| 165 | + ->subject('Your password reset request') |
| 166 | + ->htmlTemplate('reset_password/email.html.twig') |
| 167 | + ->context([ |
| 168 | + 'resetToken' => $resetToken, |
| 169 | + 'tokenLifetime' => $this->resetPasswordHelper->getTokenLifetime(), |
| 170 | + ]) |
| 171 | + ; |
| 172 | + |
| 173 | + $mailer->send($email); |
| 174 | + |
| 175 | + return $this->redirectToRoute('app_check_email'); |
| 176 | + } |
| 177 | +} |
0 commit comments