src/Security/LoginFormAuthenticator.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use Doctrine\ORM\NonUniqueResultException;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  9. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  10. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  11. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  12. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  13. class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
  14. {
  15.     use TargetPathTrait;
  16.     private CustomerLoginFormAuthenticator $customerLoginFormAuthenticator;
  17.     private AdminLoginFormAuthenticator $adminLoginFormAuthenticator;
  18.     private ControlPanelAuthenticator $controlPanelAuthenticator;
  19.     public function __construct(
  20.         CustomerLoginFormAuthenticator $customerLoginFormAuthenticator,
  21.         AdminLoginFormAuthenticator $adminLoginFormAuthenticator,
  22.         ControlPanelAuthenticator $controlPanelAuthenticator
  23.     ) {
  24.         $this->customerLoginFormAuthenticator $customerLoginFormAuthenticator;
  25.         $this->adminLoginFormAuthenticator $adminLoginFormAuthenticator;
  26.         $this->controlPanelAuthenticator $controlPanelAuthenticator;
  27.     }
  28.     /**
  29.      * @throws NonUniqueResultException
  30.      */
  31.     public function authenticate(Request $request): Passport
  32.     {
  33.         if ($this->adminLoginFormAuthenticator->supports($request)) {
  34.             return $this->adminLoginFormAuthenticator->authenticate($request);
  35.         }
  36.         if ($this->customerLoginFormAuthenticator->supports($request)) {
  37.             return $this->customerLoginFormAuthenticator->authenticate($request);
  38.         }
  39.         if ($this->controlPanelAuthenticator->supports($request)) {
  40.             return $this->controlPanelAuthenticator->authenticate($request);
  41.         }
  42.         throw new UserNotFoundException();
  43.     }
  44.     /**
  45.      * {@inheritDoc}
  46.      */
  47.     public function supports(Request $request): bool
  48.     {
  49.         return $this->customerLoginFormAuthenticator->supports($request)
  50.             || $this->adminLoginFormAuthenticator->supports($request)
  51.             || $this->controlPanelAuthenticator->supports($request);
  52.     }
  53.     /**
  54.      * {@inheritDoc}
  55.      */
  56.     public function onAuthenticationSuccess(Request $requestTokenInterface $token$firewallName): ?Response
  57.     {
  58.         if ($this->adminLoginFormAuthenticator->supports($request)) {
  59.             return $this->adminLoginFormAuthenticator->onAuthenticationSuccess($request$token$firewallName);
  60.         }
  61.         if ($this->customerLoginFormAuthenticator->supports($request)) {
  62.             return $this->customerLoginFormAuthenticator->onAuthenticationSuccess($request$token$firewallName);
  63.         }
  64.         if ($this->controlPanelAuthenticator->supports($request)) {
  65.             return $this->controlPanelAuthenticator->onAuthenticationSuccess($request$token$firewallName);
  66.         }
  67.         return null;
  68.     }
  69.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): Response
  70.     {
  71.         if ($this->adminLoginFormAuthenticator->supports($request)) {
  72.             return $this->adminLoginFormAuthenticator->onAuthenticationFailure($request$exception);
  73.         }
  74.         if ($this->customerLoginFormAuthenticator->supports($request)) {
  75.             return $this->customerLoginFormAuthenticator->onAuthenticationFailure($request$exception);
  76.         }
  77.         if ($this->controlPanelAuthenticator->supports($request)) {
  78.             return $this->controlPanelAuthenticator->onAuthenticationFailure($request$exception);
  79.         }
  80.         return new RedirectResponse($this->getLoginUrl($request));
  81.     }
  82.     /**
  83.      * {@inheritDoc}
  84.      *
  85.      * @param Request $request
  86.      */
  87.     protected function getLoginUrl(Request $request): string
  88.     {
  89.         if ($this->adminLoginFormAuthenticator->supports($request)) {
  90.             return $this->adminLoginFormAuthenticator->getLoginUrl($request);
  91.         }
  92.         return $this->customerLoginFormAuthenticator->getLoginUrl($request);
  93.     }
  94. }