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, or IS_AUTHENTICATED_ANONYMOUSLY 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 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.     public const PUBLIC_ACCESS 'PUBLIC_ACCESS';
  32.     private $authenticationTrustResolver;
  33.     public function __construct(AuthenticationTrustResolverInterface $authenticationTrustResolver)
  34.     {
  35.         $this->authenticationTrustResolver $authenticationTrustResolver;
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      */
  40.     public function vote(TokenInterface $token$subject, array $attributes)
  41.     {
  42.         if ($attributes === [self::PUBLIC_ACCESS]) {
  43.             return VoterInterface::ACCESS_GRANTED;
  44.         }
  45.         $result VoterInterface::ACCESS_ABSTAIN;
  46.         foreach ($attributes as $attribute) {
  47.             if (null === $attribute || (self::IS_AUTHENTICATED_FULLY !== $attribute
  48.                     && self::IS_AUTHENTICATED_REMEMBERED !== $attribute
  49.                     && self::IS_AUTHENTICATED_ANONYMOUSLY !== $attribute
  50.                     && self::IS_ANONYMOUS !== $attribute
  51.                     && self::IS_IMPERSONATOR !== $attribute
  52.                     && self::IS_REMEMBERED !== $attribute)) {
  53.                 continue;
  54.             }
  55.             $result VoterInterface::ACCESS_DENIED;
  56.             if (self::IS_AUTHENTICATED_FULLY === $attribute
  57.                 && $this->authenticationTrustResolver->isFullFledged($token)) {
  58.                 return VoterInterface::ACCESS_GRANTED;
  59.             }
  60.             if (self::IS_AUTHENTICATED_REMEMBERED === $attribute
  61.                 && ($this->authenticationTrustResolver->isRememberMe($token)
  62.                     || $this->authenticationTrustResolver->isFullFledged($token))) {
  63.                 return VoterInterface::ACCESS_GRANTED;
  64.             }
  65.             if (self::IS_AUTHENTICATED_ANONYMOUSLY === $attribute
  66.                 && ($this->authenticationTrustResolver->isAnonymous($token)
  67.                     || $this->authenticationTrustResolver->isRememberMe($token)
  68.                     || $this->authenticationTrustResolver->isFullFledged($token))) {
  69.                 return VoterInterface::ACCESS_GRANTED;
  70.             }
  71.             if (self::IS_REMEMBERED === $attribute && $this->authenticationTrustResolver->isRememberMe($token)) {
  72.                 return VoterInterface::ACCESS_GRANTED;
  73.             }
  74.             if (self::IS_ANONYMOUS === $attribute && $this->authenticationTrustResolver->isAnonymous($token)) {
  75.                 return VoterInterface::ACCESS_GRANTED;
  76.             }
  77.             if (self::IS_IMPERSONATOR === $attribute && $token instanceof SwitchUserToken) {
  78.                 return VoterInterface::ACCESS_GRANTED;
  79.             }
  80.         }
  81.         return $result;
  82.     }
  83. }