<?php
namespace FMT\Domain\Listener;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
class RequestListener
{
private Security $security;
private UrlGeneratorInterface $router;
public function __construct(Security $security, UrlGeneratorInterface $router)
{
$this->security = $security;
$this->router = $router;
}
public function onKernelResponse(ResponseEvent $event)
{
$response = $event->getResponse();
$response->headers->set("Vary", "Cookie");
}
public function onKernelRequest(RequestEvent $event)
{
$user = $this->security->getUser();
if ($user === null) {
return;
}
if ($user->isUserBlocked()) {
$url = $this->router->generate('logout_manual');
$event->setResponse(new RedirectResponse($url));
}
}
}