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

  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\Token\NullToken;
  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\Http\AccessMapInterface;
  19. use Symfony\Component\Security\Http\Event\LazyResponseEvent;
  20. /**
  21.  * AccessListener enforces access control rules.
  22.  *
  23.  * @author Fabien Potencier <fabien@symfony.com>
  24.  *
  25.  * @final
  26.  */
  27. class AccessListener extends AbstractListener
  28. {
  29.     private TokenStorageInterface $tokenStorage;
  30.     private AccessDecisionManagerInterface $accessDecisionManager;
  31.     private AccessMapInterface $map;
  32.     public function __construct(TokenStorageInterface $tokenStorageAccessDecisionManagerInterface $accessDecisionManagerAccessMapInterface $mapbool $exceptionOnNoToken false)
  33.     {
  34.         if (false !== $exceptionOnNoToken) {
  35.             throw new \LogicException(sprintf('Argument $exceptionOnNoToken of "%s()" must be set to "false".'__METHOD__));
  36.         }
  37.         $this->tokenStorage $tokenStorage;
  38.         $this->accessDecisionManager $accessDecisionManager;
  39.         $this->map $map;
  40.     }
  41.     public function supports(Request $request): ?bool
  42.     {
  43.         [$attributes] = $this->map->getPatterns($request);
  44.         $request->attributes->set('_access_control_attributes'$attributes);
  45.         if ($attributes && (
  46.             (\defined(AuthenticatedVoter::class.'::IS_AUTHENTICATED_ANONYMOUSLY') ? [AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY] !== $attributes true)
  47.             && [AuthenticatedVoter::PUBLIC_ACCESS] !== $attributes
  48.         )) {
  49.             return true;
  50.         }
  51.         return null;
  52.     }
  53.     /**
  54.      * Handles access authorization.
  55.      *
  56.      * @throws AccessDeniedException
  57.      */
  58.     public function authenticate(RequestEvent $event)
  59.     {
  60.         $request $event->getRequest();
  61.         $attributes $request->attributes->get('_access_control_attributes');
  62.         $request->attributes->remove('_access_control_attributes');
  63.         if (!$attributes || ((
  64.             (\defined(AuthenticatedVoter::class.'::IS_AUTHENTICATED_ANONYMOUSLY') ? [AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY] === $attributes false)
  65.             || [AuthenticatedVoter::PUBLIC_ACCESS] === $attributes
  66.         ) && $event instanceof LazyResponseEvent)) {
  67.             return;
  68.         }
  69.         $token $this->tokenStorage->getToken() ?? new NullToken();
  70.         if (!$this->accessDecisionManager->decide($token$attributes$requesttrue)) {
  71.             throw $this->createAccessDeniedException($request$attributes);
  72.         }
  73.     }
  74.     private function createAccessDeniedException(Request $request, array $attributes)
  75.     {
  76.         $exception = new AccessDeniedException();
  77.         $exception->setAttributes($attributes);
  78.         $exception->setSubject($request);
  79.         return $exception;
  80.     }
  81.     public static function getPriority(): int
  82.     {
  83.         return -255;
  84.     }
  85. }