<?php
namespace App\Wam\EventSubscriber;
use AppBundle\Repository\UserRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouterInterface;
/**
* Intercepts the login form submission before the security firewall processes it.
* If the submitted user has a permId, they must authenticate via /wam/login instead.
*/
class WamLoginRedirectSubscriber implements EventSubscriberInterface
{
private const LOGIN_CHECK_PATH = '/login_check';
private const USERNAME_PARAM = '_username';
public function __construct(private UserRepository $userRepository, private RouterInterface $router)
{
}
public static function getSubscribedEvents(): array
{
// Priority 9 runs before Symfony's FirewallListener (priority 8)
return [
KernelEvents::REQUEST => ['onKernelRequest', 9],
];
}
public function onKernelRequest(RequestEvent $event): void
{
$request = $event->getRequest();
if (!$event->isMasterRequest()) {
return;
}
if ($request->getPathInfo() !== self::LOGIN_CHECK_PATH) {
return;
}
if (!$request->isMethod('POST')) {
return;
}
$username = $request->request->get(self::USERNAME_PARAM);
if (empty($username)) {
return;
}
$user = $this->userRepository->findOneBy(['email' => $username]);
if (null === $user || (empty($user->getPermId()) && empty($user->getLawFirm()?->getEcmId()))) {
return;
}
$event->setResponse(new RedirectResponse($this->router->generate('wam_login')));
}
}