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