<?php
namespace App\EventSubscriber;
use App\Entity\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Security;
class NotAccessDeletedUserSubscriber implements EventSubscriberInterface
{
const AFECT_ROUTE="company.request.panding-department";
/**
*
*/
public function __construct(private Security $security, private RouterInterface $router) {
$this->security = $security;
}
public function onKernelController(ControllerEvent $event)
{
//return;
/**
*
*/
$user = $this->security->getUser();
$request = $event->getRequest();
//dd($request->attributes->get('_route'));
if
(
$user &&
$user instanceof User &&
$user->getDeletedAt() &&
$request->attributes->get('_route') != self::AFECT_ROUTE
) {
$event->setController(
function ()
{
return new RedirectResponse($this->router->generate(self::AFECT_ROUTE));
}
);
}
}
public static function getSubscribedEvents()
{
return [
'kernel.controller' => 'onKernelController',
];
}
}