From 0649ea52c02158da409164514e8b81b9288c29c6 Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Sat, 20 Apr 2024 16:21:34 +0200 Subject: [PATCH] Introduce CacheableVoterInterface to reduce amounts of calls (#228) --- .../Voter/TwoFactorInProgressVoter.php | 13 ++++- .../Voter/TwoFactorInProgressVoterTest.php | 56 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/bundle/Security/Authorization/Voter/TwoFactorInProgressVoter.php b/src/bundle/Security/Authorization/Voter/TwoFactorInProgressVoter.php index fb197f55..2ca3f67f 100644 --- a/src/bundle/Security/Authorization/Voter/TwoFactorInProgressVoter.php +++ b/src/bundle/Security/Authorization/Voter/TwoFactorInProgressVoter.php @@ -7,12 +7,13 @@ use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter; +use Symfony\Component\Security\Core\Authorization\Voter\CacheableVoterInterface; use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; /** * @final */ -class TwoFactorInProgressVoter implements VoterInterface +class TwoFactorInProgressVoter implements CacheableVoterInterface { public const IS_AUTHENTICATED_2FA_IN_PROGRESS = 'IS_AUTHENTICATED_2FA_IN_PROGRESS'; @@ -37,4 +38,14 @@ public function vote(TokenInterface $token, mixed $subject, array $attributes): return VoterInterface::ACCESS_ABSTAIN; } + + public function supportsAttribute(string $attribute): bool + { + return self::IS_AUTHENTICATED_2FA_IN_PROGRESS === $attribute || AuthenticatedVoter::PUBLIC_ACCESS === $attribute; + } + + public function supportsType(string $subjectType): bool + { + return true; + } } diff --git a/tests/Security/Authorization/Voter/TwoFactorInProgressVoterTest.php b/tests/Security/Authorization/Voter/TwoFactorInProgressVoterTest.php index 44ef717f..40c4a450 100644 --- a/tests/Security/Authorization/Voter/TwoFactorInProgressVoterTest.php +++ b/tests/Security/Authorization/Voter/TwoFactorInProgressVoterTest.php @@ -7,9 +7,11 @@ use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface; use Scheb\TwoFactorBundle\Security\Authorization\Voter\TwoFactorInProgressVoter; use Scheb\TwoFactorBundle\Tests\TestCase; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter; use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; +use Symfony\Component\Security\Core\User\UserInterface; class TwoFactorInProgressVoterTest extends TestCase { @@ -58,4 +60,58 @@ public static function provideAttributeAndExpectedResult(): array [TwoFactorInProgressVoter::IS_AUTHENTICATED_2FA_IN_PROGRESS, VoterInterface::ACCESS_GRANTED], ]; } + + /** + * @test + * @dataProvider provideTypesForSupportCheck + */ + public function supports_type(string $checkType, bool $expectedResult): void + { + $returnValue = $this->voter->supportsType($checkType); + $this->assertEquals($expectedResult, $returnValue); + } + + /** + * @return array> + */ + public static function provideTypesForSupportCheck(): array + { + return [ + [UserInterface::class, true], + ['any', true], + ['int', true], + ['array', true], + ['string', true], + ['null', true], + [Request::class, true], + ]; + } + + /** + * @test + * @dataProvider provideAttributesForSupportCheck + */ + public function supports_attribute(string $attribute, int $expectedResult): void + { + $returnValue = $this->voter->supportsAttribute($attribute); + $this->assertEquals(VoterInterface::ACCESS_GRANTED === $expectedResult, $returnValue); + } + + /** + * Copied from provideAttributeAndExpectedResult() but removed null. + * + * @return array> + */ + public static function provideAttributesForSupportCheck(): array + { + return [ + ['any', VoterInterface::ACCESS_ABSTAIN], + [AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED, VoterInterface::ACCESS_ABSTAIN], + [AuthenticatedVoter::IS_AUTHENTICATED_FULLY, VoterInterface::ACCESS_ABSTAIN], + + // Granted + [AuthenticatedVoter::PUBLIC_ACCESS, VoterInterface::ACCESS_GRANTED], + [TwoFactorInProgressVoter::IS_AUTHENTICATED_2FA_IN_PROGRESS, VoterInterface::ACCESS_GRANTED], + ]; + } }