vendor/symfony/security/Core/Authorization/Voter/AuthenticatedVoter.php line 26

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