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

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  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. /**
  19.  * AccessListener enforces access control rules.
  20.  *
  21.  * @author Fabien Potencier <[email protected]>
  22.  *
  23.  * @final since Symfony 4.3
  24.  */
  25. class AccessListener implements ListenerInterface
  26. {
  27.     use LegacyListenerTrait;
  28.     private $tokenStorage;
  29.     private $accessDecisionManager;
  30.     private $map;
  31.     private $authManager;
  32.     public function __construct(TokenStorageInterface $tokenStorageAccessDecisionManagerInterface $accessDecisionManagerAccessMapInterface $mapAuthenticationManagerInterface $authManager)
  33.     {
  34.         $this->tokenStorage $tokenStorage;
  35.         $this->accessDecisionManager $accessDecisionManager;
  36.         $this->map $map;
  37.         $this->authManager $authManager;
  38.     }
  39.     /**
  40.      * Handles access authorization.
  41.      *
  42.      * @throws AccessDeniedException
  43.      * @throws AuthenticationCredentialsNotFoundException
  44.      */
  45.     public function __invoke(RequestEvent $event)
  46.     {
  47.         if (null === $token $this->tokenStorage->getToken()) {
  48.             throw new AuthenticationCredentialsNotFoundException('A Token was not found in the TokenStorage.');
  49.         }
  50.         $request $event->getRequest();
  51.         list($attributes) = $this->map->getPatterns($request);
  52.         if (null === $attributes) {
  53.             return;
  54.         }
  55.         if (!$token->isAuthenticated()) {
  56.             $token $this->authManager->authenticate($token);
  57.             $this->tokenStorage->setToken($token);
  58.         }
  59.         if (!$this->accessDecisionManager->decide($token$attributes$request)) {
  60.             $exception = new AccessDeniedException();
  61.             $exception->setAttributes($attributes);
  62.             $exception->setSubject($request);
  63.             throw $exception;
  64.         }
  65.     }
  66. }