8000 bug #44096 Make ExpressionVoter Cacheable (jderusse) · symfony/symfony@8ca777b · GitHub
[go: up one dir, main page]

Skip to content

Commit 8ca777b

Browse files
committed
bug #44096 Make ExpressionVoter Cacheable (jderusse)
This PR was merged into the 5.4 branch. Discussion ---------- Make ExpressionVoter Cacheable | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - When making voters cacheable, I forgot to update the `ExpressionVoter`. The ExpressionVoter only supports attributes that are an instance of Expression. So, the Expression does not support any attribute of type string. FYI: When an attribute is not a string, the AccessDessision manager considers that all voters support it. Commits ------- 588e36d AddMake ExpressionVoter Cacheable
2 parents dd8c1dd + 588e36d commit 8ca777b

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