<?php
namespace App\Security;
use Doctrine\ORM\NonUniqueResultException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
{
use TargetPathTrait;
private CustomerLoginFormAuthenticator $customerLoginFormAuthenticator;
private AdminLoginFormAuthenticator $adminLoginFormAuthenticator;
private ControlPanelAuthenticator $controlPanelAuthenticator;
public function __construct(
CustomerLoginFormAuthenticator $customerLoginFormAuthenticator,
AdminLoginFormAuthenticator $adminLoginFormAuthenticator,
ControlPanelAuthenticator $controlPanelAuthenticator
) {
$this->customerLoginFormAuthenticator = $customerLoginFormAuthenticator;
$this->adminLoginFormAuthenticator = $adminLoginFormAuthenticator;
$this->controlPanelAuthenticator = $controlPanelAuthenticator;
}
/**
* @throws NonUniqueResultException
*/
public function authenticate(Request $request): Passport
{
if ($this->adminLoginFormAuthenticator->supports($request)) {
return $this->adminLoginFormAuthenticator->authenticate($request);
}
if ($this->customerLoginFormAuthenticator->supports($request)) {
return $this->customerLoginFormAuthenticator->authenticate($request);
}
if ($this->controlPanelAuthenticator->supports($request)) {
return $this->controlPanelAuthenticator->authenticate($request);
}
throw new UserNotFoundException();
}
/**
* {@inheritDoc}
*/
public function supports(Request $request): bool
{
return $this->customerLoginFormAuthenticator->supports($request)
|| $this->adminLoginFormAuthenticator->supports($request)
|| $this->controlPanelAuthenticator->supports($request);
}
/**
* {@inheritDoc}
*/
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $firewallName): ?Response
{
if ($this->adminLoginFormAuthenticator->supports($request)) {
return $this->adminLoginFormAuthenticator->onAuthenticationSuccess($request, $token, $firewallName);
}
if ($this->customerLoginFormAuthenticator->supports($request)) {
return $this->customerLoginFormAuthenticator->onAuthenticationSuccess($request, $token, $firewallName);
}
if ($this->controlPanelAuthenticator->supports($request)) {
return $this->controlPanelAuthenticator->onAuthenticationSuccess($request, $token, $firewallName);
}
return null;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): Response
{
if ($this->adminLoginFormAuthenticator->supports($request)) {
return $this->adminLoginFormAuthenticator->onAuthenticationFailure($request, $exception);
}
if ($this->customerLoginFormAuthenticator->supports($request)) {
return $this->customerLoginFormAuthenticator->onAuthenticationFailure($request, $exception);
}
if ($this->controlPanelAuthenticator->supports($request)) {
return $this->controlPanelAuthenticator->onAuthenticationFailure($request, $exception);
}
return new RedirectResponse($this->getLoginUrl($request));
}
/**
* {@inheritDoc}
*
* @param Request $request
*/
protected function getLoginUrl(Request $request): string
{
if ($this->adminLoginFormAuthenticator->supports($request)) {
return $this->adminLoginFormAuthenticator->getLoginUrl($request);
}
return $this->customerLoginFormAuthenticator->getLoginUrl($request);
}
}