From 2fa85afe01da9c0d657618ae0c1cd2a42b1504f1 Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Fri, 19 Apr 2024 14:46:16 +0200 Subject: [PATCH] introduce CacheableVoterInterface to reduce amounts of calls --- .../Voter/TwoFactorInProgressVoter.php | 13 ++++- .../Voter/TwoFactorInProgressVoterTest.php | 54 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/bundle/Security/Authorization/Voter/TwoFactorInProgressVoter.php b/src/bundle/Security/Authorization/Voter/TwoFactorInProgressVoter.php index fb197f55..1329194f 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 $attribute === self::IS_AUTHENTICATED_2FA_IN_PROGRESS || $attribute === AuthenticatedVoter::PUBLIC_ACCESS; + } + + public function supportsType(string $subjectType): bool + { + return $subjectType === 'null'; + } } diff --git a/tests/Security/Authorization/Voter/TwoFactorInProgressVoterTest.php b/tests/Security/Authorization/Voter/TwoFactorInProgressVoterTest.php index 44ef717f..e031e07f 100644 --- a/tests/Security/Authorization/Voter/TwoFactorInProgressVoterTest.php +++ b/tests/Security/Authorization/Voter/TwoFactorInProgressVoterTest.php @@ -10,6 +10,7 @@ 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 +59,57 @@ 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, false], + ['any', false], + ['int', false], + ['array', false], + ['string', false], + ['null', true], + ]; + } + + /** + * @test + * @dataProvider provideAttributesForSupportCheck + */ + public function supports_attribute(string $attribute, int $expectedResult): void + { + $returnValue = $this->voter->supportsAttribute($attribute); + $this->assertEquals($expectedResult === VoterInterface::ACCESS_GRANTED, $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], + ]; + } }