<?php
namespace App\Security;
use App\Application\Service\Factory\JsonApiFactory;
use App\Entity\System\Customer;
use App\EventListener\ControlpanelAccessControlListener;
use App\Model\Controlpanel\Response\JsonApiResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
class ControlPanelAuthenticator extends AbstractAuthenticator
{
protected TokenStorageInterface $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public function authenticate(Request $request): Passport
{
throw new AuthenticationException('No user found');
}
public function supports(Request $request): bool
{
$requestUri = $request->getRequestUri();
$token = $this->tokenStorage->getToken();
return \strpos($requestUri, ControlpanelAccessControlListener::CONTROLPANEL_URI) !== false
&& !($token && $token->getUser() instanceof Customer);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
return null;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): Response
{
$jsonApiDocument = JsonApiFactory::createUnauthorizedResponse();
return new JsonApiResponse($jsonApiDocument, Response::HTTP_UNAUTHORIZED, ['Content-Type' => 'application/vnd.api+json']);
}
}