<?php
namespace App\MultiUser\Listener;
use Doctrine\Common\Util\ClassUtils;
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
use Symfony\Component\Security\Http\SecurityEvents;
use App\MultiUser\Model\UserDiscriminator;
class AuthenticationListener implements EventSubscriberInterface
{
/**
* @var UserDiscriminator
*/
protected $userDiscriminator;
/**
* @param UserDiscriminator $controllerHandler
*/
public function __construct(UserDiscriminator $userDiscriminator)
{
$this->userDiscriminator = $userDiscriminator;
}
/**
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
FOSUserEvents::SECURITY_IMPLICIT_LOGIN => 'onSecurityImplicitLogin',
SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
SecurityEvents::SWITCH_USER => 'onSecuritySwitchUser',
];
}
protected function discriminate($user)
{
$class = ClassUtils::getClass($user);
$this->userDiscriminator->setClass($class, true);
}
/**
* @param \FOS\UserBundle\Event\UserEvent $event
*/
public function onSecurityImplicitLogin(UserEvent $event)
{
$this->discriminate($event->getUser());
}
/**
* @param \Symfony\Component\Security\Http\Event\InteractiveLoginEvent $event
*/
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$this->discriminate($event->getAuthenticationToken()->getUser());
}
/**
* @param \Symfony\Component\Security\Http\Event\SwitchUserEvent $event
*/
public function onSecuritySwitchUser(SwitchUserEvent $event)
{
$this->discriminate($event->getTargetUser());
}
}