8000 AddMake ExpressionVoter Cacheable · symfony/symfony@588e36d · GitHub
[go: up one dir, main page]

Skip to content

Commit 588e36d

Browse files
committed
AddMake ExpressionVoter Cacheable
1 parent dd8c1dd commit 588e36d

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/Symfony/Component/Security/Core/Authorization/Voter/ExpressionVoter.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*
2525
* @author Fabien Potencier <fabien@symfony.com>
2626
*/
27-
class ExpressionVoter implements VoterInterface
27+
class ExpressionVoter implements CacheableVoterInterface
2828
{
2929
private $expressionLanguage;
3030
private $trustResolver;
@@ -39,6 +39,16 @@ public function __construct(ExpressionLanguage $expressionLanguage, Authenticati
3939
$this->roleHierarchy = $roleHierarchy;
4040
}
4141

42+
public function supportsAttribute(string $attribute): bool
43+
{
44+
return false;
45+
}
46+
47+
public function supportsType(string $subjectType): bool
48+
{
49+
return true;
50+
}
51+
4252
/**
4353
* {@inheritdoc}
4454
*/

src/Symfony/Component/Security/Core/Tests/Authorization/AccessDecisionManagerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,31 @@ public function testCacheableVotersNotCalled()
313313
$this->assertFalse($manager->decide($token, ['foo'], 'bar'));
314314
}
315315

316+
public function testCacheableVotersWithMultipleAttributesAndNonString()
317+
{
318+
$token = $this->createMock(TokenInterface::class);
319+
$voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass();
320+
$voter
321+
->expects($this->once())
322+
->method('supportsAttribute')
323+
->with('foo')
324+
->willReturn(false);
325+
$voter
326+
// Voter does not support "foo", but given 1337 is not a string, it implicitly supports it.
327+
->expects($this->once())
328+
->method('supportsType')
329+
->with('string')
330+
->willReturn(true);
331+
$voter
332+
->expects($this->once())
333+
->method('vote')
334+
->with($token, 'bar', ['foo', 1337])
335+
->willReturn(VoterInterface::ACCESS_GRANTED);
336+
337+
$manager = new AccessDecisionManager([$voter]);
338+
$this->assertTrue($manager->decide($token, ['foo', 1337], 'bar', true));
339+
}
340+
316341
protected function getVoters($grants, $denies, $abstains)
317342
{
318343
$voters = [];

0 commit comments

Comments
 (0)
0