Skip to content

Commit

Permalink
introduce CacheableVoterInterface to reduce amounts of calls
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpapst committed Apr 19, 2024
1 parent 3a2733e commit 2fa85af
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<array<mixed>>
*/
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<array<mixed>>
*/
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],
];
}
}

0 comments on commit 2fa85af

Please sign in to comment.