. */ declare(strict_types=1); namespace App\EventSubscriber\UserSystem; use App\Entity\UserSystem\User; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Security\Core\Security; /** * This subscriber is used to log out a disabled user, as soon as he to do a request. * It is not possible for him to login again, afterward. */ final class LogoutDisabledUserSubscriber implements EventSubscriberInterface { private \Symfony\Bundle\SecurityBundle\Security $security; private UrlGeneratorInterface $urlGenerator; public function __construct(\Symfony\Bundle\SecurityBundle\Security $security, UrlGeneratorInterface $urlGenerator) { $this->security = $security; $this->urlGenerator = $urlGenerator; } public function onRequest(RequestEvent $event): void { $user = $this->security->getUser(); if ($user instanceof User && $user->isDisabled()) { //Redirect to log in $response = new RedirectResponse($this->urlGenerator->generate('logout')); $event->setResponse($response); } } /** * Returns an array of event names this subscriber wants to listen to. * * The array keys are event names and the value can be: * * * The method name to call (priority defaults to 0) * * An array composed of the method name to call and the priority * * An array of arrays composed of the method names to call and respective * priorities, or 0 if unset * * For instance: * * * ['eventName' => 'methodName'] * * ['eventName' => ['methodName', $priority]] * * ['eventName' => [['methodName1', $priority], ['methodName2']]] * * @return array The event names to listen to */ public static function getSubscribedEvents(): array { return [KernelEvents::REQUEST => 'onRequest']; } }