vendor/symfony/security-http/Firewall/AccessListener.php line 32

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Security\Http\Firewall;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Event\RequestEvent;
  13. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  16. use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
  17. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  18. use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
  19. use Symfony\Component\Security\Http\AccessMapInterface;
  20. use Symfony\Component\Security\Http\Event\LazyResponseEvent;
  21. /**
  22.  * AccessListener enforces access control rules.
  23.  *
  24.  * @author Fabien Potencier <fabien@symfony.com>
  25.  *
  26.  * @final
  27.  */
  28. class AccessListener extends AbstractListener
  29. {
  30.     public const PUBLIC_ACCESS 'PUBLIC_ACCESS';
  31.     private $tokenStorage;
  32.     private $accessDecisionManager;
  33.     private $map;
  34.     private $authManager;
  35.     private $exceptionOnNoToken;
  36.     public function __construct(TokenStorageInterface $tokenStorageAccessDecisionManagerInterface $accessDecisionManagerAccessMapInterface $mapAuthenticationManagerInterface $authManagerbool $exceptionOnNoToken true)
  37.     {
  38.         $this->tokenStorage $tokenStorage;
  39.         $this->accessDecisionManager $accessDecisionManager;
  40.         $this->map $map;
  41.         $this->authManager $authManager;
  42.         $this->exceptionOnNoToken $exceptionOnNoToken;
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public function supports(Request $request): ?bool
  48.     {
  49.         [$attributes] = $this->map->getPatterns($request);
  50.         $request->attributes->set('_access_control_attributes'$attributes);
  51.         return $attributes && ([AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY] !== $attributes && [self::PUBLIC_ACCESS] !== $attributes) ? true null;
  52.     }
  53.     /**
  54.      * Handles access authorization.
  55.      *
  56.      * @throws AccessDeniedException
  57.      * @throws AuthenticationCredentialsNotFoundException when the token storage has no authentication token and $exceptionOnNoToken is set to true
  58.      */
  59.     public function authenticate(RequestEvent $event)
  60.     {
  61.         if (!$event instanceof LazyResponseEvent && null === ($token $this->tokenStorage->getToken()) && $this->exceptionOnNoToken) {
  62.             throw new AuthenticationCredentialsNotFoundException('A Token was not found in the TokenStorage.');
  63.         }
  64.         $request $event->getRequest();
  65.         $attributes $request->attributes->get('_access_control_attributes');
  66.         $request->attributes->remove('_access_control_attributes');
  67.         if (!$attributes || ([AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY] === $attributes && $event instanceof LazyResponseEvent)) {
  68.             return;
  69.         }
  70.         if ($event instanceof LazyResponseEvent) {
  71.             $token $this->tokenStorage->getToken();
  72.         }
  73.         if (null === $token) {
  74.             if ($this->exceptionOnNoToken) {
  75.                 throw new AuthenticationCredentialsNotFoundException('A Token was not found in the TokenStorage.');
  76.             }
  77.             if ([AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY] === $attributes) {
  78.                 trigger_deprecation('symfony/security-http''5.1''Using "IS_AUTHENTICATED_ANONYMOUSLY" in your access_control rules when using the authenticator Security system is deprecated, use "PUBLIC_ACCESS" instead.');
  79.                 return;
  80.             }
  81.             if ([self::PUBLIC_ACCESS] !== $attributes) {
  82.                 throw $this->createAccessDeniedException($request$attributes);
  83.             }
  84.         }
  85.         if ([self::PUBLIC_ACCESS] === $attributes) {
  86.             return;
  87.         }
  88.         if (!$token->isAuthenticated()) {
  89.             $token $this->authManager->authenticate($token);
  90.             $this->tokenStorage->setToken($token);
  91.         }
  92.         if (!$this->accessDecisionManager->decide($token$attributes$requesttrue)) {
  93.             throw $this->createAccessDeniedException($request$attributes);
  94.         }
  95.     }
  96.     private function createAccessDeniedException(Request $request, array $attributes)
  97.     {
  98.         $exception = new AccessDeniedException();
  99.         $exception->setAttributes($attributes);
  100.         $exception->setSubject($request);
  101.         return $exception;
  102.     }
  103. }