Skip to content

Commit 6c522a7

Browse files
HeahDudewouterj
authored andcommitted
Added IS_ANONYMOUS, IS_REMEMBERED, IS_IMPERSONATOR
1 parent f01bbc7 commit 6c522a7

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/Symfony/Component/Security/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* Added access decision strategy to override access decisions by voter service priority
8+
* Added `IS_ANONYMOUS`, `IS_REMEMBERED`, `IS_IMPERSONATOR`
89

910
5.0.0
1011
-----

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Security\Core\Authorization\Voter;
1313

1414
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
15+
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
1516
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1617

1718
/**
@@ -28,6 +29,9 @@ class AuthenticatedVoter implements VoterInterface
2829
const IS_AUTHENTICATED_FULLY = 'IS_AUTHENTICATED_FULLY';
2930
const IS_AUTHENTICATED_REMEMBERED = 'IS_AUTHENTICATED_REMEMBERED';
3031
const IS_AUTHENTICATED_ANONYMOUSLY = 'IS_AUTHENTICATED_ANONYMOUSLY';
32+
const IS_ANONYMOUS = 'IS_ANONYMOUS';
33+
const IS_IMPERSONATOR = 'IS_IMPERSONATOR';
34+
const IS_REMEMBERED = 'IS_REMEMBERED';
3135

3236
private $authenticationTrustResolver;
3337

@@ -45,7 +49,10 @@ public function vote(TokenInterface $token, $subject, array $attributes)
4549
foreach ($attributes as $attribute) {
4650
if (null === $attribute || (self::IS_AUTHENTICATED_FULLY !== $attribute
4751
&& self::IS_AUTHENTICATED_REMEMBERED !== $attribute
48-
&& self::IS_AUTHENTICATED_ANONYMOUSLY !== $attribute)) {
52+
&& self::IS_AUTHENTICATED_ANONYMOUSLY !== $attribute
53+
&& self::IS_ANONYMOUS !== $attribute
54+
&& self::IS_IMPERSONATOR !== $attribute
55+
&& self::IS_REMEMBERED !== $attribute)) {
4956
continue;
5057
}
5158

@@ -68,6 +75,18 @@ public function vote(TokenInterface $token, $subject, array $attributes)
6875
|| $this->authenticationTrustResolver->isFullFledged($token))) {
6976
return VoterInterface::ACCESS_GRANTED;
7077
}
78+
79+
if (self::IS_REMEMBERED === $attribute && $this->authenticationTrustResolver->isRememberMe($token)) {
80+
return VoterInterface::ACCESS_GRANTED;
81+
}
82+
83+
if (self::IS_ANONYMOUS === $attribute && $this->authenticationTrustResolver->isAnonymous($token)) {
84+
return VoterInterface::ACCESS_GRANTED;
85+
}
86+
87+
if (self::IS_IMPERSONATOR === $attribute && $token instanceof SwitchUserToken) {
88+
return VoterInterface::ACCESS_GRANTED;
89+
}
7190
}
7291

7392
return $result;

src/Symfony/Component/Security/Core/Tests/Authorization/Voter/AuthenticatedVoterTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ public function getVoteTests()
4949
['fully', ['IS_AUTHENTICATED_FULLY'], VoterInterface::ACCESS_GRANTED],
5050
['remembered', ['IS_AUTHENTICATED_FULLY'], VoterInterface::ACCESS_DENIED],
5151
['anonymously', ['IS_AUTHENTICATED_FULLY'], VoterInterface::ACCESS_DENIED],
52+
53+
['fully', ['IS_ANONYMOUS'], VoterInterface::ACCESS_DENIED],
54+
['remembered', ['IS_ANONYMOUS'], VoterInterface::ACCESS_DENIED],
55+
['anonymously', ['IS_ANONYMOUS'], VoterInterface::ACCESS_GRANTED],
56+
57+
['fully', ['IS_IMPERSONATOR'], VoterInterface::ACCESS_DENIED],
58+
['remembered', ['IS_IMPERSONATOR'], VoterInterface::ACCESS_DENIED],
59+
['anonymously', ['IS_IMPERSONATOR'], VoterInterface::ACCESS_DENIED],
60+
['impersonated', ['IS_IMPERSONATOR'], VoterInterface::ACCESS_GRANTED],
5261
];
5362
}
5463

@@ -58,6 +67,8 @@ protected function getToken($authenticated)
5867
return $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
5968
} elseif ('remembered' === $authenticated) {
6069
return $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\RememberMeToken')->setMethods(['setPersistent'])->disableOriginalConstructor()->getMock();
70+
} elseif ('impersonated' === $authenticated) {
71+
return $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken')->disableOriginalConstructor()->getMock();
6172
} else {
6273
return $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\AnonymousToken')->setConstructorArgs(['', ''])->getMock();
6374
}

0 commit comments

Comments
 (0)