11
11
12
12
namespace Symfony \Component \Security \Acl \Tests \Domain ;
13
13
14
- use PHPUnit \Framework \Assert ;
15
14
use PHPUnit \Framework \TestCase ;
16
15
use Symfony \Component \Security \Acl \Domain \RoleSecurityIdentity ;
17
16
use Symfony \Component \Security \Acl \Domain \SecurityIdentityRetrievalStrategy ;
20
19
use Symfony \Component \Security \Core \Authentication \AuthenticationTrustResolverInterface ;
21
20
use Symfony \Component \Security \Core \Authentication \Token \AbstractToken ;
22
21
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 ;
23
24
use Symfony \Component \Security \Core \Role \RoleHierarchyInterface ;
25
+ use Symfony \Component \Security \Core \User \UserInterface ;
24
26
25
27
class SecurityIdentityRetrievalStrategyTest extends TestCase
26
28
{
@@ -29,6 +31,54 @@ class SecurityIdentityRetrievalStrategyTest extends TestCase
29
31
*/
30
32
public function testGetSecurityIdentities ($ user , array $ roles , string $ authenticationStatus , array $ sids )
31
33
{
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
+
32
82
if ('anonymous ' === $ authenticationStatus ) {
33
83
$ token = $ this ->getMockBuilder (AnonymousToken::class)
34
84
->disableOriginalConstructor ()
@@ -69,50 +119,62 @@ public function testGetSecurityIdentities($user, array $roles, string $authentic
69
119
70
120
foreach ($ extractedSids as $ index => $ extractedSid ) {
71
121
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 ));
73
123
}
74
124
75
125
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 ));
77
127
}
78
128
}
79
129
}
80
130
81
131
public function getSecurityIdentityRetrievalTests (): array
82
132
{
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
+
83
138
return [
84
- [new Account ('johannes ' ), ['ROLE_USER ' , 'ROLE_SUPERADMIN ' ], 'fullFledged ' , [
139
+ [new Account ('johannes ' ), ['ROLE_USER ' , 'ROLE_SUPERADMIN ' ], 'fullFledged ' , array_merge ( [
85
140
new UserSecurityIdentity ('johannes ' , Account::class),
86
141
new RoleSecurityIdentity ('ROLE_USER ' ),
87
142
new RoleSecurityIdentity ('ROLE_SUPERADMIN ' ),
88
143
new RoleSecurityIdentity ('IS_AUTHENTICATED_FULLY ' ),
89
144
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 ([
100
147
new UserSecurityIdentity ('johannes ' , CustomUserImpl::class),
101
148
new RoleSecurityIdentity ('ROLE_FOO ' ),
102
149
new RoleSecurityIdentity ('IS_AUTHENTICATED_FULLY ' ),
103
150
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 ([
107
153
new UserSecurityIdentity ('foo ' , Account::class),
108
154
new RoleSecurityIdentity ('ROLE_FOO ' ),
109
155
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 ([
113
176
new RoleSecurityIdentity ('ROLE_FOO ' ),
114
- new RoleSecurityIdentity ('IS_AUTHENTICATED_ANONYMOUSLY ' ),
115
- ]],
177
+ ], $ anonymousRoles )],
116
178
];
117
179
}
118
180
@@ -128,18 +190,30 @@ public function __construct(array $roles)
128
190
129
191
public function getReachableRoleNames (array $ roles ): array
130
192
{
131
- Assert::assertSame (['foo ' ], $ roles );
132
-
133
193
return $ this ->roles ;
134
194
}
135
195
};
136
196
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
+ }
143
217
144
218
if ('fullFledged ' === $ authenticationStatus ) {
145
219
$ trustResolver
@@ -163,10 +237,17 @@ public function getReachableRoleNames(array $roles): array
163
237
->willReturn (true )
164
238
;
165
239
} 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
+
170
251
$ trustResolver
171
252
->expects ($ this ->once ())
172
253
->method ('isFullFledged ' )
@@ -183,7 +264,7 @@ public function getReachableRoleNames(array $roles): array
183
264
}
184
265
}
185
266
186
- class CustomUserImpl
267
+ class CustomUserImpl implements UserInterface
187
268
{
188
269
protected $ name ;
189
270
@@ -196,4 +277,33 @@ public function __toString()
196
277
{
197
278
return $ this ->name ;
198
279
}
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
+ }
199
309
}
0 commit comments