src/EventSubscriber/RedirectToLogoutSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Applicants;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\Routing\RouterInterface;
  9. use Symfony\Component\Security\Core\Security;
  10. class RedirectToLogoutSubscriber implements EventSubscriberInterface
  11. {
  12.     public function __construct(private Security $security, private RouterInterface $router){}
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             KernelEvents::REQUEST => 'onKernelRequest',
  17.         ];
  18.     }
  19.     public function onKernelRequest(RequestEvent $event): void
  20.     {
  21.         $user$this->security->getUser();
  22.         if (!$user instanceof Applicants) {
  23.             return;
  24.         }
  25.         if (!$event->isMainRequest() ) {
  26.             return;
  27.         }
  28.         if(!$user->getIsEnabled()){
  29.             $event->setResponse(new RedirectResponse($this->router->generate('applicant_logout')));
  30.         }
  31.     }
  32. }