vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authorization/AuthorizationChecker.php line 65

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.     /**
  29.      * @param AuthenticationManagerInterface $authenticationManager An AuthenticationManager instance
  30.      * @param AccessDecisionManagerInterface $accessDecisionManager An AccessDecisionManager instance
  31.      * @param bool                           $alwaysAuthenticate
  32.      */
  33.     public function __construct(TokenStorageInterface $tokenStorageAuthenticationManagerInterface $authenticationManagerAccessDecisionManagerInterface $accessDecisionManager$alwaysAuthenticate false)
  34.     {
  35.         $this->tokenStorage $tokenStorage;
  36.         $this->authenticationManager $authenticationManager;
  37.         $this->accessDecisionManager $accessDecisionManager;
  38.         $this->alwaysAuthenticate $alwaysAuthenticate;
  39.     }
  40.     /**
  41.      * {@inheritdoc}
  42.      *
  43.      * @throws AuthenticationCredentialsNotFoundException when the token storage has no authentication token
  44.      */
  45.     final public function isGranted($attributes$subject null)
  46.     {
  47.         if (null === ($token $this->tokenStorage->getToken())) {
  48.             throw new AuthenticationCredentialsNotFoundException('The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL.');
  49.         }
  50.         if ($this->alwaysAuthenticate || !$token->isAuthenticated()) {
  51.             $this->tokenStorage->setToken($token $this->authenticationManager->authenticate($token));
  52.         }
  53.         if (!\is_array($attributes)) {
  54.             $attributes = [$attributes];
  55.         }
  56.         return $this->accessDecisionManager->decide($token$attributes$subject);
  57.     }
  58. }