vendor/friendsofsymfony/user-bundle/EventListener/ResettingListener.php line 70

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\EventListener;
  11. use FOS\UserBundle\Event\FormEvent;
  12. use FOS\UserBundle\Event\GetResponseUserEvent;
  13. use FOS\UserBundle\FOSUserEvents;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  17. class ResettingListener implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var UrlGeneratorInterface
  21.      */
  22.     private $router;
  23.     /**
  24.      * @var int
  25.      */
  26.     private $tokenTtl;
  27.     /**
  28.      * ResettingListener constructor.
  29.      *
  30.      * @param UrlGeneratorInterface $router
  31.      * @param int                   $tokenTtl
  32.      */
  33.     public function __construct(UrlGeneratorInterface $router$tokenTtl)
  34.     {
  35.         $this->router $router;
  36.         $this->tokenTtl $tokenTtl;
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return array(
  44.             FOSUserEvents::RESETTING_RESET_INITIALIZE => 'onResettingResetInitialize',
  45.             FOSUserEvents::RESETTING_RESET_SUCCESS => 'onResettingResetSuccess',
  46.             FOSUserEvents::RESETTING_RESET_REQUEST => 'onResettingResetRequest',
  47.         );
  48.     }
  49.     /**
  50.      * @param GetResponseUserEvent $event
  51.      */
  52.     public function onResettingResetInitialize(GetResponseUserEvent $event)
  53.     {
  54.         if (!$event->getUser()->isPasswordRequestNonExpired($this->tokenTtl)) {
  55.             $event->setResponse(new RedirectResponse($this->router->generate('fos_user_resetting_request')));
  56.         }
  57.     }
  58.     /**
  59.      * @param FormEvent $event
  60.      */
  61.     public function onResettingResetSuccess(FormEvent $event)
  62.     {
  63.         /** @var $user \FOS\UserBundle\Model\UserInterface */
  64.         $user $event->getForm()->getData();
  65.         $user->setConfirmationToken(null);
  66.         $user->setPasswordRequestedAt(null);
  67.         $user->setEnabled(true);
  68.     }
  69.     /**
  70.      * @param GetResponseUserEvent $event
  71.      */
  72.     public function onResettingResetRequest(GetResponseUserEvent $event)
  73.     {
  74.         if (!$event->getUser()->isAccountNonLocked()) {
  75.             $event->setResponse(new RedirectResponse($this->router->generate('fos_user_resetting_request')));
  76.         }
  77.     }
  78. }