src/Controller/Security/ResettingController.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Security;
  3. use FOS\UserBundle\Event\FilterUserResponseEvent;
  4. use FOS\UserBundle\Event\FormEvent;
  5. use FOS\UserBundle\Event\GetResponseNullableUserEvent;
  6. use FOS\UserBundle\Event\GetResponseUserEvent;
  7. use FOS\UserBundle\FOSUserEvents;
  8. use FOS\UserBundle\Model\UserInterface;
  9. use FOS\UserBundle\Util\TokenGeneratorInterface;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. use App\Service\Mailer;
  17. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  18. use FOS\UserBundle\Controller\ResettingController as BaseResettingController;
  19. use FOS\UserBundle\Form\Factory\FactoryInterface;
  20. use FOS\UserBundle\Model\UserManagerInterface;
  21. use FOS\UserBundle\Mailer\MailerInterface;
  22. use Psr\Log\LoggerInterface;
  23. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  24. /**
  25.  * @see https://symfony.com/doc/current/bundles/FOSUserBundle/overriding_controllers.html
  26.  * @see https://symfony.com/doc/3.4/bundles/inheritance.html
  27.  */
  28. class ResettingController extends AbstractController # BaseResettingController
  29. {
  30.     /**
  31.      * @param EventDispatcherInterface $eventDispatcher
  32.      * @param FactoryInterface         $formFactory
  33.      * @param UserManagerInterface     $userManager
  34.      * @param TokenGeneratorInterface  $tokenGenerator
  35.      * @param MailerInterface          $mailer
  36.      * @param int                      $retryTtl
  37.      */
  38.     private $manager;
  39.     public function __construct(UserManagerInterface $managerEventDispatcherInterface $eventFactoryInterface $formFactory)
  40.     {
  41.         $this->manager $manager;
  42.         $this->event $event;
  43.         $this->formFactory $formFactory;
  44.     }
  45.     /**
  46.      * @param Request $request
  47.      * @Route("/change-password/request", name="back_resetting_request")
  48.      */
  49.     public function requestAction(Request $request)
  50.     {
  51.         $username $request->get('username');
  52.         return $this->render('resetting/request.html.twig'compact('username'));
  53.     }
  54.     /**
  55.      * @param Request $request
  56.      * @Route("/change-password/send-email", name="back_resetting_send_email")
  57.      *
  58.      * @return Response
  59.      */
  60.     public function sendEmailAction(Request $request,TokenGeneratorInterface $tokenGenerator,Mailer $mailer)
  61.     {
  62.         $username $request->request->get('username');
  63.         /** @var UserInterface $user */
  64.         $user $this->manager->findUserByUsernameOrEmail($username);
  65.       //  $this->logger->info('user reset password ==> '.serialize($user));
  66.         if(!$user) {
  67.             return $this->redirectToRoute('back_resetting_request'compact('username'));
  68.         }
  69.         /** @var $dispatcher EventDispatcherInterface */
  70.         $dispatcher $this->event;
  71.         /* Dispatch init event */
  72.         $event = new GetResponseNullableUserEvent($user$request);
  73.         $dispatcher->dispatch(FOSUserEvents::RESETTING_SEND_EMAIL_INITIALIZE$event);
  74.         if (null !== $event->getResponse()) {
  75.             return $event->getResponse();
  76.         }
  77.         $ttl $this->getParameter('fos_user.resetting.retry_ttl');
  78.         if (null !== $user && !$user->isPasswordRequestNonExpired($ttl)) {
  79.             $event = new GetResponseUserEvent($user$request);
  80.             $dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_REQUEST$event);
  81.             if (null !== $event->getResponse()) {
  82.                 return $event->getResponse();
  83.             }
  84.             if (null === $user->getConfirmationToken()) {
  85.                 $user->setConfirmationToken($tokenGenerator->generateToken());
  86.             }
  87.             /* Dispatch confirm event */
  88.             $event = new GetResponseUserEvent($user$request);
  89.             $dispatcher->dispatch(FOSUserEvents::RESETTING_SEND_EMAIL_CONFIRM$event);
  90.             if (null !== $event->getResponse()) {
  91.                 return $event->getResponse();
  92.             }
  93.             // send Email ressetting pwd
  94.             $mailer->sendResettingEmailMessage($user);
  95.             $user->setPasswordRequestedAt(new \DateTime());
  96.             $this->manager->updateUser($user);
  97.             /* Dispatch completed event */
  98.             $event = new GetResponseUserEvent($user$request);
  99.             $dispatcher->dispatch(FOSUserEvents::RESETTING_SEND_EMAIL_COMPLETED$event);
  100.             if (null !== $event->getResponse()) {
  101.                 return $event->getResponse();
  102.             }
  103.         }
  104.         return new RedirectResponse($this->generateUrl('back_resetting_check_email', array('username' => $username)));
  105.     }
  106.     /**
  107.      * Tell the user to check his email provider.
  108.      *
  109.      * @param Request $request
  110.      * @Route("/change-password/check-email", name="back_resetting_check_email")
  111.      *
  112.      * @return Response
  113.      */
  114.     public function checkEmailAction(Request $request)
  115.     {
  116.         $username $request->query->get('username');
  117.         if (empty($username)) {
  118.             // the user does not come from the sendEmail action
  119.             return new RedirectResponse($this->generateUrl('back_resetting_request'));
  120.         }
  121.         return $this->render('resetting/check_email.html.twig', array(
  122.             'tokenLifetime' => ceil($this->getParameter('fos_user.resetting.retry_ttl') / 3600),
  123.         ));
  124.     }
  125.     /**
  126.      * Reset user password.
  127.      *
  128.      * @param Request $request
  129.      * @param string  $token
  130.      * @Route("/change-password/reset/{token}", name="back_resetting_reset")
  131.      *
  132.      * @return Response
  133.      */
  134.     public function resetAction(Request $request$token)
  135.     {
  136.         /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
  137.         $formFactory $this->formFactory;
  138.         /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
  139.         $userManager $this->manager;
  140.         /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
  141.         $dispatcher $this->event;
  142.         $user $userManager->findUserByConfirmationToken($token);
  143.         if (null === $user) {
  144.             throw new NotFoundHttpException(sprintf('The user with "confirmation token" does not exist for value "%s"'$token));
  145.         }
  146.         $event = new GetResponseUserEvent($user$request);
  147.         $dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_INITIALIZE$event);
  148.         if (null !== $event->getResponse()) {
  149.             return $event->getResponse();
  150.         }
  151.         $form $formFactory->createForm();
  152.         $form->setData($user);
  153.         $form->handleRequest($request);
  154.         if ($form->isSubmitted() && $form->isValid()) {
  155.             $event = new FormEvent($form$request);
  156.             $dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_SUCCESS$event);
  157.             $userManager->updateUser($user);
  158.             if (null === $response $event->getResponse()) {
  159.                 $url $this->generateUrl('app_login');
  160.                 $response = new RedirectResponse($url);
  161.             }
  162.             $dispatcher->dispatch(
  163.                 FOSUserEvents::RESETTING_RESET_COMPLETED,
  164.                 new FilterUserResponseEvent($user$request$response)
  165.             );
  166.             return $response;
  167.         }
  168.         return $this->render('resetting/reset.html.twig', array(
  169.             'token' => $token,
  170.             'form' => $form->createView()
  171.         ));
  172.     }
  173. }