16
16
use Symfony \Component \Security \Acl \Domain \SecurityIdentityRetrievalStrategy ;
17
17
use Symfony \Component \Security \Acl \Domain \UserSecurityIdentity ;
18
18
use Symfony \Component \Security \Acl \Tests \Fixtures \Account ;
19
+ use Symfony \Component \Security \Core \Authentication \AuthenticationTrustResolver ;
19
20
use Symfony \Component \Security \Core \Authentication \AuthenticationTrustResolverInterface ;
20
21
use Symfony \Component \Security \Core \Authentication \Token \AbstractToken ;
21
22
use Symfony \Component \Security \Core \Authentication \Token \AnonymousToken ;
22
23
use Symfony \Component \Security \Core \Authentication \Token \NullToken ;
24
+ use Symfony \Component \Security \Core \Authentication \Token \RememberMeToken ;
25
+ use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
26
+ use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
23
27
use Symfony \Component \Security \Core \Authorization \Voter \AuthenticatedVoter ;
28
+ use Symfony \Component \Security \Core \Authorization \Voter \CacheableVoterInterface ;
24
29
use Symfony \Component \Security \Core \Role \RoleHierarchyInterface ;
25
30
use Symfony \Component \Security \Core \User \UserInterface ;
26
31
@@ -31,33 +36,12 @@ class SecurityIdentityRetrievalStrategyTest extends TestCase
31
36
*/
32
37
public function testGetSecurityIdentities ($ user , array $ roles , string $ authenticationStatus , array $ sids )
33
38
{
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
-
39
+ $ token = $ this ->getToken ($ user , $ authenticationStatus , $ roles );
58
40
$ strategy = $ this ->getStrategy ($ roles , $ authenticationStatus );
59
41
$ extractedSids = $ strategy ->getSecurityIdentities ($ token );
60
42
43
+ $ this ->assertEquals ($ sids , $ extractedSids );
44
+
61
45
foreach ($ extractedSids as $ index => $ extractedSid ) {
62
46
if (!isset ($ sids [$ index ])) {
63
47
$ this ->fail (sprintf ('Expected SID at index %d, but there was none. ' , $ index ));
@@ -97,26 +81,25 @@ public function testDeprecatedGetSecurityIdentities($user, array $roles, string
97
81
$ strategy = $ this ->getStrategy ($ roles , $ authenticationStatus );
98
82
99
83
$ token
100
- ->expects ($ this ->once ())
101
84
->method ('getRoleNames ' )
102
85
->willReturn (['foo ' ])
103
86
;
104
87
105
88
if ('anonymous ' === $ authenticationStatus ) {
106
89
$ token
107
- ->expects ($ this ->never ())
108
90
->method ('getUser ' )
109
91
;
110
92
} else {
111
93
$ token
112
- ->expects ($ this ->once ())
113
94
->method ('getUser ' )
114
95
->willReturn ($ user )
115
96
;
116
97
}
117
98
118
99
$ extractedSids = $ strategy ->getSecurityIdentities ($ token );
119
100
101
+ $ this ->assertEquals ($ sids , $ extractedSids );
102
+
120
103
foreach ($ extractedSids as $ index => $ extractedSid ) {
121
104
if (!isset ($ sids [$ index ])) {
122
105
$ this ->fail (sprintf ('Expected SID at index %d, but there was none. ' , $ index ));
@@ -143,7 +126,7 @@ public function getSecurityIdentityRetrievalTests(): array
143
126
new RoleSecurityIdentity ('IS_AUTHENTICATED_FULLY ' ),
144
127
new RoleSecurityIdentity ('IS_AUTHENTICATED_REMEMBERED ' ),
145
128
], $ anonymousRoles )],
146
- [new CustomUserImpl ('johannes ' ), ['ROLE_FOO ' ], 'fullFledged ' , array_merge ([
129
+ [new CustomUserImpl ('johannes ' , [ ' ROLE_FOO ' ] ), ['ROLE_FOO ' ], 'fullFledged ' , array_merge ([
147
130
new UserSecurityIdentity ('johannes ' , CustomUserImpl::class),
148
131
new RoleSecurityIdentity ('ROLE_FOO ' ),
149
132
new RoleSecurityIdentity ('IS_AUTHENTICATED_FULLY ' ),
@@ -194,83 +177,34 @@ public function getReachableRoleNames(array $roles): array
194
177
}
195
178
};
196
179
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
- }
180
+ return new SecurityIdentityRetrievalStrategy ($ roleHierarchy , new AuthenticationTrustResolver ());
181
+ }
217
182
218
- if ('fullFledged ' === $ authenticationStatus ) {
219
- $ trustResolver
220
- ->expects ($ this ->once ())
221
- ->method ('isFullFledged ' )
222
- ->willReturn (true )
223
- ;
224
- $ trustResolver
225
- ->expects ($ this ->never ())
226
- ->method ('isRememberMe ' )
227
- ;
228
- } elseif ('rememberMe ' === $ authenticationStatus ) {
229
- $ trustResolver
230
- ->expects ($ this ->once ())
231
- ->method ('isFullFledged ' )
232
- ->willReturn (false )
233
- ;
234
- $ trustResolver
235
- ->expects ($ this ->once ())
236
- ->method ('isRememberMe ' )
237
- ->willReturn (true )
238
- ;
239
- } else {
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
- }
183
+ private function getToken ($ user , string $ authenticationStatus , array $ roles ): TokenInterface
184
+ {
185
+ if ('anonymous ' === $ authenticationStatus ) {
186
+ return class_exists (NullToken::class) ? new NullToken () : new AnonymousToken ('' , '' );
187
+ }
250
188
251
- $ trustResolver
252
- ->expects ($ this ->once ())
253
- ->method ('isFullFledged ' )
254
- ->willReturn (false )
255
- ;
256
- $ trustResolver
257
- ->expects ($ this ->once ())
258
- ->method ('isRememberMe ' )
259
- ->willReturn (false )
260
- ;
189
+ if ('rememberMe ' === $ authenticationStatus ) {
190
+ return new RememberMeToken ($ user , 'main ' , 'secret ' );
261
191
}
262
192
263
- return new SecurityIdentityRetrievalStrategy ($ roleHierarchy , $ trustResolver );
193
+ $ args = interface_exists (CacheableVoterInterface::class) ? [$ user , 'main ' , $ roles ] : [$ user , 'password ' , 'main ' , $ roles ];
194
+
195
+ return new UsernamePasswordToken (...$ args );
264
196
}
265
197
}
266
198
267
199
class CustomUserImpl implements UserInterface
268
200
{
269
201
protected $ name ;
202
+ protected $ roles = [];
270
203
271
- public function __construct ($ name )
204
+ public function __construct ($ name, $ roles )
272
205
{
273
206
$ this ->name = $ name ;
207
+ $ this ->roles = $ roles ;
274
208
}
275
209
276
210
public function __toString ()
@@ -280,7 +214,7 @@ public function __toString()
280
214
281
215
public function getRoles (): array
282
216
{
283
- return [] ;
217
+ return $ this -> roles ;
284
218
}
285
219
286
220
public function eraseCredentials ()
0 commit comments