src/Controller/ResetPasswordController.php line 53

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