vendor/symfony/security-core/Authorization/AuthorizationChecker.php line 62

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\Core\Authorization;
  11. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
  14. /**
  15.  * AuthorizationChecker is the main authorization point of the Security component.
  16.  *
  17.  * It gives access to the token representing the current user authentication.
  18.  *
  19.  * @author Fabien Potencier <fabien@symfony.com>
  20.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  21.  */
  22. class AuthorizationChecker implements AuthorizationCheckerInterface
  23. {
  24.     private $tokenStorage;
  25.     private $accessDecisionManager;
  26.     private $authenticationManager;
  27.     private $alwaysAuthenticate;
  28.     private $exceptionOnNoToken;
  29.     public function __construct(TokenStorageInterface $tokenStorageAuthenticationManagerInterface $authenticationManagerAccessDecisionManagerInterface $accessDecisionManagerbool $alwaysAuthenticate falsebool $exceptionOnNoToken true)
  30.     {
  31.         $this->tokenStorage $tokenStorage;
  32.         $this->authenticationManager $authenticationManager;
  33.         $this->accessDecisionManager $accessDecisionManager;
  34.         $this->alwaysAuthenticate $alwaysAuthenticate;
  35.         $this->exceptionOnNoToken $exceptionOnNoToken;
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      *
  40.      * @throws AuthenticationCredentialsNotFoundException when the token storage has no authentication token and $exceptionOnNoToken is set to true
  41.      */
  42.     final public function isGranted($attribute$subject null): bool
  43.     {
  44.         if (null === ($token $this->tokenStorage->getToken())) {
  45.             if ($this->exceptionOnNoToken) {
  46.                 throw new AuthenticationCredentialsNotFoundException('The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL.');
  47.             }
  48.             return false;
  49.         }
  50.         if ($this->alwaysAuthenticate || !$token->isAuthenticated()) {
  51.             $this->tokenStorage->setToken($token $this->authenticationManager->authenticate($token));
  52.         }
  53.         return $this->accessDecisionManager->decide($token, [$attribute], $subject);
  54.     }
  55. }