vendor/symfony/security/Core/Authorization/Voter/ExpressionVoter.php line 27

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\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 VoterInterface
  24. {
  25.     private $expressionLanguage;
  26.     private $trustResolver;
  27.     private $roleHierarchy;
  28.     public function __construct(ExpressionLanguage $expressionLanguageAuthenticationTrustResolverInterface $trustResolverRoleHierarchyInterface $roleHierarchy null)
  29.     {
  30.         $this->expressionLanguage $expressionLanguage;
  31.         $this->trustResolver $trustResolver;
  32.         $this->roleHierarchy $roleHierarchy;
  33.     }
  34.     /**
  35.      * @deprecated since Symfony 4.1, register the provider directly on the injected ExpressionLanguage instance instead.
  36.      */
  37.     public function addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)
  38.     {
  39.         @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);
  40.         $this->expressionLanguage->registerProvider($provider);
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function vote(TokenInterface $token$subject, array $attributes)
  46.     {
  47.         $result VoterInterface::ACCESS_ABSTAIN;
  48.         $variables null;
  49.         foreach ($attributes as $attribute) {
  50.             if (!$attribute instanceof Expression) {
  51.                 continue;
  52.             }
  53.             if (null === $variables) {
  54.                 $variables $this->getVariables($token$subject);
  55.             }
  56.             $result VoterInterface::ACCESS_DENIED;
  57.             if ($this->expressionLanguage->evaluate($attribute$variables)) {
  58.                 return VoterInterface::ACCESS_GRANTED;
  59.             }
  60.         }
  61.         return $result;
  62.     }
  63.     private function getVariables(TokenInterface $token$subject)
  64.     {
  65.         if (null !== $this->roleHierarchy) {
  66.             $roles $this->roleHierarchy->getReachableRoles($token->getRoles());
  67.         } else {
  68.             $roles $token->getRoles();
  69.         }
  70.         $variables = [
  71.             'token' => $token,
  72.             'user' => $token->getUser(),
  73.             'object' => $subject,
  74.             'subject' => $subject,
  75.             'roles' => array_map(function ($role) { return $role->getRole(); }, $roles),
  76.             'trust_resolver' => $this->trustResolver,
  77.         ];
  78.         // this is mainly to propose a better experience when the expression is used
  79.         // in an access control rule, as the developer does not know that it's going
  80.         // to be handled by this voter
  81.         if ($subject instanceof Request) {
  82.             $variables['request'] = $subject;
  83.         }
  84.         return $variables;
  85.     }
  86. }