src/EventListener/RequestAuthenticationSubscriber.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Location;
  4. use App\Entity\User;
  5. use App\Service\UserActivityService;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Psr\Log\LoggerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Symfony\Component\HttpKernel\Event\RequestEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use Symfony\Component\Security\Core\Security;
  14. class RequestAuthenticationSubscriber implements EventSubscriberInterface
  15. {
  16. /** @var EntityManagerInterface */
  17. private EntityManagerInterface $entityManager;
  18. /** @var SessionInterface */
  19. private SessionInterface $session;
  20. /** @var TokenStorageInterface */
  21. private TokenStorageInterface $tokenStorage;
  22. /** @var UserActivityService */
  23. private UserActivityService $userActivityService;
  24. private LoggerInterface $logger;
  25. private Security $security;
  26. public function __construct(
  27. EntityManagerInterface $entityManager,
  28. TokenStorageInterface $tokenStorage,
  29. LoggerInterface $logger,
  30. UserActivityService $userActivityService,
  31. Security $security
  32. )
  33. {
  34. $this->entityManager = $entityManager;
  35. $this->tokenStorage = $tokenStorage;
  36. $this->logger = $logger;
  37. $this->userActivityService = $userActivityService;
  38. $this->security = $security;
  39. }
  40. public function onKernelRequest(RequestEvent $event): void
  41. {
  42. $logout = false;
  43. $user = $this->security->getUser();
  44. // get url
  45. $request = $event->getRequest();
  46. $this->session = $request->getSession();
  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. }