vendor/friendsofsymfony/user-bundle/Controller/ResettingController.php line 69

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSUserBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace FOS\UserBundle\Controller;
  11. use FOS\UserBundle\Event\FilterUserResponseEvent;
  12. use FOS\UserBundle\Event\FormEvent;
  13. use FOS\UserBundle\Event\GetResponseNullableUserEvent;
  14. use FOS\UserBundle\Event\GetResponseUserEvent;
  15. use FOS\UserBundle\Form\Factory\FactoryInterface;
  16. use FOS\UserBundle\FOSUserEvents;
  17. use FOS\UserBundle\Mailer\MailerInterface;
  18. use FOS\UserBundle\Model\UserManagerInterface;
  19. use FOS\UserBundle\Util\TokenGeneratorInterface;
  20. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  21. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  22. use Symfony\Component\HttpFoundation\RedirectResponse;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\Response;
  25. /**
  26.  * Controller managing the resetting of the password.
  27.  *
  28.  * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  29.  * @author Christophe Coevoet <stof@notk.org>
  30.  */
  31. class ResettingController extends Controller
  32. {
  33.     private $eventDispatcher;
  34.     private $formFactory;
  35.     private $userManager;
  36.     private $tokenGenerator;
  37.     private $mailer;
  38.     /**
  39.      * @var int
  40.      */
  41.     private $retryTtl;
  42.     /**
  43.      * @param EventDispatcherInterface $eventDispatcher
  44.      * @param FactoryInterface         $formFactory
  45.      * @param UserManagerInterface     $userManager
  46.      * @param TokenGeneratorInterface  $tokenGenerator
  47.      * @param MailerInterface          $mailer
  48.      * @param int                      $retryTtl
  49.      */
  50.     public function __construct(EventDispatcherInterface $eventDispatcherFactoryInterface $formFactoryUserManagerInterface $userManagerTokenGeneratorInterface $tokenGeneratorMailerInterface $mailer$retryTtl)
  51.     {
  52.         $this->eventDispatcher $eventDispatcher;
  53.         $this->formFactory $formFactory;
  54.         $this->userManager $userManager;
  55.         $this->tokenGenerator $tokenGenerator;
  56.         $this->mailer $mailer;
  57.         $this->retryTtl $retryTtl;
  58.     }
  59.     /**
  60.      * Request reset user password: show form.
  61.      */
  62.     public function requestAction()
  63.     {
  64.         return $this->render('@FOSUser/Resetting/request.html.twig');
  65.     }
  66.     /**
  67.      * Request reset user password: submit form and send email.
  68.      *
  69.      * @param Request $request
  70.      *
  71.      * @return Response
  72.      */
  73.     public function sendEmailAction(Request $request)
  74.     {
  75.         $username $request->request->get('username');
  76.         $user $this->userManager->findUserByUsernameOrEmail($username);
  77.         $event = new GetResponseNullableUserEvent($user$request);
  78.         $this->eventDispatcher->dispatch(FOSUserEvents::RESETTING_SEND_EMAIL_INITIALIZE$event);
  79.         if (null !== $event->getResponse()) {
  80.             return $event->getResponse();
  81.         }
  82.         if (null !== $user && !$user->isPasswordRequestNonExpired($this->retryTtl)) {
  83.             $event = new GetResponseUserEvent($user$request);
  84.             $this->eventDispatcher->dispatch(FOSUserEvents::RESETTING_RESET_REQUEST$event);
  85.             if (null !== $event->getResponse()) {
  86.                 return $event->getResponse();
  87.             }
  88.             if (null === $user->getConfirmationToken()) {
  89.                 $user->setConfirmationToken($this->tokenGenerator->generateToken());
  90.             }
  91.             $event = new GetResponseUserEvent($user$request);
  92.             $this->eventDispatcher->dispatch(FOSUserEvents::RESETTING_SEND_EMAIL_CONFIRM$event);
  93.             if (null !== $event->getResponse()) {
  94.                 return $event->getResponse();
  95.             }
  96.             $this->mailer->sendResettingEmailMessage($user);
  97.             $user->setPasswordRequestedAt(new \DateTime());
  98.             $this->userManager->updateUser($user);
  99.             $event = new GetResponseUserEvent($user$request);
  100.             $this->eventDispatcher->dispatch(FOSUserEvents::RESETTING_SEND_EMAIL_COMPLETED$event);
  101.             if (null !== $event->getResponse()) {
  102.                 return $event->getResponse();
  103.             }
  104.         }
  105.         return new RedirectResponse($this->generateUrl('fos_user_resetting_check_email', array('username' => $username)));
  106.     }
  107.     /**
  108.      * Tell the user to check his email provider.
  109.      *
  110.      * @param Request $request
  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('fos_user_resetting_request'));
  120.         }
  121.         return $this->render('@FOSUser/Resetting/check_email.html.twig', array(
  122.             'tokenLifetime' => ceil($this->retryTtl 3600),
  123.         ));
  124.     }
  125.     /**
  126.      * Reset user password.
  127.      *
  128.      * @param Request $request
  129.      * @param string  $token
  130.      *
  131.      * @return Response
  132.      */
  133.     public function resetAction(Request $request$token)
  134.     {
  135.         $user $this->userManager->findUserByConfirmationToken($token);
  136.         if (null === $user) {
  137.             return new RedirectResponse($this->container->get('router')->generate('fos_user_security_login'));
  138.         }
  139.         $event = new GetResponseUserEvent($user$request);
  140.         $this->eventDispatcher->dispatch(FOSUserEvents::RESETTING_RESET_INITIALIZE$event);
  141.         if (null !== $event->getResponse()) {
  142.             return $event->getResponse();
  143.         }
  144.         $form $this->formFactory->createForm();
  145.         $form->setData($user);
  146.         $form->handleRequest($request);
  147.         if ($form->isSubmitted() && $form->isValid()) {
  148.             $event = new FormEvent($form$request);
  149.             $this->eventDispatcher->dispatch(FOSUserEvents::RESETTING_RESET_SUCCESS$event);
  150.             $this->userManager->updateUser($user);
  151.             if (null === $response $event->getResponse()) {
  152.                 $url $this->generateUrl('fos_user_profile_show');
  153.                 $response = new RedirectResponse($url);
  154.             }
  155.             $this->eventDispatcher->dispatch(
  156.                 FOSUserEvents::RESETTING_RESET_COMPLETED,
  157.                 new FilterUserResponseEvent($user$request$response)
  158.             );
  159.             return $response;
  160.         }
  161.         return $this->render('@FOSUser/Resetting/reset.html.twig', array(
  162.             'token' => $token,
  163.             'form' => $form->createView(),
  164.         ));
  165.     }
  166. }