Skip to content

Commit 0649ea5

Browse files
authored
Introduce CacheableVoterInterface to reduce amounts of calls (#228)
1 parent 0898b40 commit 0649ea5

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/bundle/Security/Authorization/Voter/TwoFactorInProgressVoter.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
88
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
99
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
10+
use Symfony\Component\Security\Core\Authorization\Voter\CacheableVoterInterface;
1011
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
1112

1213
/**
1314
* @final
1415
*/
15-
class TwoFactorInProgressVoter implements VoterInterface
16+
class TwoFactorInProgressVoter implements CacheableVoterInterface
1617
{
1718
public const IS_AUTHENTICATED_2FA_IN_PROGRESS = 'IS_AUTHENTICATED_2FA_IN_PROGRESS';
1819

@@ -37,4 +38,14 @@ public function vote(TokenInterface $token, mixed $subject, array $attributes):
3738

3839
return VoterInterface::ACCESS_ABSTAIN;
3940
}
41+
42+
public function supportsAttribute(string $attribute): bool
43+
{
44+
return self::IS_AUTHENTICATED_2FA_IN_PROGRESS === $attribute || AuthenticatedVoter::PUBLIC_ACCESS === $attribute;
45+
}
46+
47+
public function supportsType(string $subjectType): bool
48+
{
49+
return true;
50+
}
4051
}

tests/Security/Authorization/Voter/TwoFactorInProgressVoterTest.php

+56
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
88
use Scheb\TwoFactorBundle\Security\Authorization\Voter\TwoFactorInProgressVoter;
99
use Scheb\TwoFactorBundle\Tests\TestCase;
10+
use Symfony\Component\HttpFoundation\Request;
1011
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1112
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
1213
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
14+
use Symfony\Component\Security\Core\User\UserInterface;
1315

1416
class TwoFactorInProgressVoterTest extends TestCase
1517
{
@@ -58,4 +60,58 @@ public static function provideAttributeAndExpectedResult(): array
5860
[TwoFactorInProgressVoter::IS_AUTHENTICATED_2FA_IN_PROGRESS, VoterInterface::ACCESS_GRANTED],
5961
];
6062
}
63+
64+
/**
65+
* @test
66+
* @dataProvider provideTypesForSupportCheck
67+
*/
68+
public function supports_type(string $checkType, bool $expectedResult): void
69+
{
70+
$returnValue = $this->voter->supportsType($checkType);
71+
$this->assertEquals($expectedResult, $returnValue);
72+
}
73+
74+
/**
75+
* @return array<array<mixed>>
76+
*/
77+
public static function provideTypesForSupportCheck(): array
78+
{
79+
return [
80+
[UserInterface::class, true],
81+
['any', true],
82+
['int', true],
83+
['array', true],
84+
['string', true],
85+
['null', true],
86+
[Request::class, true],
87+
];
88+
}
89+
90+
/**
91+
* @test
92+
* @dataProvider provideAttributesForSupportCheck
93+
*/
94+
public function supports_attribute(string $attribute, int $expectedResult): void
95+
{
96+
$returnValue = $this->voter->supportsAttribute($attribute);
97+
$this->assertEquals(VoterInterface::ACCESS_GRANTED === $expectedResult, $returnValue);
98+
}
99+
100+
/**
101+
* Copied from provideAttributeAndExpectedResult() but removed null.
102+
*
103+
* @return array<array<mixed>>
104+
*/
105+
public static function provideAttributesForSupportCheck(): array
106+
{
107+
return [
108+
['any', VoterInterface::ACCESS_ABSTAIN],
109+
[AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED, VoterInterface::ACCESS_ABSTAIN],
110+
[AuthenticatedVoter::IS_AUTHENTICATED_FULLY, VoterInterface::ACCESS_ABSTAIN],
111+
112+
// Granted
113+
[AuthenticatedVoter::PUBLIC_ACCESS, VoterInterface::ACCESS_GRANTED],
114+
[TwoFactorInProgressVoter::IS_AUTHENTICATED_2FA_IN_PROGRESS, VoterInterface::ACCESS_GRANTED],
115+
];
116+
}
61117
}

0 commit comments

Comments
 (0)