Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce CacheableVoterInterface to reduce amounts of calls #228

Merged
merged 5 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'phpdoc_separation' => ['groups' => [['test', 'dataProvider']]],
'nullable_type_declaration_for_default_null_value' => true,
'no_superfluous_phpdoc_tags' => ['remove_inheritdoc' => false],
'nullable_type_declaration' => false,
];

$finder = PhpCsFixer\Finder::create()
Expand Down
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],
];
}
}
Loading