src/EventSubscriber/NotAccessDeletedUserSubscriber.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\User;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\Routing\RouterInterface;
  8. use Symfony\Component\Security\Core\Security;
  9. class NotAccessDeletedUserSubscriber implements EventSubscriberInterface
  10. {
  11.     const AFECT_ROUTE="company.request.panding-department";
  12.     /**
  13.      * 
  14.      */
  15.     public function __construct(private Security $security, private RouterInterface $router) {
  16.         $this->security $security;
  17.     }
  18.     public function onKernelController(ControllerEvent $event)
  19.     {
  20.         //return;
  21.         /**
  22.          * 
  23.          */
  24.         $user $this->security->getUser();
  25.         $request $event->getRequest();
  26.         //dd($request->attributes->get('_route'));
  27.         if 
  28.         (
  29.             $user && 
  30.             $user instanceof User && 
  31.             $user->getDeletedAt() &&
  32.             $request->attributes->get('_route') != self::AFECT_ROUTE
  33.         ) {
  34.             $event->setController(
  35.                 function ()
  36.                 {
  37.                     return new RedirectResponse($this->router->generate(self::AFECT_ROUTE));
  38.                 }
  39.             );
  40.         }
  41.     }
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return [
  45.             'kernel.controller' => 'onKernelController',
  46.         ];
  47.     }
  48. }