<?php
namespace App\Subscribers;
use App\Entity\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
/**
* Gestion des comptes bloqués
* Une fois q'un compte est désactivé on déconnect automatiquement l'utilisateur connecté
*/
class CheckUserLockedDisconnectSubscriber implements EventSubscriberInterface {
/**
* @var RouterInterface
*/
private $router;
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage, RouterInterface $router) {
$this->tokenStorage = $tokenStorage;
$this->router = $router;
}
public static function getSubscribedEvents() {
return [
KernelEvents::CONTROLLER => 'disconnectEvent',
];
}
public function disconnectEvent(ControllerEvent $event) {
$token = $this->tokenStorage->getToken();
if (!empty($token)) {
$user = $token->getUser();
if ($user instanceof User) {
if ($user->isLocked()) {
$event->setController(function () {
return new RedirectResponse($this->router->generate('app_logout'));
});
}
}
}
}
}