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

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\Voter;
  11. use Symfony\Component\ExpressionLanguage\Expression;
  12. use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  16. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  17. use Symfony\Component\Security\Core\Authorization\ExpressionLanguage;
  18. use Symfony\Component\Security\Core\Role\Role;
  19. use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
  20. /**
  21.  * ExpressionVoter votes based on the evaluation of an expression.
  22.  *
  23.  * @author Fabien Potencier <fabien@symfony.com>
  24.  */
  25. class ExpressionVoter implements VoterInterface
  26. {
  27.     private $expressionLanguage;
  28.     private $trustResolver;
  29.     private $authChecker;
  30.     private $roleHierarchy;
  31.     /**
  32.      * @param AuthorizationCheckerInterface $authChecker
  33.      */
  34.     public function __construct(ExpressionLanguage $expressionLanguageAuthenticationTrustResolverInterface $trustResolver$authChecker nullRoleHierarchyInterface $roleHierarchy null)
  35.     {
  36.         if ($authChecker instanceof RoleHierarchyInterface) {
  37.             @trigger_error(sprintf('Passing a RoleHierarchyInterface to "%s()" is deprecated since Symfony 4.2. Pass an AuthorizationCheckerInterface instead.'__METHOD__), E_USER_DEPRECATED);
  38.             $roleHierarchy $authChecker;
  39.             $authChecker null;
  40.             if (!method_exists($roleHierarchy'getReachableRoleNames')) {
  41.                 @trigger_error(sprintf('Not implementing the "%s::getReachableRoleNames()" method in "%s" is deprecated since Symfony 4.3.'RoleHierarchyInterface::class, \get_class($this->roleHierarchy)), E_USER_DEPRECATED);
  42.             }
  43.         } elseif (null === $authChecker) {
  44.             @trigger_error(sprintf('Argument 3 passed to "%s()" should be an instance of AuthorizationCheckerInterface, not passing it is deprecated since Symfony 4.2.'__METHOD__), E_USER_DEPRECATED);
  45.         } elseif (!$authChecker instanceof AuthorizationCheckerInterface) {
  46.             throw new \TypeError(sprintf('Argument 3 passed to "%s()" must be an instance of "%s" or null, "%s" given.'__METHOD__AuthorizationCheckerInterface::class, \is_object($authChecker) ? \get_class($authChecker) : \gettype($authChecker)));
  47.         }
  48.         $this->expressionLanguage $expressionLanguage;
  49.         $this->trustResolver $trustResolver;
  50.         $this->authChecker $authChecker;
  51.         $this->roleHierarchy $roleHierarchy;
  52.     }
  53.     /**
  54.      * @deprecated since Symfony 4.1, register the provider directly on the injected ExpressionLanguage instance instead.
  55.      */
  56.     public function addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)
  57.     {
  58.         @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.1, register the provider directly on the injected ExpressionLanguage instance instead.'__METHOD__), E_USER_DEPRECATED);
  59.         $this->expressionLanguage->registerProvider($provider);
  60.     }
  61.     /**
  62.      * {@inheritdoc}
  63.      */
  64.     public function vote(TokenInterface $token$subject, array $attributes)
  65.     {
  66.         $result VoterInterface::ACCESS_ABSTAIN;
  67.         $variables null;
  68.         foreach ($attributes as $attribute) {
  69.             if (!$attribute instanceof Expression) {
  70.                 continue;
  71.             }
  72.             if (null === $variables) {
  73.                 $variables $this->getVariables($token$subject);
  74.             }
  75.             $result VoterInterface::ACCESS_DENIED;
  76.             if ($this->expressionLanguage->evaluate($attribute$variables)) {
  77.                 return VoterInterface::ACCESS_GRANTED;
  78.             }
  79.         }
  80.         return $result;
  81.     }
  82.     private function getVariables(TokenInterface $token$subject): array
  83.     {
  84.         if (method_exists($token'getRoleNames')) {
  85.             $roleNames $token->getRoleNames();
  86.             $roles array_map(function (string $role) { return new Role($rolefalse); }, $roleNames);
  87.         } else {
  88.             @trigger_error(sprintf('Not implementing the "%s::getRoleNames()" method in "%s" is deprecated since Symfony 4.3.'TokenInterface::class, \get_class($token)), E_USER_DEPRECATED);
  89.             $roles $token->getRoles(false);
  90.             $roleNames array_map(function (Role $role) { return $role->getRole(); }, $roles);
  91.         }
  92.         if (null !== $this->roleHierarchy && method_exists($this->roleHierarchy'getReachableRoleNames')) {
  93.             $roleNames $this->roleHierarchy->getReachableRoleNames($roleNames);
  94.             $roles array_map(function (string $role) { return new Role($rolefalse); }, $roleNames);
  95.         } elseif (null !== $this->roleHierarchy) {
  96.             $roles $this->roleHierarchy->getReachableRoles($roles);
  97.             $roleNames array_map(function (Role $role) { return $role->getRole(); }, $roles);
  98.         }
  99.         $variables = [
  100.             'token' => $token,
  101.             'user' => $token->getUser(),
  102.             'object' => $subject,
  103.             'subject' => $subject,
  104.             'roles' => $roles,
  105.             'role_names' => $roleNames,
  106.             'trust_resolver' => $this->trustResolver,
  107.             'auth_checker' => $this->authChecker,
  108.         ];
  109.         // this is mainly to propose a better experience when the expression is used
  110.         // in an access control rule, as the developer does not know that it's going
  111.         // to be handled by this voter
  112.         if ($subject instanceof Request) {
  113.             $variables['request'] = $subject;
  114.         }
  115.         return $variables;
  116.     }
  117. }