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