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

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.     private $eventDispatcher;
  33.     private $formFactory;
  34.     private $userManager;
  35.     private $tokenGenerator;
  36.     private $mailer;
  37.     /**
  38.      * @var int
  39.      */
  40.     private $retryTtl;
  41.     /**
  42.      * @param EventDispatcherInterface $eventDispatcher
  43.      * @param FactoryInterface         $formFactory
  44.      * @param UserManagerInterface     $userManager
  45.      * @param TokenGeneratorInterface  $tokenGenerator
  46.      * @param MailerInterface          $mailer
  47.      * @param int                      $retryTtl
  48.      */
  49.     public function __construct(EventDispatcherInterface $eventDispatcherFactoryInterface $formFactoryUserManagerInterface $userManagerTokenGeneratorInterface $tokenGeneratorMailerInterface $mailer$retryTtl) {
  50.         $this->eventDispatcher $eventDispatcher;
  51.         $this->formFactory $formFactory;
  52.         $this->userManager $userManager;
  53.         $this->tokenGenerator $tokenGenerator;
  54.         $this->mailer $mailer;
  55.         $this->retryTtl $retryTtl;
  56.     }
  57.     /**
  58.      * Request reset user password: show form.
  59.      */
  60.     public function requestAction() {
  61.         return $this->render('@FOSUser/Resetting/request.html.twig');
  62.     }
  63.     /**
  64.      * Request reset user password: submit form and send email.
  65.      *
  66.      * @param Request $request
  67.      *
  68.      * @return Response
  69.      */
  70.     public function sendEmailAction(Request $request) {
  71.         $username $request->request->get('username');
  72.         $user $this->userManager->findUserByUsernameOrEmail($username);
  73.         $event = new GetResponseNullableUserEvent($user$request);
  74.         $this->eventDispatcher->dispatch(FOSUserEvents::RESETTING_SEND_EMAIL_INITIALIZE$event);
  75.         if (null !== $event->getResponse()) {
  76.             return $event->getResponse();
  77.         }
  78.         if (null !== $user && !$user->isPasswordRequestNonExpired($this->retryTtl)) {
  79.             $event = new GetResponseUserEvent($user$request);
  80.             $this->eventDispatcher->dispatch(FOSUserEvents::RESETTING_RESET_REQUEST$event);
  81.             if (null !== $event->getResponse()) {
  82.                 return $event->getResponse();
  83.             }
  84.             if (null === $user->getConfirmationToken()) {
  85.                 $user->setConfirmationToken($this->tokenGenerator->generateToken());
  86.             }
  87.             $event = new GetResponseUserEvent($user$request);
  88.             $this->eventDispatcher->dispatch(FOSUserEvents::RESETTING_SEND_EMAIL_CONFIRM$event);
  89.             if (null !== $event->getResponse()) {
  90.                 return $event->getResponse();
  91.             }
  92.             $this->mailer->sendResettingEmailMessage($user);
  93.             $user->setPasswordRequestedAt(new \DateTime());
  94.             $this->userManager->updateUser($user);
  95.             $event = new GetResponseUserEvent($user$request);
  96.             $this->eventDispatcher->dispatch(FOSUserEvents::RESETTING_SEND_EMAIL_COMPLETED$event);
  97.             if (null !== $event->getResponse()) {
  98.                 return $event->getResponse();
  99.             }
  100.         }
  101.         return new RedirectResponse($this->generateUrl('fos_user_resetting_check_email', array('username' => $username)));
  102.     }
  103.     /**
  104.      * Tell the user to check his email provider.
  105.      *
  106.      * @param Request $request
  107.      *
  108.      * @return Response
  109.      */
  110.     public function checkEmailAction(Request $request) {
  111.         $username $request->query->get('username');
  112.         if (empty($username)) {
  113.             // the user does not come from the sendEmail action
  114.             return new RedirectResponse($this->generateUrl('fos_user_resetting_request'));
  115.         }
  116.         return $this->render('@FOSUser/Resetting/check_email.html.twig', array(
  117.                     'tokenLifetime' => ceil($this->retryTtl 3600),
  118.         ));
  119.     }
  120.     /**
  121.      * Reset user password.
  122.      *
  123.      * @param Request $request
  124.      * @param string  $token
  125.      *
  126.      * @return Response
  127.      */
  128.     public function resetAction(Request $request$token) {
  129.         $user $this->userManager->findUserByConfirmationToken($token);
  130.         if (null === $user) {
  131.             return new RedirectResponse($this->container->get('router')->generate('fos_user_security_login'));
  132.         }
  133.         $event = new GetResponseUserEvent($user$request);
  134.         $this->eventDispatcher->dispatch(FOSUserEvents::RESETTING_RESET_INITIALIZE$event);
  135.         if (null !== $event->getResponse()) {
  136.             return $event->getResponse();
  137.         }
  138.         $form $this->formFactory->createForm();
  139.         $form->setData($user);
  140.         $form->handleRequest($request);
  141.         if ($form->isSubmitted() && $form->isValid()) {
  142.             $event = new FormEvent($form$request);
  143.             $this->eventDispatcher->dispatch(FOSUserEvents::RESETTING_RESET_SUCCESS$event);
  144.             $this->userManager->updateUser($user);
  145.             if (null === $response $event->getResponse()) {
  146.                 $url $this->generateUrl('fos_user_profile_show');
  147.                 $response = new RedirectResponse($url);
  148.             }
  149.             $this->eventDispatcher->dispatch(
  150.                     FOSUserEvents::RESETTING_RESET_COMPLETED, new FilterUserResponseEvent($user$request$response)
  151.             );
  152.             return $response;
  153.         }
  154.         return $this->render('@FOSUser/Resetting/reset.html.twig', array(
  155.                     'token' => $token,
  156.                     'form' => $form->createView(),
  157.         ));
  158.     }
  159. }