src/EventSubscriber/RequestSubscriber.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\WhiteList;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  8. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. class RequestSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(EntityManagerInterface $entityManagerTokenStorageInterface $tokenStorage)
  14.     {
  15.         $this->em $entityManager;
  16.         $this->tokenStorage $tokenStorage;
  17.     }
  18.     public static function getSubscribedEvents()
  19.     {
  20.         // return the subscribed events, their methods and priorities
  21.         return [
  22.             KernelEvents::CONTROLLER => [
  23.                 ['logException'0],
  24.             ],
  25.         ];
  26.     }
  27.     /**
  28.      * @param ControllerEvent $event
  29.      */
  30.     public function logException($event)
  31.     {
  32.         $token $this->tokenStorage->getToken();
  33.         if($token) {
  34.             $this_user $token->getUser();
  35.             if (
  36.                 $this_user != 'anon.' and
  37.                 $_SERVER['FIREWALL'] == 'true'
  38.             ) {
  39.                 $user_ip $_SERVER['REMOTE_ADDR'];
  40.                 $white_list $this->em->getRepository(WhiteList::class)->findBy(['ip' => $user_ip]);
  41.                 if (!$white_list) {
  42.                     if(isset($_SESSION))
  43.                         session_destroy();
  44.                     $event->setController(function () {
  45.                         return new RedirectResponse('/logout/no/valid/ip');
  46.                     });
  47.                 }
  48.             }
  49.         }
  50.     }
  51. }