vendor/friendsofsymfony/user-bundle/src/EventListener/AuthenticationListener.php line 65

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\CompatibilityUtil;
  12. use FOS\UserBundle\Event\FilterUserResponseEvent;
  13. use FOS\UserBundle\Event\UserEvent;
  14. use FOS\UserBundle\FOSUserEvents;
  15. use FOS\UserBundle\Security\LoginManagerInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\Security\Core\Exception\AccountStatusException;
  18. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  19. /**
  20. * @internal
  21. *
  22. * @final
  23. */
  24. class AuthenticationListener implements EventSubscriberInterface
  25. {
  26. /**
  27. * @var LoginManagerInterface
  28. */
  29. private $loginManager;
  30. /**
  31. * @var string
  32. */
  33. private $firewallName;
  34. /**
  35. * AuthenticationListener constructor.
  36. *
  37. * @param string $firewallName
  38. */
  39. public function __construct(LoginManagerInterface $loginManager, $firewallName)
  40. {
  41. $this->loginManager = $loginManager;
  42. $this->firewallName = $firewallName;
  43. }
  44. public static function getSubscribedEvents(): array
  45. {
  46. return [
  47. FOSUserEvents::REGISTRATION_COMPLETED => 'authenticate',
  48. FOSUserEvents::REGISTRATION_CONFIRMED => 'authenticate',
  49. FOSUserEvents::RESETTING_RESET_COMPLETED => 'authenticate',
  50. ];
  51. }
  52. /**
  53. * @param string $eventName
  54. *
  55. * @return void
  56. */
  57. public function authenticate(FilterUserResponseEvent $event, $eventName, EventDispatcherInterface $eventDispatcher)
  58. {
  59. $eventDispatcher = CompatibilityUtil::upgradeEventDispatcher($eventDispatcher);
  60. try {
  61. $this->loginManager->logInUser($this->firewallName, $event->getUser(), $event->getResponse());
  62. $eventDispatcher->dispatch(new UserEvent($event->getUser(), $event->getRequest()), FOSUserEvents::SECURITY_IMPLICIT_LOGIN);
  63. } catch (AccountStatusException $ex) {
  64. // We simply do not authenticate users which do not pass the user
  65. // checker (not enabled, expired, etc.).
  66. }
  67. }
  68. }