Skip to content

Commit 3492ce6

Browse files
acrobatwouterj
authored andcommitted
Fix failing tests on symfony 6
1 parent 5855e09 commit 3492ce6

File tree

2 files changed

+170
-40
lines changed

2 files changed

+170
-40
lines changed

Domain/SecurityIdentityRetrievalStrategy.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Security\Acl\Model\SecurityIdentityRetrievalStrategyInterface;
1515
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
1616
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
17+
use Symfony\Component\Security\Core\Authentication\Token\NullToken;
1718
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1819
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
1920
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
@@ -39,13 +40,15 @@ public function __construct(RoleHierarchyInterface $roleHierarchy, Authenticatio
3940

4041
/**
4142
* {@inheritdoc}
43+
*
44+
* @return RoleSecurityIdentity[]
4245
*/
4346
public function getSecurityIdentities(TokenInterface $token)
4447
{
4548
$sids = [];
4649

4750
// add user security identity
48-
if (!$token instanceof AnonymousToken) {
51+
if (!$token instanceof AnonymousToken && !$token instanceof NullToken) {
4952
try {
5053
$sids[] = UserSecurityIdentity::fromToken($token);
5154
} catch (\InvalidArgumentException $e) {
@@ -62,14 +65,31 @@ public function getSecurityIdentities(TokenInterface $token)
6265
if ($this->authenticationTrustResolver->isFullFledged($token)) {
6366
$sids[] = new RoleSecurityIdentity(AuthenticatedVoter::IS_AUTHENTICATED_FULLY);
6467
$sids[] = new RoleSecurityIdentity(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED);
65-
$sids[] = new RoleSecurityIdentity(AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY);
68+
$this->addAnonymousRoles($sids);
6669
} elseif ($this->authenticationTrustResolver->isRememberMe($token)) {
6770
$sids[] = new RoleSecurityIdentity(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED);
68-
$sids[] = new RoleSecurityIdentity(AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY);
69-
} elseif ($this->authenticationTrustResolver->isAnonymous($token)) {
70-
$sids[] = new RoleSecurityIdentity(AuthenticatedVoter::IS_AUTHENTICATED_ANONYMOUSLY);
71+
$this->addAnonymousRoles($sids);
72+
} elseif ($this->isNotAuthenticated($token)) {
73+
$this->addAnonymousRoles($sids);
7174
}
7275

7376
return $sids;
7477
}
78+
79+
private function isNotAuthenticated(TokenInterface $token): bool
80+
{
81+
if (\defined('\Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter::PUBLIC_ACCESS')) {
82+
return !$this->authenticationTrustResolver->isAuthenticated($token);
83+
}
84+
85+
return $this->authenticationTrustResolver->isAnonymous($token);
86+
}
87+
88+
private function addAnonymousRoles(array &$sids)
89+
{
90+
$sids[] = new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY');
91+
if (\defined('\Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter::PUBLIC_ACCESS')) {
92+
$sids[] = new RoleSecurityIdentity(AuthenticatedVoter::PUBLIC_ACCESS);
93+
}
94+
}
7595
}

Tests/Domain/SecurityIdentityRetrievalStrategyTest.php

Lines changed: 145 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Security\Acl\Tests\Domain;
1313

14-
use PHPUnit\Framework\Assert;
1514
use PHPUnit\Framework\TestCase;
1615
use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
1716
use Symfony\Component\Security\Acl\Domain\SecurityIdentityRetrievalStrategy;
@@ -20,7 +19,10 @@
2019
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
2120
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
2221
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
22+
use Symfony\Component\Security\Core\Authentication\Token\NullToken;
23+
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
2324
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
25+
use Symfony\Component\Security\Core\User\UserInterface;
2426

2527
class SecurityIdentityRetrievalStrategyTest extends TestCase
2628
{
@@ -29,6 +31,54 @@ class SecurityIdentityRetrievalStrategyTest extends TestCase
2931
*/
3032
public function testGetSecurityIdentities($user, array $roles, string $authenticationStatus, array $sids)
3133
{
34+
$token = class_exists(NullToken::class) ? new NullToken() : new AnonymousToken('', '');
35+
if ('anonymous' !== $authenticationStatus) {
36+
$class = '';
37+
if (\is_string($user)) {
38+
$class = 'MyCustomTokenImpl';
39+
}
40+
41+
$token = $this->getMockBuilder(AbstractToken::class)
42+
->setMockClassName($class)
43+
->getMock();
44+
45+
$token
46+
->expects($this->once())
47+
->method('getRoleNames')
48+
->willReturn(['foo'])
49+
;
50+
51+
$token
52+
->expects($this->once())
53+
->method('getUser')
54+
->willReturn($user)
55+
;
56+
}
57+
58+
$strategy = $this->getStrategy($roles, $authenticationStatus);
59+
$extractedSids = $strategy->getSecurityIdentities($token);
60+
61+
foreach ($extractedSids as $index => $extractedSid) {
62+
if (!isset($sids[$index])) {
63+
$this->fail(sprintf('Expected SID at index %d, but there was none.', $index));
64+
}
65+
66+
if (false === $sids[$index]->equals($extractedSid)) {
67+
$this->fail(sprintf('Index: %d, expected SID "%s", but got "%s".', $index, $sids[$index], (string) $extractedSid));
68+
}
69+
}
70+
}
71+
72+
/**
73+
* @group legacy
74+
* @dataProvider getDeprecatedSecurityIdentityRetrievalTests
75+
*/
76+
public function testDeprecatedGetSecurityIdentities($user, array $roles, string $authenticationStatus, array $sids)
77+
{
78+
if (method_exists(AuthenticationTrustResolverInterface::class, 'isAuthenticated')) {
79+
$this->markTestSkipped();
80+
}
81+
3282
if ('anonymous' === $authenticationStatus) {
3383
$token = $this->getMockBuilder(AnonymousToken::class)
3484
->disableOriginalConstructor()
@@ -69,50 +119,62 @@ public function testGetSecurityIdentities($user, array $roles, string $authentic
69119

70120
foreach ($extractedSids as $index => $extractedSid) {
71121
if (!isset($sids[$index])) {
72-
$this->fail(sprintf('Expected SID at index %d, but there was none.', true));
122+
$this->fail(sprintf('Expected SID at index %d, but there was none.', $index));
73123
}
74124

75125
if (false === $sids[$index]->equals($extractedSid)) {
76-
$this->fail(sprintf('Index: %d, expected SID "%s", but got "%s".', $index, $sids[$index], $extractedSid));
126+
$this->fail(sprintf('Index: %d, expected SID "%s", but got "%s".', $index, $sids[$index], (string) $extractedSid));
77127
}
78128
}
79129
}
80130

81131
public function getSecurityIdentityRetrievalTests(): array
82132
{
133+
$anonymousRoles = [new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY')];
134+
if (\defined('\Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter::PUBLIC_ACCESS')) {
135+
$anonymousRoles[] = new RoleSecurityIdentity(AuthenticatedVoter::PUBLIC_ACCESS);
136+
}
137+
83138
return [
84-
[new Account('johannes'), ['ROLE_USER', 'ROLE_SUPERADMIN'], 'fullFledged', [
139+
[new Account('johannes'), ['ROLE_USER', 'ROLE_SUPERADMIN'], 'fullFledged', array_merge([
85140
new UserSecurityIdentity('johannes', Account::class),
86141
new RoleSecurityIdentity('ROLE_USER'),
87142
new RoleSecurityIdentity('ROLE_SUPERADMIN'),
88143
new RoleSecurityIdentity('IS_AUTHENTICATED_FULLY'),
89144
new RoleSecurityIdentity('IS_AUTHENTICATED_REMEMBERED'),
90-
new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY'),
91-
]],
92-
['johannes', ['ROLE_FOO'], 'fullFledged', [
93-
new UserSecurityIdentity('johannes', 'MyCustomTokenImpl'),
94-
new RoleSecurityIdentity('ROLE_FOO'),
95-
new RoleSecurityIdentity('IS_AUTHENTICATED_FULLY'),
96-
new RoleSecurityIdentity('IS_AUTHENTICATED_REMEMBERED'),
97-
new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY'),
98-
]],
99-
[new CustomUserImpl('johannes'), ['ROLE_FOO'], 'fullFledged', [
145+
], $anonymousRoles)],
146+
[new CustomUserImpl('johannes'), ['ROLE_FOO'], 'fullFledged', array_merge([
100147
new UserSecurityIdentity('johannes', CustomUserImpl::class),
101148
new RoleSecurityIdentity('ROLE_FOO'),
102149
new RoleSecurityIdentity('IS_AUTHENTICATED_FULLY'),
103150
new RoleSecurityIdentity('IS_AUTHENTICATED_REMEMBERED'),
104-
new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY'),
105-
]],
106-
[new Account('foo'), ['ROLE_FOO'], 'rememberMe', [
151+
], $anonymousRoles)],
152+
[new Account('foo'), ['ROLE_FOO'], 'rememberMe', array_merge([
107153
new UserSecurityIdentity('foo', Account::class),
108154
new RoleSecurityIdentity('ROLE_FOO'),
109155
new RoleSecurityIdentity('IS_AUTHENTICATED_REMEMBERED'),
110-
new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY'),
111-
]],
112-
['guest', ['ROLE_FOO'], 'anonymous', [
156+
], $anonymousRoles)],
157+
['guest', [], 'anonymous', $anonymousRoles],
158+
];
159+
}
160+
161+
public function getDeprecatedSecurityIdentityRetrievalTests()
162+
{
163+
$anonymousRoles = [new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY')];
164+
if (\defined('\Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter::PUBLIC_ACCESS')) {
165+
$anonymousRoles[] = new RoleSecurityIdentity(AuthenticatedVoter::PUBLIC_ACCESS);
166+
}
167+
168+
return [
169+
['johannes', ['ROLE_FOO'], 'fullFledged', array_merge([
170+
new UserSecurityIdentity('johannes', 'MyCustomTokenImpl'),
171+
new RoleSecurityIdentity('ROLE_FOO'),
172+
new RoleSecurityIdentity('IS_AUTHENTICATED_FULLY'),
173+
new RoleSecurityIdentity('IS_AUTHENTICATED_REMEMBERED'),
174+
], $anonymousRoles)],
175+
['guest', ['ROLE_FOO'], 'anonymous', array_merge([
113176
new RoleSecurityIdentity('ROLE_FOO'),
114-
new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY'),
115-
]],
177+
], $anonymousRoles)],
116178
];
117179
}
118180

@@ -128,18 +190,30 @@ public function __construct(array $roles)
128190

129191
public function getReachableRoleNames(array $roles): array
130192
{
131-
Assert::assertSame(['foo'], $roles);
132-
133193
return $this->roles;
134194
}
135195
};
136196

137-
$trustResolver = $this->createMock(AuthenticationTrustResolverInterface::class);
138-
139-
$trustResolver
140-
->method('isAnonymous')
141-
->willReturn('anonymous' === $authenticationStatus)
142-
;
197+
$trustResolverMockBuild = $this->getMockBuilder(AuthenticationTrustResolverInterface::class);
198+
if (\defined('\Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter::PUBLIC_ACCESS')) {
199+
if (method_exists(AuthenticationTrustResolverInterface::class, 'isAuthenticated')) {
200+
$trustResolver = $trustResolverMockBuild->getMock();
201+
} else {
202+
$trustResolver = $trustResolverMockBuild
203+
->onlyMethods(['isAnonymous', 'isRememberMe', 'isFullFledged'])
204+
->addMethods(['isAuthenticated'])
205+
->getMock()
206+
;
207+
}
208+
$trustResolver
209+
->method('isAuthenticated')
210+
->willReturn('anonymous' !== $authenticationStatus);
211+
} else {
212+
$trustResolver = $trustResolverMockBuild->getMock();
213+
$trustResolver
214+
->method('isAnonymous')
215+
->willReturn('anonymous' === $authenticationStatus);
216+
}
143217

144218
if ('fullFledged' === $authenticationStatus) {
145219
$trustResolver
@@ -163,10 +237,17 @@ public function getReachableRoleNames(array $roles): array
163237
->willReturn(true)
164238
;
165239
} else {
166-
$trustResolver
167-
->method('isAnonymous')
168-
->willReturn(true)
169-
;
240+
if (method_exists(AuthenticationTrustResolverInterface::class, 'isAuthenticated')) {
241+
$trustResolver
242+
->method('isAuthenticated')
243+
->willReturn(false)
244+
;
245+
} else {
246+
$trustResolver
247+
->method('isAnonymous')
248+
->willReturn(true);
249+
}
250+
170251
$trustResolver
171252
->expects($this->once())
172253
->method('isFullFledged')
@@ -183,7 +264,7 @@ public function getReachableRoleNames(array $roles): array
183264
}
184265
}
185266

186-
class CustomUserImpl
267+
class CustomUserImpl implements UserInterface
187268
{
188269
protected $name;
189270

@@ -196,4 +277,33 @@ public function __toString()
196277
{
197278
return $this->name;
198279
}
280+
281+
public function getRoles(): array
282+
{
283+
return [];
284+
}
285+
286+
public function eraseCredentials()
287+
{
288+
}
289+
290+
public function getUserIdentifier(): string
291+
{
292+
return $this->name;
293+
}
294+
295+
public function getPassword()
296+
{
297+
return null;
298+
}
299+
300+
public function getSalt()
301+
{
302+
return null;
303+
}
304+
305+
public function getUsername(): string
306+
{
307+
return $this->getUserIdentifier();
308+
}
199309
}

0 commit comments

Comments
 (0)