vendor/scheb/2fa-bundle/Security/Authorization/Voter/TwoFactorInProgressVoter.php line 16

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Scheb\TwoFactorBundle\Security\Authorization\Voter;
  4. use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
  7. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  8. use function defined;
  9. /**
  10. * @final
  11. */
  12. class TwoFactorInProgressVoter implements VoterInterface
  13. {
  14. public const IS_AUTHENTICATED_2FA_IN_PROGRESS = 'IS_AUTHENTICATED_2FA_IN_PROGRESS';
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function vote(TokenInterface $token, mixed $subject, array $attributes): int
  19. {
  20. if (!($token instanceof TwoFactorTokenInterface)) {
  21. return VoterInterface::ACCESS_ABSTAIN;
  22. }
  23. foreach ($attributes as $attribute) {
  24. if (self::IS_AUTHENTICATED_2FA_IN_PROGRESS === $attribute) {
  25. return VoterInterface::ACCESS_GRANTED;
  26. }
  27. if (AuthenticatedVoter::PUBLIC_ACCESS === $attribute) {
  28. return VoterInterface::ACCESS_GRANTED;
  29. }
  30. // Compatibility for Symfony < 6.0
  31. /** @psalm-suppress UndefinedConstant */
  32. if (
  33. defined(AuthenticatedVoter::class.'::IS_AUTHENTICATED_ANONYMOUSLY')
  34. && AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY === $attribute
  35. ) {
  36. return VoterInterface::ACCESS_GRANTED;
  37. }
  38. }
  39. return VoterInterface::ACCESS_ABSTAIN;
  40. }
  41. }