vendor/symfony/symfony/src/Symfony/Component/Security/Http/Firewall/AccessListener.php line 30

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\HttpKernel\Event\RequestEvent;
  12. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  15. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  16. use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
  17. use Symfony\Component\Security\Http\AccessMapInterface;
  18. use Symfony\Component\Security\Http\Event\LazyResponseEvent;
  19. /**
  20.  * AccessListener enforces access control rules.
  21.  *
  22.  * @author Fabien Potencier <fabien@symfony.com>
  23.  *
  24.  * @final since Symfony 4.3
  25.  */
  26. class AccessListener implements ListenerInterface
  27. {
  28.     use LegacyListenerTrait;
  29.     private $tokenStorage;
  30.     private $accessDecisionManager;
  31.     private $map;
  32.     private $authManager;
  33.     public function __construct(TokenStorageInterface $tokenStorageAccessDecisionManagerInterface $accessDecisionManagerAccessMapInterface $mapAuthenticationManagerInterface $authManager)
  34.     {
  35.         $this->tokenStorage $tokenStorage;
  36.         $this->accessDecisionManager $accessDecisionManager;
  37.         $this->map $map;
  38.         $this->authManager $authManager;
  39.     }
  40.     /**
  41.      * Handles access authorization.
  42.      *
  43.      * @throws AccessDeniedException
  44.      * @throws AuthenticationCredentialsNotFoundException
  45.      */
  46.     public function __invoke(RequestEvent $event)
  47.     {
  48.         if (!$event instanceof LazyResponseEvent && null === $token $this->tokenStorage->getToken()) {
  49.             throw new AuthenticationCredentialsNotFoundException('A Token was not found in the TokenStorage.');
  50.         }
  51.         $request $event->getRequest();
  52.         list($attributes) = $this->map->getPatterns($request);
  53.         if (!$attributes) {
  54.             return;
  55.         }
  56.         if ($event instanceof LazyResponseEvent && null === $token $this->tokenStorage->getToken()) {
  57.             throw new AuthenticationCredentialsNotFoundException('A Token was not found in the TokenStorage.');
  58.         }
  59.         if (!$token->isAuthenticated()) {
  60.             $token $this->authManager->authenticate($token);
  61.             $this->tokenStorage->setToken($token);
  62.         }
  63.         $granted false;
  64.         foreach ($attributes as $key => $value) {
  65.             if ($this->accessDecisionManager->decide($token, [$key => $value], $request)) {
  66.                 $granted true;
  67.                 break;
  68.             }
  69.         }
  70.         if (!$granted) {
  71.             $exception = new AccessDeniedException();
  72.             $exception->setAttributes($attributes);
  73.             $exception->setSubject($request);
  74.             throw $exception;
  75.         }
  76.     }
  77. }