<?php
namespace App\Domain\User\EventSubscriber;
use App\Domain\User\Entity\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
class SecuritySubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly Security $security,
private readonly UrlGeneratorInterface $router
) {
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
public function onKernelRequest($event)
{
$user = $this->security->getUser();
if (!$user instanceof User) {
return;
}
if (User::STATUS_ACTIVE !== $user->getStatus()) {
$response = new RedirectResponse($this->router->generate('app_logout'));
$event->setResponse($response);
}
}
}