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

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