src/EventListener/RequestListener.php line 44

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Enum\User\UserRestrictionsEnum;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use App\Entity\User;
  7. use App\Entity\Location;
  8. use Symfony\Component\Routing\RouterInterface;
  9. use Symfony\Component\Security\Core\Security;
  10. use App\Service\DatabaseService;
  11. use App\Service\UserAccessService;
  12. class RequestListener
  13. {
  14. /**
  15. * @var RouterInterface
  16. */
  17. private $router;
  18. /**
  19. * @var Security
  20. */
  21. private $security;
  22. /**
  23. * @var DatabaseService
  24. */
  25. private $databaseService;
  26. /**
  27. * @var UserAccessService
  28. */
  29. private $userAccessService;
  30. public function __construct(RouterInterface $router, Security $security, DatabaseService $databaseService, UserAccessService $userAccessService) {
  31. $this->router = $router;
  32. $this->security = $security;
  33. $this->databaseService = $databaseService;
  34. $this->userAccessService = $userAccessService;
  35. }
  36. public function onKernelRequest(RequestEvent $event): void
  37. {
  38. $request = $event->getRequest();
  39. $path = $request->getPathInfo();
  40. $user = $this->security->getUser();
  41. if (!$user instanceof User) {
  42. return;
  43. }
  44. if ($path === "/login" && $request->getSession()->isStarted()) {
  45. /** @var User $user */
  46. $user = $this->databaseService->getManager()->getRepository(User::class)->findOneBy(["username" =>$user->getUsername()]);
  47. $activeLocations = $user->getActiveLocations();
  48. if($activeLocations->isEmpty()) {
  49. if ($user->isHatchAdministrator()) {
  50. $event->setResponse(new RedirectResponse($this->router->generate('admin_index')));
  51. return;
  52. } else {
  53. $event->getRequest()->getSession()->getFlashBag()->add('error', "No active locations for login");
  54. return;
  55. }
  56. }
  57. $defaultLocation = $user->getDefaultLocation();
  58. if ( $defaultLocation instanceof Location && $activeLocations->contains($defaultLocation) ) {
  59. $event->setResponse(new RedirectResponse($this->router->generate('client_index', ['locationId' =>$defaultLocation->getId()])));
  60. return;
  61. }
  62. if($user->isHatchAdministrator()) {
  63. $event->setResponse(new RedirectResponse($this->router->generate('admin_index')));
  64. return;
  65. } elseif ($activeLocations->count() === 1) {
  66. $event->setResponse(new RedirectResponse($this->router->generate('client_index', ['locationId' =>$activeLocations->first()->getId()])));
  67. return;
  68. } else {
  69. $event->setResponse(new RedirectResponse($this->router->generate('location_login_select')));
  70. return;
  71. }
  72. }
  73. if (str_starts_with($path, '/admin')) {
  74. if ($user->isHatchAdministrator() && $user->hasRestrictions()) {
  75. $restrictionsMap = UserRestrictionsEnum::getRestrictionsMap();
  76. $userRestrictions = $user->getRestrictions();
  77. $allowedPaths = [];
  78. $redirectRoute = 'app_login';
  79. foreach ($userRestrictions as $restriction) {
  80. if (isset($restrictionsMap[$restriction])) {
  81. $allowedPaths = array_merge($allowedPaths, $restrictionsMap[$restriction]['paths']);
  82. if ($redirectRoute === 'app_login') {
  83. $redirectRoute = $restrictionsMap[$restriction]['redirect_route'];
  84. }
  85. }
  86. }
  87. $isAllowed = false;
  88. foreach ($allowedPaths as $allowedPath) {
  89. if (str_starts_with($path, $allowedPath)) {
  90. $isAllowed = true;
  91. break;
  92. }
  93. }
  94. if (!$isAllowed) {
  95. $event->setResponse(new RedirectResponse($this->router->generate($redirectRoute)));
  96. return;
  97. }
  98. } elseif (!$user->isHatchAdministrator()) {
  99. $event->setResponse(new RedirectResponse($this->router->generate('app_login')));
  100. return;
  101. }
  102. }
  103. if (str_starts_with($path, '/client')) {
  104. $locationId = $request->attributes->get('locationId');
  105. if ($locationId) {
  106. $location = $this->databaseService->getManager()->getRepository(Location::class)->find($locationId);
  107. if ($location instanceof Location && $location->getCompany() && $location->getCompany()->isBillingHold()) {
  108. $firstValidLocation = $this->userAccessService->getFirstActiveLocationInFirstNonBillingHoldCompany($user);
  109. $companyName = $location->getCompany()->getName();
  110. $request->getSession()->getFlashBag()->add(
  111. 'error',
  112. sprintf(
  113. 'Payment is Past Due for %s. Please submit payment to regain access. For any questions, please contact ComplianceBilling@KipuHealth.com.',
  114. $companyName
  115. )
  116. );
  117. if ($firstValidLocation) {
  118. $event->setResponse(
  119. new RedirectResponse(
  120. $this->router->generate('client_index', ['locationId' => $firstValidLocation->getId()])
  121. )
  122. );
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }