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, 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. 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 AuthenticationTrustResolverInterface $authenticationTrustResolver;
  32. public function __construct(AuthenticationTrustResolverInterface $authenticationTrustResolver)
  33. {
  34. $this->authenticationTrustResolver = $authenticationTrustResolver;
  35. }
  36. public function vote(TokenInterface $token, mixed $subject, array $attributes): int
  37. {
  38. if ($attributes === [self::PUBLIC_ACCESS]) {
  39. return VoterInterface::ACCESS_GRANTED;
  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 !== $attribute
  46. && self::IS_IMPERSONATOR !== $attribute
  47. && self::IS_REMEMBERED !== $attribute)) {
  48. continue;
  49. }
  50. $result = VoterInterface::ACCESS_DENIED;
  51. if (self::IS_AUTHENTICATED_FULLY === $attribute
  52. && $this->authenticationTrustResolver->isFullFledged($token)) {
  53. return VoterInterface::ACCESS_GRANTED;
  54. }
  55. if (self::IS_AUTHENTICATED_REMEMBERED === $attribute
  56. && ($this->authenticationTrustResolver->isRememberMe($token)
  57. || $this->authenticationTrustResolver->isFullFledged($token))) {
  58. return VoterInterface::ACCESS_GRANTED;
  59. }
  60. if (self::IS_AUTHENTICATED === $attribute && $this->authenticationTrustResolver->isAuthenticated($token)) {
  61. return VoterInterface::ACCESS_GRANTED;
  62. }
  63. if (self::IS_REMEMBERED === $attribute && $this->authenticationTrustResolver->isRememberMe($token)) {
  64. return VoterInterface::ACCESS_GRANTED;
  65. }
  66. if (self::IS_IMPERSONATOR === $attribute && $token instanceof SwitchUserToken) {
  67. return VoterInterface::ACCESS_GRANTED;
  68. }
  69. }
  70. return $result;
  71. }
  72. public function supportsAttribute(string $attribute): bool
  73. {
  74. return \in_array($attribute, [
  75. self::IS_AUTHENTICATED_FULLY,
  76. self::IS_AUTHENTICATED_REMEMBERED,
  77. self::IS_AUTHENTICATED,
  78. self::IS_IMPERSONATOR,
  79. self::IS_REMEMBERED,
  80. self::PUBLIC_ACCESS,
  81. ], true);
  82. }
  83. public function supportsType(string $subjectType): bool
  84. {
  85. return true;
  86. }
  87. }