vendor/symfony/security-core/Authorization/Voter/ExpressionVoter.php line 27

  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\Voter;
  11. use Symfony\Component\ExpressionLanguage\Expression;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  15. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  16. use Symfony\Component\Security\Core\Authorization\ExpressionLanguage;
  17. use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
  18. /**
  19.  * ExpressionVoter votes based on the evaluation of an expression.
  20.  *
  21.  * @author Fabien Potencier <fabien@symfony.com>
  22.  */
  23. class ExpressionVoter implements CacheableVoterInterface
  24. {
  25.     private ExpressionLanguage $expressionLanguage;
  26.     private AuthenticationTrustResolverInterface $trustResolver;
  27.     private AuthorizationCheckerInterface $authChecker;
  28.     private ?RoleHierarchyInterface $roleHierarchy;
  29.     public function __construct(ExpressionLanguage $expressionLanguageAuthenticationTrustResolverInterface $trustResolverAuthorizationCheckerInterface $authChecker, ?RoleHierarchyInterface $roleHierarchy null)
  30.     {
  31.         $this->expressionLanguage $expressionLanguage;
  32.         $this->trustResolver $trustResolver;
  33.         $this->authChecker $authChecker;
  34.         $this->roleHierarchy $roleHierarchy;
  35.     }
  36.     public function supportsAttribute(string $attribute): bool
  37.     {
  38.         return false;
  39.     }
  40.     public function supportsType(string $subjectType): bool
  41.     {
  42.         return true;
  43.     }
  44.     public function vote(TokenInterface $tokenmixed $subject, array $attributes): int
  45.     {
  46.         $result VoterInterface::ACCESS_ABSTAIN;
  47.         $variables null;
  48.         foreach ($attributes as $attribute) {
  49.             if (!$attribute instanceof Expression) {
  50.                 continue;
  51.             }
  52.             $variables ??= $this->getVariables($token$subject);
  53.             $result VoterInterface::ACCESS_DENIED;
  54.             if ($this->expressionLanguage->evaluate($attribute$variables)) {
  55.                 return VoterInterface::ACCESS_GRANTED;
  56.             }
  57.         }
  58.         return $result;
  59.     }
  60.     private function getVariables(TokenInterface $tokenmixed $subject): array
  61.     {
  62.         $roleNames $token->getRoleNames();
  63.         if (null !== $this->roleHierarchy) {
  64.             $roleNames $this->roleHierarchy->getReachableRoleNames($roleNames);
  65.         }
  66.         $variables = [
  67.             'token' => $token,
  68.             'user' => $token->getUser(),
  69.             'object' => $subject,
  70.             'subject' => $subject,
  71.             'role_names' => $roleNames,
  72.             'trust_resolver' => $this->trustResolver,
  73.             'auth_checker' => $this->authChecker,
  74.         ];
  75.         // this is mainly to propose a better experience when the expression is used
  76.         // in an access control rule, as the developer does not know that it's going
  77.         // to be handled by this voter
  78.         if ($subject instanceof Request) {
  79.             $variables['request'] = $subject;
  80.         }
  81.         return $variables;
  82.     }
  83. }