src/Controller/ResetPasswordController.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Constant\RouteTitleConstant;
  4. use App\Entity\User;
  5. use App\Form\ChangePasswordFormType;
  6. use App\Form\ResetPasswordRequestFormType;
  7. use Doctrine\ORM\EntityManagerInterface;
  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\Exception\TransportExceptionInterface;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use Symfony\Component\Mime\Address;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait;
  19. use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface;
  20. use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
  21. /**
  22.  * @Route("/reset-password")
  23.  */
  24. class ResetPasswordController extends AbstractController
  25. {
  26.   use ResetPasswordControllerTrait;
  27.   private $resetPasswordHelper;
  28.   private $entityManager;
  29.   public function __construct(ResetPasswordHelperInterface $resetPasswordHelperEntityManagerInterface $entityManager)
  30.   {
  31.     $this->resetPasswordHelper $resetPasswordHelper;
  32.     $this->entityManager $entityManager;
  33.   }
  34.   /**
  35.    * Display & process form to request a password reset.
  36.    *
  37.    * @Route("", name="app_forgot_password_request")
  38.    */
  39.   public function request(Request $requestMailerInterface $mailer): Response
  40.   {
  41.     $title RouteTitleConstant::RESET_PASSWORD;
  42.     $form $this->createForm(ResetPasswordRequestFormType::class);
  43.     $form->handleRequest($request);
  44.     if ($form->isSubmitted() && $form->isValid()) {
  45.       return $this->processSendingPasswordResetEmail(
  46.         $form->get('email')->getData(),
  47.         $mailer
  48.       );
  49.     }
  50.     return $this->render('reset_password/request.html.twig', [
  51.       'title' => $title,
  52.       'requestForm' => $form->createView(),
  53.     ]);
  54.   }
  55.   /**
  56.    * Confirmation page after a user has requested a password reset.
  57.    *
  58.    * @Route("/check-email", name="app_check_email")
  59.    */
  60.   public function checkEmail(): Response
  61.   {
  62.     $title RouteTitleConstant::PASSWORD_RESET;
  63.     // Generate a fake token if the user does not exist or someone hit this page directly.
  64.     // This prevents exposing whether or not a user was found with the given email address or not
  65.     if (null === ($resetToken $this->getTokenObjectFromSession())) {
  66.       $resetToken $this->resetPasswordHelper->generateFakeResetToken();
  67.     }
  68.     return $this->render('reset_password/check_email.html.twig', [
  69.       'title'=>$title,
  70.       'resetToken' => $resetToken,
  71.     ]);
  72.   }
  73.   /**
  74.    * Validates and process the reset URL that the user clicked in their email.
  75.    *
  76.    * @Route("/reset/{token}", name="app_reset_password")
  77.    */
  78.   public function reset(Request $requestUserPasswordHasherInterface $userPasswordHasherstring $token null): Response
  79.   {
  80.     $title RouteTitleConstant::RESET_YOUR_PASS;
  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.       return $this->redirectToRoute('app_reset_password');
  86.     }
  87.     $token $this->getTokenFromSession();
  88.     if (null === $token) {
  89.       throw $this->createNotFoundException('No reset password token found in the URL or in the session.');
  90.     }
  91.     try {
  92.       $user $this->resetPasswordHelper->validateTokenAndFetchUser($token);
  93.     } catch (ResetPasswordExceptionInterface $e) {
  94.       $this->addFlash('reset_password_error'sprintf(
  95.         'Un problème est survenu lors de la validation de votre demande de réinitialisation - %s',
  96.         $e->getReason()
  97.       ));
  98.       return $this->redirectToRoute('app_forgot_password_request');
  99.     }
  100.     // The token is valid; allow the user to change their password.
  101.     $form $this->createForm(ChangePasswordFormType::class);
  102.     $form->handleRequest($request);
  103.     if ($form->isSubmitted() && $form->isValid()) {
  104.       // A password reset token should be used only once, remove it.
  105.       $this->resetPasswordHelper->removeResetRequest($token);
  106.       // Encode(hash) the plain password, and set it.
  107.       $encodedPassword $userPasswordHasher->hashPassword(
  108.         $user,
  109.         $form->get('plainPassword')->getData()
  110.       );
  111.       $user->setPassword($encodedPassword);
  112.       $this->entityManager->flush();
  113.       // The session is cleaned up after the password has been changed.
  114.       $this->cleanSessionAfterReset();
  115.       return $this->redirectToRoute('app_dashboard');
  116.     }
  117.     return $this->render('reset_password/reset.html.twig', [
  118.       'title'=>$title,
  119.       'resetForm' => $form->createView(),
  120.     ]);
  121.   }
  122.   /**
  123.    * @param string $emailFormData
  124.    * @param MailerInterface $mailer
  125.    * @return RedirectResponse
  126.    * @throws TransportExceptionInterface
  127.    */
  128.   private function processSendingPasswordResetEmail(string $emailFormDataMailerInterface $mailer): RedirectResponse
  129.   {
  130.     $user $this->entityManager->getRepository(User::class)->findOneBy([
  131.       'email' => $emailFormData,
  132.     ]);
  133.     // Do not reveal whether a user account was found or not.
  134.     if (!$user) {
  135.       return $this->redirectToRoute('app_check_email');
  136.     }
  137.     try {
  138.       $resetToken $this->resetPasswordHelper->generateResetToken($user);
  139.     } catch (ResetPasswordExceptionInterface $e) {
  140.       // If you want to tell the user why a reset email was not sent, uncomment
  141.       // the lines below and change the redirect to 'app_forgot_password_request'.
  142.       // Caution: This may reveal if a user is registered or not.
  143.       //
  144.        $this->addFlash('reset_password_error'sprintf(
  145.            'Un problème est survenu lors du traitement de votre demande de réinitialisation de mot de passe - %s',
  146.            $e->getReason()
  147.        ));
  148.       return $this->redirectToRoute('app_check_email');
  149.     }
  150.     $email = (new TemplatedEmail())
  151.       ->from(new Address('kromi@kromi.fr''Occupation Kromi'))
  152.       ->to($user->getEmail())
  153.       ->subject('Votre demande de réinitialisation de mot de passe')
  154.       ->htmlTemplate('reset_password/email.html.twig')
  155.       ->context([
  156.         'resetToken' => $resetToken,
  157.       ]);
  158.     $mailer->send($email);
  159.     // Store the token object in session for retrieval in check-email route.
  160.     $this->setTokenObjectInSession($resetToken);
  161.     return $this->redirectToRoute('app_check_email');
  162.   }
  163. }