src/MultiUser/Listener/AuthenticationListener.php line 65

Open in your IDE?
  1. <?php
  2. namespace App\MultiUser\Listener;
  3. use Doctrine\Common\Util\ClassUtils;
  4. use FOS\UserBundle\Event\UserEvent;
  5. use FOS\UserBundle\FOSUserEvents;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  8. use Symfony\Component\Security\Http\Event\SwitchUserEvent;
  9. use Symfony\Component\Security\Http\SecurityEvents;
  10. use App\MultiUser\Model\UserDiscriminator;
  11. class AuthenticationListener implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var UserDiscriminator
  15.      */
  16.     protected $userDiscriminator;
  17.     /**
  18.      * @param UserDiscriminator $controllerHandler
  19.      */
  20.     public function __construct(UserDiscriminator $userDiscriminator)
  21.     {
  22.         $this->userDiscriminator $userDiscriminator;
  23.     }
  24.     /**
  25.      * @return array The event names to listen to
  26.      */
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             FOSUserEvents::SECURITY_IMPLICIT_LOGIN => 'onSecurityImplicitLogin',
  31.             SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
  32.             SecurityEvents::SWITCH_USER => 'onSecuritySwitchUser',
  33.         ];
  34.     }
  35.     protected function discriminate($user)
  36.     {
  37.         $class ClassUtils::getClass($user);
  38.         $this->userDiscriminator->setClass($classtrue);
  39.     }
  40.     /**
  41.      * @param \FOS\UserBundle\Event\UserEvent $event
  42.      */
  43.     public function onSecurityImplicitLogin(UserEvent $event)
  44.     {
  45.         $this->discriminate($event->getUser());
  46.     }
  47.     /**
  48.      * @param \Symfony\Component\Security\Http\Event\InteractiveLoginEvent $event
  49.      */
  50.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
  51.     {
  52.         $this->discriminate($event->getAuthenticationToken()->getUser());
  53.     }
  54.     /**
  55.      * @param \Symfony\Component\Security\Http\Event\SwitchUserEvent $event
  56.      */
  57.     public function onSecuritySwitchUser(SwitchUserEvent $event)
  58.     {
  59.         $this->discriminate($event->getTargetUser());
  60.     }
  61. }