vendor/eckinox/security-bundle/src/Controller/ResetPasswordController.php line 52

Open in your IDE?
  1. <?php
  2. namespace Eckinox\SecurityBundle\Controller;
  3. use App\Entity\Security\User;
  4. use App\Entity\Security\AppUser;
  5. use Eckinox\SecurityBundle\Form\ChangePasswordFormType;
  6. use Eckinox\SecurityBundle\Form\ResetPasswordRequestFormType;
  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\RequestStack;
  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\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. use SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait;
  18. use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface;
  19. use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
  20. use Symfony\Contracts\Translation\TranslatorInterface;
  21. use Twig\Environment;
  22. class ResetPasswordController extends AbstractController
  23. {
  24.     use ResetPasswordControllerTrait;
  25.     /**
  26.      * @var UserPasswordHasherInterface
  27.      */
  28.     private $passwordHasher;
  29.     /**
  30.      * @var TranslatorInterface
  31.      */
  32.     private $translator;
  33.     public function __construct(TranslatorInterface $translatorRequestStack $requestStackEnvironment $twigResetPasswordHelperInterface $userResetPasswordHelperResetPasswordHelperInterface $appUserResetPasswordHelper)
  34.     {
  35.         $this->translator $translator;
  36.         $this->request $requestStack->getCurrentRequest();
  37.         $this->firewall $this->request->attributes->get('_firewall_context');
  38.         $this->twig $twig->getLoader();
  39.         $this->userClass $this->firewall == 'security.firewall.map.context.app' AppUser::class : User::class;
  40.         $this->resetPasswordHelper $this->firewall == 'security.firewall.map.context.app' $appUserResetPasswordHelper $userResetPasswordHelper;
  41.     }
  42.     /**
  43.      * Display & process form to request a password reset.
  44.      */
  45.     public function request(MailerInterface $mailer): Response
  46.     {
  47.         $template $this->firewall == 'security.firewall.map.context.app' && $this->twig->exists('@EckinoxSecurity/app_reset_password/request.html.twig') ? '@EckinoxSecurity/app_reset_password/request.html.twig' '@EckinoxSecurity/reset_password/request.html.twig';
  48.         $form $this->createForm(ResetPasswordRequestFormType::class);
  49.         $form->handleRequest($this->request);
  50.         if ($form->isSubmitted() && $form->isValid()) {
  51.             return $this->processSendingPasswordResetEmail(
  52.                 $form->get('email')->getData(),
  53.                 $mailer
  54.             );
  55.         }
  56.         return $this->render($template, [
  57.             'requestForm' => $form->createView(),
  58.         ]);
  59.     }
  60.     /**
  61.      * Confirmation page after a user has requested a password reset.
  62.      */
  63.     public function checkEmail(): Response
  64.     {
  65.         $redirectRouteParameter $this->firewall == 'security.firewall.map.context.app' 'eckinox_security_app_forgot_password_request' 'eckinox_security_forgot_password_request';
  66.         $template $this->firewall == 'security.firewall.map.context.app' && $this->twig->exists('@EckinoxSecurity/app_reset_password/check_email.html.twig') ? '@EckinoxSecurity/app_reset_password/check_email.html.twig' '@EckinoxSecurity/reset_password/check_email.html.twig';
  67.         // We prevent users from directly accessing this page
  68.         if (null === ($resetToken $this->getTokenObjectFromSession())) {
  69.             return $this->redirectToRoute($redirectRouteParameter);
  70.         }
  71.         return $this->render($template, [
  72.             'resetToken' => $resetToken,
  73.         ]);
  74.     }
  75.     /**
  76.      * Validates and process the reset URL that the user clicked in their email.
  77.      */
  78.     public function reset(UserPasswordHasherInterface $passwordHasherstring $token null): Response
  79.     {
  80.         $redirectResetRouteParameter $this->firewall == 'security.firewall.map.context.app' 'eckinox_security_app_reset_password' 'eckinox_security_reset_password';
  81.         $redirectForgotRouteParameter $this->firewall == 'security.firewall.map.context.app' 'eckinox_security_app_forgot_password_request' 'eckinox_security_forgot_password_request';
  82.         $redirectLoginRouteParameter $this->firewall == 'security.firewall.map.context.app' 'eckinox_security_app_login' 'eckinox_security_login';
  83.         $template $this->firewall == 'security.firewall.map.context.app' && $this->twig->exists('@EckinoxSecurity/app_reset_password/reset.html.twig') ? '@EckinoxSecurity/app_reset_password/reset.html.twig' '@EckinoxSecurity/reset_password/reset.html.twig';
  84.         if ($token) {
  85.             // We store the token in session and remove it from the URL, to avoid the URL being
  86.             // loaded in a browser and potentially leaking the token to 3rd party JavaScript.
  87.             $this->storeTokenInSession($token);
  88.             return $this->redirectToRoute($redirectResetRouteParameter);
  89.         }
  90.         $token $this->getTokenFromSession();
  91.         if (null === $token) {
  92.             throw $this->createNotFoundException('No reset password token found in the URL or in the session.');
  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.             return $this->redirectToRoute($redirectForgotRouteParameter);
  102.         }
  103.         $form $this->createForm(ChangePasswordFormType::class);
  104.         $form->handleRequest($this->request);
  105.         if ($form->isSubmitted() && $form->isValid()) {
  106.             $this->resetPasswordHelper->removeResetRequest($token);
  107.             $encodedPassword $passwordHasher->hashPassword(
  108.                 $user,
  109.                 $form->get('plainPassword')->getData()
  110.             );
  111.             $user->setPassword($encodedPassword);
  112.             $this->getDoctrine()->getManager()->flush();
  113.             $this->cleanSessionAfterReset();
  114.             $this->addFlash('success'$this->translator->trans('eckinox_security.messages.your_password_has_been_reset'));
  115.             return $this->redirectToRoute($redirectLoginRouteParameter);
  116.         }
  117.         return $this->render($template, [
  118.             'resetForm' => $form->createView(),
  119.         ]);
  120.     }
  121.     private function processSendingPasswordResetEmail(string $emailFormDataMailerInterface $mailer): RedirectResponse
  122.     {
  123.         $redirectRouteParameter $this->firewall == 'security.firewall.map.context.app' 'eckinox_security_app_forgot_password_request' 'eckinox_security_forgot_password_request';
  124.         $redirectCheckEmailRouteParameter $this->firewall == 'security.firewall.map.context.app' 'eckinox_security_app_check_email' 'eckinox_security_check_email';
  125.         $resetPasswordRouteParameter $this->firewall == 'security.firewall.map.context.app' 'eckinox_security_app_reset_password' 'eckinox_security_reset_password';
  126.         $template $this->firewall == 'security.firewall.map.context.app' && $this->twig->exists('@EckinoxSecurity/app_reset_password/email.html.twig') ? '@EckinoxSecurity/app_reset_password/email.html.twig' '@EckinoxSecurity/reset_password/email.html.twig';
  127.         $user $this->getDoctrine()->getRepository($this->userClass)->findOneBy([
  128.             'email' => $emailFormData,
  129.         ]);
  130.         // Do not reveal whether a user account was found or not.
  131.         if (!$user) {
  132.             return $this->redirectToRoute($redirectCheckEmailRouteParameter);
  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 'eckinox_security_forgot_password_request'.
  139.             // Caution: This may reveal if a user is registered or not.
  140.             $this->addFlash('reset_password_error'$this->translator->trans('eckinox_security.messages.' $e->getReason()));
  141.             return $this->redirectToRoute($redirectRouteParameter);
  142.         }
  143.         $email = (new TemplatedEmail())
  144.             ->from(new Address($this->getParameter('eckinox_security.sender_email'), $this->getParameter('eckinox_security.sender_name')))
  145.             ->to($user->getEmail())
  146.             ->subject($this->translator->trans('eckinox_security.messages.your_password_reset_request'))
  147.             ->htmlTemplate($template)
  148.             ->context([
  149.                 'resetToken' => $resetToken,
  150.                 'reset_password_route' => $resetPasswordRouteParameter
  151.             ]);
  152.         $mailer->send($email);
  153.         $this->setTokenObjectInSession($resetToken);
  154.         return $this->redirectToRoute($redirectCheckEmailRouteParameter);
  155.     }
  156. }