vendor/symfony/security-core/Authorization/Voter/AuthenticatedVoter.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\Security\Core\Authentication\AuthenticationTrustResolverInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. /**
  15.  * AuthenticatedVoter votes if an attribute like IS_AUTHENTICATED_FULLY,
  16.  * IS_AUTHENTICATED_REMEMBERED, or IS_AUTHENTICATED_ANONYMOUSLY is present.
  17.  *
  18.  * This list is most restrictive to least restrictive checking.
  19.  *
  20.  * @author Fabien Potencier <fabien@symfony.com>
  21.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  22.  */
  23. class AuthenticatedVoter implements VoterInterface
  24. {
  25.     public const IS_AUTHENTICATED_FULLY 'IS_AUTHENTICATED_FULLY';
  26.     public const IS_AUTHENTICATED_REMEMBERED 'IS_AUTHENTICATED_REMEMBERED';
  27.     public const IS_AUTHENTICATED_ANONYMOUSLY 'IS_AUTHENTICATED_ANONYMOUSLY';
  28.     public const IS_ANONYMOUS 'IS_ANONYMOUS';
  29.     public const IS_IMPERSONATOR 'IS_IMPERSONATOR';
  30.     public const IS_REMEMBERED 'IS_REMEMBERED';
  31.     private $authenticationTrustResolver;
  32.     public function __construct(AuthenticationTrustResolverInterface $authenticationTrustResolver)
  33.     {
  34.         $this->authenticationTrustResolver $authenticationTrustResolver;
  35.     }
  36.     /**
  37.      * {@inheritdoc}
  38.      */
  39.     public function vote(TokenInterface $token$subject, array $attributes)
  40.     {
  41.         $result VoterInterface::ACCESS_ABSTAIN;
  42.         foreach ($attributes as $attribute) {
  43.             if (null === $attribute || (self::IS_AUTHENTICATED_FULLY !== $attribute
  44.                     && self::IS_AUTHENTICATED_REMEMBERED !== $attribute
  45.                     && self::IS_AUTHENTICATED_ANONYMOUSLY !== $attribute
  46.                     && self::IS_ANONYMOUS !== $attribute
  47.                     && self::IS_IMPERSONATOR !== $attribute
  48.                     && self::IS_REMEMBERED !== $attribute)) {
  49.                 continue;
  50.             }
  51.             $result VoterInterface::ACCESS_DENIED;
  52.             if (self::IS_AUTHENTICATED_FULLY === $attribute
  53.                 && $this->authenticationTrustResolver->isFullFledged($token)) {
  54.                 return VoterInterface::ACCESS_GRANTED;
  55.             }
  56.             if (self::IS_AUTHENTICATED_REMEMBERED === $attribute
  57.                 && ($this->authenticationTrustResolver->isRememberMe($token)
  58.                     || $this->authenticationTrustResolver->isFullFledged($token))) {
  59.                 return VoterInterface::ACCESS_GRANTED;
  60.             }
  61.             if (self::IS_AUTHENTICATED_ANONYMOUSLY === $attribute
  62.                 && ($this->authenticationTrustResolver->isAnonymous($token)
  63.                     || $this->authenticationTrustResolver->isRememberMe($token)
  64.                     || $this->authenticationTrustResolver->isFullFledged($token))) {
  65.                 return VoterInterface::ACCESS_GRANTED;
  66.             }
  67.             if (self::IS_REMEMBERED === $attribute && $this->authenticationTrustResolver->isRememberMe($token)) {
  68.                 return VoterInterface::ACCESS_GRANTED;
  69.             }
  70.             if (self::IS_ANONYMOUS === $attribute && $this->authenticationTrustResolver->isAnonymous($token)) {
  71.                 return VoterInterface::ACCESS_GRANTED;
  72.             }
  73.             if (self::IS_IMPERSONATOR === $attribute && $token instanceof SwitchUserToken) {
  74.                 return VoterInterface::ACCESS_GRANTED;
  75.             }
  76.         }
  77.         return $result;
  78.     }
  79. }