vendor/symfony/security-core/Authorization/Voter/AuthenticatedVoter.php line 106

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, IS_AUTHENTICATED 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 CacheableVoterInterface
  24. {
  25.     public const IS_AUTHENTICATED_FULLY 'IS_AUTHENTICATED_FULLY';
  26.     public const IS_AUTHENTICATED_REMEMBERED 'IS_AUTHENTICATED_REMEMBERED';
  27.     /**
  28.      * @deprecated since Symfony 5.4
  29.      */
  30.     public const IS_AUTHENTICATED_ANONYMOUSLY 'IS_AUTHENTICATED_ANONYMOUSLY';
  31.     /**
  32.      * @deprecated since Symfony 5.4
  33.      */
  34.     public const IS_ANONYMOUS 'IS_ANONYMOUS';
  35.     public const IS_AUTHENTICATED 'IS_AUTHENTICATED';
  36.     public const IS_IMPERSONATOR 'IS_IMPERSONATOR';
  37.     public const IS_REMEMBERED 'IS_REMEMBERED';
  38.     public const PUBLIC_ACCESS 'PUBLIC_ACCESS';
  39.     private $authenticationTrustResolver;
  40.     public function __construct(AuthenticationTrustResolverInterface $authenticationTrustResolver)
  41.     {
  42.         $this->authenticationTrustResolver $authenticationTrustResolver;
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public function vote(TokenInterface $token$subject, array $attributes)
  48.     {
  49.         if ($attributes === [self::PUBLIC_ACCESS]) {
  50.             return VoterInterface::ACCESS_GRANTED;
  51.         }
  52.         $result VoterInterface::ACCESS_ABSTAIN;
  53.         foreach ($attributes as $attribute) {
  54.             if (null === $attribute || (self::IS_AUTHENTICATED_FULLY !== $attribute
  55.                     && self::IS_AUTHENTICATED_REMEMBERED !== $attribute
  56.                     && self::IS_AUTHENTICATED_ANONYMOUSLY !== $attribute
  57.                     && self::IS_AUTHENTICATED !== $attribute
  58.                     && self::IS_ANONYMOUS !== $attribute
  59.                     && self::IS_IMPERSONATOR !== $attribute
  60.                     && self::IS_REMEMBERED !== $attribute)) {
  61.                 continue;
  62.             }
  63.             $result VoterInterface::ACCESS_DENIED;
  64.             if (self::IS_AUTHENTICATED_FULLY === $attribute
  65.                 && $this->authenticationTrustResolver->isFullFledged($token)) {
  66.                 return VoterInterface::ACCESS_GRANTED;
  67.             }
  68.             if (self::IS_AUTHENTICATED_REMEMBERED === $attribute
  69.                 && ($this->authenticationTrustResolver->isRememberMe($token)
  70.                     || $this->authenticationTrustResolver->isFullFledged($token))) {
  71.                 return VoterInterface::ACCESS_GRANTED;
  72.             }
  73.             if (self::IS_AUTHENTICATED_ANONYMOUSLY === $attribute
  74.                 && ($this->authenticationTrustResolver->isAnonymous($token)
  75.                     || $this->authenticationTrustResolver->isRememberMe($token)
  76.                     || $this->authenticationTrustResolver->isFullFledged($token))) {
  77.                 trigger_deprecation('symfony/security-core''5.4''The "IS_AUTHENTICATED_ANONYMOUSLY" security attribute is deprecated, use "PUBLIC_ACCESS" for public resources, otherwise use "IS_AUTHENTICATED" or "IS_AUTHENTICATED_FULLY" instead if you want to check if the request is (fully) authenticated.');
  78.                 return VoterInterface::ACCESS_GRANTED;
  79.             }
  80.             // @deprecated $this->authenticationTrustResolver must implement isAuthenticated() in 6.0
  81.             if (self::IS_AUTHENTICATED === $attribute
  82.                 && (method_exists($this->authenticationTrustResolver'isAuthenticated')
  83.                     ? $this->authenticationTrustResolver->isAuthenticated($token)
  84.                     : ($token && $token->getUser()))) {
  85.                 return VoterInterface::ACCESS_GRANTED;
  86.             }
  87.             if (self::IS_REMEMBERED === $attribute && $this->authenticationTrustResolver->isRememberMe($token)) {
  88.                 return VoterInterface::ACCESS_GRANTED;
  89.             }
  90.             if (self::IS_ANONYMOUS === $attribute && $this->authenticationTrustResolver->isAnonymous($token)) {
  91.                 trigger_deprecation('symfony/security-core''5.4''The "IS_ANONYMOUSLY" security attribute is deprecated, anonymous no longer exists in version 6.');
  92.                 return VoterInterface::ACCESS_GRANTED;
  93.             }
  94.             if (self::IS_IMPERSONATOR === $attribute && $token instanceof SwitchUserToken) {
  95.                 return VoterInterface::ACCESS_GRANTED;
  96.             }
  97.         }
  98.         return $result;
  99.     }
  100.     public function supportsAttribute(string $attribute): bool
  101.     {
  102.         return \in_array($attribute, [
  103.             self::IS_AUTHENTICATED_FULLY,
  104.             self::IS_AUTHENTICATED_REMEMBERED,
  105.             self::IS_AUTHENTICATED_ANONYMOUSLY,
  106.             self::IS_AUTHENTICATED,
  107.             self::IS_ANONYMOUS,
  108.             self::IS_IMPERSONATOR,
  109.             self::IS_REMEMBERED,
  110.             self::PUBLIC_ACCESS,
  111.         ], true);
  112.     }
  113.     public function supportsType(string $subjectType): bool
  114.     {
  115.         return true;
  116.     }
  117. }