src/EventListener/AuthenticationListener.php line 36

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 App\EventListener;
  11. use FOS\UserBundle\Event\FilterUserResponseEvent;
  12. use FOS\UserBundle\Event\UserEvent;
  13. use FOS\UserBundle\FOSUserEvents;
  14. use FOS\UserBundle\Security\LoginManagerInterface;
  15. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\Security\Core\Exception\AccountStatusException;
  18. class AuthenticationListener implements EventSubscriberInterface
  19. {
  20.     public function __construct(private readonly LoginManagerInterface $loginManager, private readonly EventDispatcherInterface $eventDispatcher, private readonly string $firewallName)
  21.     {
  22.     }
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [
  26.             FOSUserEvents::REGISTRATION_COMPLETED => 'authenticate',
  27.             FOSUserEvents::RESETTING_RESET_COMPLETED => 'authenticate',
  28.         ];
  29.     }
  30.     public function authenticate(FilterUserResponseEvent $event): void
  31.     {
  32.         if (!$event->getUser()->isEnabled()) {
  33.             return;
  34.         }
  35.         try {
  36.             $this->loginManager->loginUser($this->firewallName$event->getUser(), $event->getResponse());
  37.             $this->eventDispatcher->dispatch(new UserEvent($event->getUser(), $event->getRequest()), FOSUserEvents::SECURITY_IMPLICIT_LOGIN);
  38.         } catch (AccountStatusException) {
  39.             // We simply do not authenticate users which do not pass the user
  40.             // checker (not enabled, expired, etc.).
  41.         }
  42.     }
  43. }