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

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.     public function __construct(TokenStorageInterface $tokenStorageAuthenticationManagerInterface $authenticationManagerAccessDecisionManagerInterface $accessDecisionManagerbool $alwaysAuthenticate false)
  29.     {
  30.         $this->tokenStorage $tokenStorage;
  31.         $this->authenticationManager $authenticationManager;
  32.         $this->accessDecisionManager $accessDecisionManager;
  33.         $this->alwaysAuthenticate $alwaysAuthenticate;
  34.     }
  35.     /**
  36.      * {@inheritdoc}
  37.      *
  38.      * @throws AuthenticationCredentialsNotFoundException when the token storage has no authentication token
  39.      */
  40.     final public function isGranted($attributes$subject null): bool
  41.     {
  42.         if (null === ($token $this->tokenStorage->getToken())) {
  43.             throw new AuthenticationCredentialsNotFoundException('The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL.');
  44.         }
  45.         if ($this->alwaysAuthenticate || !$token->isAuthenticated()) {
  46.             $this->tokenStorage->setToken($token $this->authenticationManager->authenticate($token));
  47.         }
  48.         if (!\is_array($attributes)) {
  49.             $attributes = [$attributes];
  50.         } else {
  51.             @trigger_error(sprintf('Passing an array of Security attributes to %s() is deprecated since Symfony 4.4. Use multiple isGranted() calls or the expression language (e.g. "is_granted(...) or is_granted(...)") instead.'__METHOD__), \E_USER_DEPRECATED);
  52.         }
  53.         return $this->accessDecisionManager->decide($token$attributes$subject);
  54.     }
  55. }