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