src/Domain/User/EventSubscriber/SecuritySubscriber.php line 27

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