src/EventListener/RequestAuthenticationSubscriber.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Location;
  4. use App\Entity\User;
  5. use App\Entity\UserActivity;
  6. use App\Service\UserActivityService;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  11. use Symfony\Component\HttpKernel\Event\RequestEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use Symfony\Component\Security\Core\Security;
  15. class RequestAuthenticationSubscriber implements EventSubscriberInterface
  16. {
  17.     /** @var EntityManagerInterface */
  18.     private $entityManager;
  19.     /** @var SessionInterface */
  20.     private $session;
  21.     /** @var TokenStorageInterface */
  22.     private $tokenStorage;
  23.     /** @var UserActivityService */
  24.     private $userActivityService;
  25.     public function __construct(
  26.         EntityManagerInterface $entityManager,
  27.         SessionInterface $session,
  28.         TokenStorageInterface $tokenStorage,
  29.         LoggerInterface $logger,
  30.         UserActivityService $userActivityService,
  31.         Security $security
  32.     )
  33.     {
  34.         $this->entityManager $entityManager;
  35.         $this->session $session;
  36.         $this->tokenStorage $tokenStorage;
  37.         $this->logger $logger;
  38.         $this->userActivityService $userActivityService;
  39.         $this->security $security;
  40.     }
  41.     public function onKernelRequest(RequestEvent $event): void
  42.     {
  43.         $logout false;
  44.         $user $this->security->getUser();
  45.         // get url
  46.         $request $event->getRequest();
  47.         $url $request->getRequestUri();
  48.         if(!$this->tokenStorage->getToken()){
  49.             $url str_replace('/login',''$url);
  50.         }
  51.         if (strpos($url'/login') !== false &&
  52.             $this->tokenStorage->getToken() &&
  53.             $this->tokenStorage->getToken()->getUser() instanceof User) {
  54.             $url str_replace('/login',''$url);
  55.         }
  56.         // check if client
  57.         if(
  58.             strpos($url'/client/') !== false
  59.             && strpos($url'/api/') === false
  60.             && strpos($url'/relational/') === false
  61.             && strpos($url'/client/toolbar/') === false
  62.             && strpos($url'/client/cform/') === false
  63.             && strpos($url'/client/hr-document/') === false
  64.             && strpos($url'/client/employee-survey/') === false
  65.             && strpos($url'/client/location-select') === false
  66.         ){
  67.             // break apart into pieces
  68.             if($locationId $request->get('locationId')) {
  69.                 /** @var Location $location */
  70.                 $location $this->entityManager->getRepository(Location::class)->find($locationId);
  71.                 //Track login when default controller is bypassed by external link
  72.                 if(!$this->session->get('loginIsTracked') && $this->tokenStorage->getToken() && !strpos($url'select_location=1')) {
  73.                     $this->userActivityService->logUserActivity(LoggableListener::ACTION_LOGIN$user,  $locationId$user);
  74.                     $this->session->set('loginIsTracked'true);
  75.                 }
  76.                 if( !$location || !$location->isActive()
  77.                     || !$location->getCompany() || !$location->getCompany()->isActive()
  78.                     || (
  79.                         $user
  80.                         && (
  81.                             !$user->isHatchAdministrator()
  82.                             && !$user->getActiveLocations()->contains($location)
  83.                         )
  84.                     )
  85.                 ){
  86.                     $logout true;
  87.                 }
  88.                 if($logout) {
  89.                     $event->getRequest()->getSession()->getFlashBag()->add('error'"Invalid location for login");
  90.                     $this->logger->error('Invalid URL: ' $url);
  91.                     // clear session - logout
  92.                     $this->session->clear();
  93.                     $this->tokenStorage->setToken(null);
  94.                     $this->session->invalidate();
  95.                     header("Location: /");
  96.                     exit;
  97.                 }
  98.             }
  99.         }
  100.         if ($url === '/admin/' && !$this->session->get('loginIsTracked')) {
  101.             $this->userActivityService->logAdminActivity(LoggableListener::ACTION_LOGIN$this->security->getUser(), $this->security->getUser());
  102.             $this->session->set('loginIsTracked'true);
  103.         }
  104.     }
  105.     /**
  106.      * @inheritDoc
  107.      */
  108.     public static function getSubscribedEvents(): array
  109.     {
  110.         return array(
  111.             KernelEvents::REQUEST => 'onKernelRequest',
  112.         );
  113.     }
  114. }