src/Wam/EventSubscriber/WamLoginRedirectSubscriber.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Wam\EventSubscriber;
  3. use AppBundle\Repository\UserRepository;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\Routing\RouterInterface;
  9. /**
  10.  * Intercepts the login form submission before the security firewall processes it.
  11.  * If the submitted user has a permId, they must authenticate via /wam/login instead.
  12.  */
  13. class WamLoginRedirectSubscriber implements EventSubscriberInterface
  14. {
  15.     private const LOGIN_CHECK_PATH '/login_check';
  16.     private const USERNAME_PARAM '_username';
  17.     public function __construct(private UserRepository $userRepository, private RouterInterface $router)
  18.     {
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         // Priority 9 runs before Symfony's FirewallListener (priority 8)
  23.         return [
  24.             KernelEvents::REQUEST => ['onKernelRequest'9],
  25.         ];
  26.     }
  27.     public function onKernelRequest(RequestEvent $event): void
  28.     {
  29.         $request $event->getRequest();
  30.         if (!$event->isMasterRequest()) {
  31.             return;
  32.         }
  33.         if ($request->getPathInfo() !== self::LOGIN_CHECK_PATH) {
  34.             return;
  35.         }
  36.         if (!$request->isMethod('POST')) {
  37.             return;
  38.         }
  39.         $username $request->request->get(self::USERNAME_PARAM);
  40.         
  41.         if (empty($username)) {
  42.             return;
  43.         }
  44.         $user $this->userRepository->findOneBy(['email' => $username]);
  45.         if (null === $user || (empty($user->getPermId()) && empty($user->getLawFirm()?->getEcmId()))) {
  46.             return;
  47.         }
  48.         $event->setResponse(new RedirectResponse($this->router->generate('wam_login')));
  49.     }
  50. }