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.         $token $this->tokenStorage->getToken();
  39.         $location $this->location;
  40.         $user_data $location->getUserData();
  41.         $request_url $user_data['request_url'];
  42.         $firewall $_SERVER['FIREWALL'];
  43.         if(!is_null($token)) {
  44.             $user $token->getUser();
  45.             $last_login $user->getLastLogin();
  46.             $this_time = (new \DateTime());
  47.             if(
  48.                 (
  49.                 $last_login and
  50.                 $last_login < (clone $this_time)->modify('-20 minutes')
  51.                 ) and
  52.                 $request_url !== '/security'
  53.             ){
  54.                 if (isset($_SESSION))
  55.                     session_destroy();
  56.                 $event->setController(function () {
  57.                     return new RedirectResponse('/');
  58.                 });
  59.             }
  60.             if($firewall === 'true') {
  61.                 $user_ip $user_data['ip'];
  62.                 $logout true;
  63.                 $white_list $this->em->getRepository(WhiteList::class)->findBy([
  64.                     'ip' => $user_ip
  65.                 ]);
  66.                 if ($white_list) {
  67.                     foreach ($white_list as $white) {
  68.                         $status_user $white->checkUser($user);
  69.                         if ($status_user) {
  70.                             $logout false;
  71.                             break;
  72.                         }
  73.                     }
  74.                 }
  75.                 if ($logout) {
  76.                     if (isset($_SESSION))
  77.                         session_destroy();
  78.                     $event->setController(function () {
  79.                         return new RedirectResponse('/logout/no/valid/ip');
  80.                     });
  81.                 }
  82.             }
  83.         }
  84.     }
  85. }