Skip to content

Commit

Permalink
Introduce CacheableVoterInterface to reduce amounts of calls (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpapst authored Apr 20, 2024
1 parent 0898b40 commit 0649ea5
Show file tree
Hide file tree
Showing 2 changed files with 68 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 self::IS_AUTHENTICATED_2FA_IN_PROGRESS === $attribute || AuthenticatedVoter::PUBLIC_ACCESS === $attribute;
}

public function supportsType(string $subjectType): bool
{
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<array<mixed>>
*/
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<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 0649ea5

Please sign in to comment.