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.     private $tokenStorage;
  31.     private $accessDecisionManager;
  32.     private $map;
  33.     private $authManager;
  34.     public function __construct(TokenStorageInterface $tokenStorageAccessDecisionManagerInterface $accessDecisionManagerAccessMapInterface $mapAuthenticationManagerInterface $authManager)
  35.     {
  36.         $this->tokenStorage $tokenStorage;
  37.         $this->accessDecisionManager $accessDecisionManager;
  38.         $this->map $map;
  39.         $this->authManager $authManager;
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public function supports(Request $request): ?bool
  45.     {
  46.         [$attributes] = $this->map->getPatterns($request);
  47.         $request->attributes->set('_access_control_attributes'$attributes);
  48.         return $attributes && [AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY] !== $attributes true null;
  49.     }
  50.     /**
  51.      * Handles access authorization.
  52.      *
  53.      * @throws AccessDeniedException
  54.      * @throws AuthenticationCredentialsNotFoundException
  55.      */
  56.     public function authenticate(RequestEvent $event)
  57.     {
  58.         if (!$event instanceof LazyResponseEvent && null === $token $this->tokenStorage->getToken()) {
  59.             throw new AuthenticationCredentialsNotFoundException('A Token was not found in the TokenStorage.');
  60.         }
  61.         $request $event->getRequest();
  62.         $attributes $request->attributes->get('_access_control_attributes');
  63.         $request->attributes->remove('_access_control_attributes');
  64.         if (!$attributes || ([AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY] === $attributes && $event instanceof LazyResponseEvent)) {
  65.             return;
  66.         }
  67.         if ($event instanceof LazyResponseEvent && null === $token $this->tokenStorage->getToken()) {
  68.             throw new AuthenticationCredentialsNotFoundException('A Token was not found in the TokenStorage.');
  69.         }
  70.         if (!$token->isAuthenticated()) {
  71.             $token $this->authManager->authenticate($token);
  72.             $this->tokenStorage->setToken($token);
  73.         }
  74.         $granted false;
  75.         foreach ($attributes as $key => $value) {
  76.             if ($this->accessDecisionManager->decide($token, [$key => $value], $request)) {
  77.                 $granted true;
  78.                 break;
  79.             }
  80.         }
  81.         if (!$granted) {
  82.             $exception = new AccessDeniedException();
  83.             $exception->setAttributes($attributes);
  84.             $exception->setSubject($request);
  85.             throw $exception;
  86.         }
  87.     }
  88. }