src/Subscriber/EveryRequestSubscriber.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber;
  3. use App\Entity\User;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Routing\RouterInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. class EveryRequestSubscriber implements EventSubscriberInterface
  13. {
  14.     private $tokenStorage;
  15.     private $router;
  16.     private $session;
  17.     private $tranlaor;
  18.     public function __construct(TokenStorageInterface $tokenStorageRouterInterface $routerSessionInterface $sessionTranslatorInterface $translator)
  19.     {
  20.         $this->tokenStorage $tokenStorage;
  21.         $this->router $router;
  22.         $this->session $session;
  23.         $this->tranlaor $translator;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         // TODO: Implement getSubscribedEvents() method.
  28.         return [
  29.             KernelEvents::REQUEST => [
  30.                 'checkUser'
  31.             ]
  32.         ];
  33.     }
  34.     public function checkUser(RequestEvent $event)
  35.     {
  36.         if (!$event->isMainRequest())
  37.         {
  38.             return;
  39.         }
  40.         if ($this->tokenStorage->getToken())
  41.         {
  42.             $user $this->tokenStorage->getToken()->getUser();
  43.             if ($user instanceof User)
  44.             {
  45.                 if ($user->getUserStatus()->getId() != || $user->getActive() != 1)
  46.                 {
  47.                     $this->tokenStorage->setToken(null);
  48.                     $route 'login';
  49.                     $url $this->router->generate($route);
  50.                     $response = new RedirectResponse($url);
  51.                     $this->session->getFlashBag()->add('error'$this->tranlaor->trans('accountNotActive'));
  52.                     $event->setResponse($response);
  53.                 }
  54.             }
  55.         }
  56.     }
  57. }