<?php
namespace App\EventListener;
use App\Entity\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class KernelRequestListener implements EventSubscriberInterface
{
private $sam;
public function __construct(TokenStorageInterface $sam)
{
$this->sam = $sam;
}
public function onKernelRequest(RequestEvent $event)
{
/**
* @var User $user
*/
$token = $this->sam->getToken();
$user = $token?->getUser();
if( is_a($user, User::class) && $user->getDeactivatedAt() && $event->getRequest()->getRequestUri() != "/"){
$response = new RedirectResponse("/");
$event->setResponse($response);
}
}
public static function getSubscribedEvents()
{
return [
RequestEvent::class => 'onKernelRequest'
];
}
}