src/EventSubscriber/RequestSubscriber.php line 43

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