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

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. /**
  18.  * @internal
  19.  * @final
  20.  */
  21. class ResettingListener implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var UrlGeneratorInterface
  25.      */
  26.     private $router;
  27.     /**
  28.      * @var int
  29.      */
  30.     private $tokenTtl;
  31.     /**
  32.      * ResettingListener constructor.
  33.      *
  34.      * @param int $tokenTtl
  35.      */
  36.     public function __construct(UrlGeneratorInterface $router$tokenTtl)
  37.     {
  38.         $this->router $router;
  39.         $this->tokenTtl $tokenTtl;
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [
  47.             FOSUserEvents::RESETTING_RESET_INITIALIZE => 'onResettingResetInitialize',
  48.             FOSUserEvents::RESETTING_RESET_SUCCESS => 'onResettingResetSuccess',
  49.             FOSUserEvents::RESETTING_RESET_REQUEST => 'onResettingResetRequest',
  50.         ];
  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.     public function onResettingResetSuccess(FormEvent $event)
  59.     {
  60.         /** @var $user \FOS\UserBundle\Model\UserInterface */
  61.         $user $event->getForm()->getData();
  62.         $user->setConfirmationToken(null);
  63.         $user->setPasswordRequestedAt(null);
  64.         $user->setEnabled(true);
  65.     }
  66.     public function onResettingResetRequest(GetResponseUserEvent $event)
  67.     {
  68.         if (!$event->getUser()->isAccountNonLocked()) {
  69.             $event->setResponse(new RedirectResponse($this->router->generate('fos_user_resetting_request')));
  70.         }
  71.     }
  72. }