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 <[email protected]>
  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, IS_AUTHENTICATED is present.
  17.  *
  18.  * This list is most restrictive to least restrictive checking.
  19.  *
  20.  * @author Fabien Potencier <[email protected]>
  21.  * @author Johannes M. Schmitt <[email protected]>
  22.  */
  23. class AuthenticatedVoter implements CacheableVoterInterface
  24. {
  25.     public const IS_AUTHENTICATED_FULLY 'IS_AUTHENTICATED_FULLY';
  26.     public const IS_AUTHENTICATED_REMEMBERED 'IS_AUTHENTICATED_REMEMBERED';
  27.     public const IS_AUTHENTICATED 'IS_AUTHENTICATED';
  28.     public const IS_IMPERSONATOR 'IS_IMPERSONATOR';
  29.     public const IS_REMEMBERED 'IS_REMEMBERED';
  30.     public const PUBLIC_ACCESS 'PUBLIC_ACCESS';
  31.     private $authenticationTrustResolver;
  32.     public function __construct(AuthenticationTrustResolverInterface $authenticationTrustResolver)
  33.     {
  34.         $this->authenticationTrustResolver $authenticationTrustResolver;
  35.     }
  36.     /**
  37.      * {@inheritdoc}
  38.      */
  39.     public function vote(TokenInterface $tokenmixed $subject, array $attributes): int
  40.     {
  41.         if ($attributes === [self::PUBLIC_ACCESS]) {
  42.             return VoterInterface::ACCESS_GRANTED;
  43.         }
  44.         $result VoterInterface::ACCESS_ABSTAIN;
  45.         foreach ($attributes as $attribute) {
  46.             if (null === $attribute || (self::IS_AUTHENTICATED_FULLY !== $attribute
  47.                     && self::IS_AUTHENTICATED_REMEMBERED !== $attribute
  48.                     && self::IS_AUTHENTICATED !== $attribute
  49.                     && self::IS_IMPERSONATOR !== $attribute
  50.                     && self::IS_REMEMBERED !== $attribute)) {
  51.                 continue;
  52.             }
  53.             $result VoterInterface::ACCESS_DENIED;
  54.             if (self::IS_AUTHENTICATED_FULLY === $attribute
  55.                 && $this->authenticationTrustResolver->isFullFledged($token)) {
  56.                 return VoterInterface::ACCESS_GRANTED;
  57.             }
  58.             if (self::IS_AUTHENTICATED_REMEMBERED === $attribute
  59.                 && ($this->authenticationTrustResolver->isRememberMe($token)
  60.                     || $this->authenticationTrustResolver->isFullFledged($token))) {
  61.                 return VoterInterface::ACCESS_GRANTED;
  62.             }
  63.             if (self::IS_AUTHENTICATED === $attribute && $this->authenticationTrustResolver->isAuthenticated($token)) {
  64.                 return VoterInterface::ACCESS_GRANTED;
  65.             }
  66.             if (self::IS_REMEMBERED === $attribute && $this->authenticationTrustResolver->isRememberMe($token)) {
  67.                 return VoterInterface::ACCESS_GRANTED;
  68.             }
  69.             if (self::IS_IMPERSONATOR === $attribute && $token instanceof SwitchUserToken) {
  70.                 return VoterInterface::ACCESS_GRANTED;
  71.             }
  72.         }
  73.         return $result;
  74.     }
  75.     public function supportsAttribute(string $attribute): bool
  76.     {
  77.         return \in_array($attribute, [
  78.             self::IS_AUTHENTICATED_FULLY,
  79.             self::IS_AUTHENTICATED_REMEMBERED,
  80.             self::IS_AUTHENTICATED,
  81.             self::IS_IMPERSONATOR,
  82.             self::IS_REMEMBERED,
  83.             self::PUBLIC_ACCESS,
  84.         ], true);
  85.     }
  86.     public function supportsType(string $subjectType): bool
  87.     {
  88.         return true;
  89.     }
  90. }