vendor/api-platform/core/src/Symfony/Security/ResourceAccessChecker.php line 39

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.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. declare(strict_types=1);
  11. namespace ApiPlatform\Symfony\Security;
  12. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  13. use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\NullToken;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  17. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  18. use Symfony\Component\Security\Core\Role\Role;
  19. use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
  20. /**
  21.  * Checks if the logged user has sufficient permissions to access the given resource.
  22.  *
  23.  * @author Kévin Dunglas <dunglas@gmail.com>
  24.  */
  25. final class ResourceAccessChecker implements ResourceAccessCheckerInterface
  26. {
  27.     private $expressionLanguage;
  28.     private $authenticationTrustResolver;
  29.     private $roleHierarchy;
  30.     private $tokenStorage;
  31.     private $authorizationChecker;
  32.     private $exceptionOnNoToken;
  33.     public function __construct(ExpressionLanguage $expressionLanguage nullAuthenticationTrustResolverInterface $authenticationTrustResolver nullRoleHierarchyInterface $roleHierarchy nullTokenStorageInterface $tokenStorage nullAuthorizationCheckerInterface $authorizationChecker nullbool $exceptionOnNoToken true)
  34.     {
  35.         $this->expressionLanguage $expressionLanguage;
  36.         $this->authenticationTrustResolver $authenticationTrustResolver;
  37.         $this->roleHierarchy $roleHierarchy;
  38.         $this->tokenStorage $tokenStorage;
  39.         $this->authorizationChecker $authorizationChecker;
  40.         if (\func_num_args()) {
  41.             $this->exceptionOnNoToken $exceptionOnNoToken;
  42.             trigger_deprecation('api-platform/core''2.7''The $exceptionOnNoToken parameter in "%s()" is deprecated and will always be false in 3.0, you should stop using it.'__METHOD__);
  43.         }
  44.     }
  45.     public function isGranted(string $resourceClassstring $expression, array $extraVariables = []): bool
  46.     {
  47.         if (null === $this->tokenStorage || null === $this->authenticationTrustResolver) {
  48.             throw new \LogicException('The "symfony/security" library must be installed to use the "security" attribute.');
  49.         }
  50.         if (null === $token $this->tokenStorage->getToken()) {
  51.             if ($this->exceptionOnNoToken) {
  52.                 throw new \LogicException('The current token must be set to use the "security" attribute (is the URL behind a firewall?).');
  53.             }
  54.             if (class_exists(NullToken::class)) {
  55.                 $token = new NullToken();
  56.             }
  57.         }
  58.         if (null === $this->expressionLanguage) {
  59.             throw new \LogicException('The "symfony/expression-language" library must be installed to use the "security" attribute.');
  60.         }
  61.         $variables array_merge($extraVariables, [
  62.             'trust_resolver' => $this->authenticationTrustResolver,
  63.             'auth_checker' => $this->authorizationChecker// needed for the is_granted expression function
  64.         ]);
  65.         if ($token) {
  66.             $variables array_merge($variables$this->getVariables($token));
  67.         }
  68.         return (bool) $this->expressionLanguage->evaluate($expression$variables);
  69.     }
  70.     /**
  71.      * @copyright Fabien Potencier <fabien@symfony.com>
  72.      *
  73.      * @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Core/Authorization/Voter/ExpressionVoter.php
  74.      */
  75.     private function getVariables(TokenInterface $token): array
  76.     {
  77.         return [
  78.             'token' => $token,
  79.             'user' => $token->getUser(),
  80.             'roles' => $this->getEffectiveRoles($token),
  81.         ];
  82.     }
  83.     /**
  84.      * @return string[]
  85.      */
  86.     private function getEffectiveRoles(TokenInterface $token): array
  87.     {
  88.         if (null === $this->roleHierarchy) {
  89.             return method_exists($token'getRoleNames') ? $token->getRoleNames() : array_map('strval'$token->getRoles()); // @phpstan-ignore-line
  90.         }
  91.         if (method_exists($this->roleHierarchy'getReachableRoleNames')) {
  92.             return $this->roleHierarchy->getReachableRoleNames($token->getRoleNames());
  93.         }
  94.         return array_map(static function (Role $role): string {
  95.             return $role->getRole(); // @phpstan-ignore-line
  96.         }, $this->roleHierarchy->getReachableRoles($token->getRoles())); // @phpstan-ignore-line
  97.     }
  98. }
  99. class_alias(ResourceAccessChecker::class, \ApiPlatform\Core\Security\ResourceAccessChecker::class);