src/Security/ControlPanelAuthenticator.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Application\Service\Factory\JsonApiFactory;
  4. use App\Entity\System\Customer;
  5. use App\EventListener\ControlpanelAccessControlListener;
  6. use App\Model\Controlpanel\Response\JsonApiResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  12. use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  14. class ControlPanelAuthenticator extends AbstractAuthenticator
  15. {
  16.     protected TokenStorageInterface $tokenStorage;
  17.     public function __construct(TokenStorageInterface $tokenStorage)
  18.     {
  19.         $this->tokenStorage $tokenStorage;
  20.     }
  21.     public function authenticate(Request $request): Passport
  22.     {
  23.         throw new AuthenticationException('No user found');
  24.     }
  25.     public function supports(Request $request): bool
  26.     {
  27.         $requestUri $request->getRequestUri();
  28.         $token $this->tokenStorage->getToken();
  29.         return \strpos($requestUriControlpanelAccessControlListener::CONTROLPANEL_URI) !== false
  30.             && !($token && $token->getUser() instanceof Customer);
  31.     }
  32.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  33.     {
  34.         return null;
  35.     }
  36.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): Response
  37.     {
  38.         $jsonApiDocument JsonApiFactory::createUnauthorizedResponse();
  39.         return new JsonApiResponse($jsonApiDocumentResponse::HTTP_UNAUTHORIZED, ['Content-Type' => 'application/vnd.api+json']);
  40.     }
  41. }