1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the RoleServiceAuthorizationTest class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\API\Repository\Tests; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\Values\User\Limitation\SubtreeLimitation; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Test case for operations in the RoleService using in memory storage. |
15
|
|
|
* |
16
|
|
|
* @see eZ\Publish\API\Repository\RoleService |
17
|
|
|
* @group integration |
18
|
|
|
* @group authorization |
19
|
|
|
*/ |
20
|
|
|
class RoleServiceAuthorizationTest extends BaseTest |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Test for the createRole() method. |
24
|
|
|
* |
25
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::createRole() |
26
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testCreateRole |
27
|
|
|
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
28
|
|
|
*/ |
29
|
|
|
public function testCreateRoleThrowsUnauthorizedException() |
30
|
|
|
{ |
31
|
|
|
$this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
32
|
|
|
|
33
|
|
|
$repository = $this->getRepository(); |
34
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
35
|
|
|
|
36
|
|
|
/* BEGIN: Use Case */ |
37
|
|
|
$user = $this->createUserVersion1(); |
38
|
|
|
|
39
|
|
|
// Set "Editor" user as current user. |
40
|
|
|
$permissionResolver->setCurrentUserReference($user); |
41
|
|
|
|
42
|
|
|
// Get the role service |
43
|
|
|
$roleService = $repository->getRoleService(); |
44
|
|
|
|
45
|
|
|
// Instantiate a role create struct. |
46
|
|
|
$roleCreate = $roleService->newRoleCreateStruct('roleName'); |
47
|
|
|
|
48
|
|
|
// This call will fail with an "UnauthorizedException" |
49
|
|
|
$roleService->createRole($roleCreate); |
50
|
|
|
/* END: Use Case */ |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Test for the loadRole() method. |
55
|
|
|
* |
56
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::loadRole() |
57
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRole |
58
|
|
|
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
59
|
|
|
*/ |
60
|
|
|
public function testLoadRoleThrowsUnauthorizedException() |
61
|
|
|
{ |
62
|
|
|
$this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
63
|
|
|
|
64
|
|
|
$repository = $this->getRepository(); |
65
|
|
|
$roleService = $repository->getRoleService(); |
66
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
67
|
|
|
|
68
|
|
|
/* BEGIN: Use Case */ |
69
|
|
|
$user = $this->createUserVersion1(); |
70
|
|
|
|
71
|
|
|
$role = $this->createRole(); |
72
|
|
|
|
73
|
|
|
// Set "Editor" user as current user. |
74
|
|
|
$permissionResolver->setCurrentUserReference($user); |
75
|
|
|
|
76
|
|
|
// This call will fail with an "UnauthorizedException" |
77
|
|
|
$roleService->loadRole($role->id); |
78
|
|
|
/* END: Use Case */ |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Test for the loadRoleByIdentifier() method. |
83
|
|
|
* |
84
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::loadRoleByIdentifier() |
85
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testLoadRoleByIdentifier |
86
|
|
|
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
87
|
|
|
*/ |
88
|
|
|
public function testLoadRoleByIdentifierThrowsUnauthorizedException() |
89
|
|
|
{ |
90
|
|
|
$this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
91
|
|
|
|
92
|
|
|
$repository = $this->getRepository(); |
93
|
|
|
$roleService = $repository->getRoleService(); |
94
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
95
|
|
|
|
96
|
|
|
/* BEGIN: Use Case */ |
97
|
|
|
$user = $this->createUserVersion1(); |
98
|
|
|
|
99
|
|
|
$role = $this->createRole(); |
100
|
|
|
|
101
|
|
|
// Set "Editor" user as current user. |
102
|
|
|
$permissionResolver->setCurrentUserReference($user); |
103
|
|
|
|
104
|
|
|
// This call will fail with an "UnauthorizedException" |
105
|
|
|
$roleService->loadRoleByIdentifier($role->identifier); |
106
|
|
|
/* END: Use Case */ |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Test for the loadRoles() method. |
111
|
|
|
* |
112
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::loadRoles() |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public function testLoadRolesLoadsEmptyListForAnonymousUser() |
115
|
|
|
{ |
116
|
|
|
$repository = $this->getRepository(); |
117
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
118
|
|
|
|
119
|
|
|
/* BEGIN: Use Case */ |
120
|
|
|
$user = $this->createUserVersion1(); |
121
|
|
|
|
122
|
|
|
// Set "Editor" user as current user. |
123
|
|
|
$permissionResolver->setCurrentUserReference($user); |
124
|
|
|
|
125
|
|
|
// Get the role service |
126
|
|
|
$roleService = $repository->getRoleService(); |
127
|
|
|
/* END: Use Case */ |
128
|
|
|
|
129
|
|
|
$this->assertEquals([], $roleService->loadRoles()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Test for the loadRoles() method. |
134
|
|
|
* |
135
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::loadRoles() |
136
|
|
|
*/ |
137
|
|
|
public function testLoadRolesForUserWithSubtreeLimitation() |
138
|
|
|
{ |
139
|
|
|
$repository = $this->getRepository(); |
140
|
|
|
$roleService = $repository->getRoleService(); |
141
|
|
|
|
142
|
|
|
/* BEGIN: Use Case */ |
143
|
|
|
// create user that can read/create/delete but cannot edit or content |
144
|
|
|
$this->createRoleWithPolicies('roleReader', [ |
145
|
|
|
['module' => 'role', 'function' => 'read'], |
146
|
|
|
]); |
147
|
|
|
|
148
|
|
|
$user = $this->createCustomUserWithLogin( |
149
|
|
|
'user', |
150
|
|
|
'[email protected]', |
151
|
|
|
'roleReaders', |
152
|
|
|
'roleReader', |
153
|
|
|
new SubtreeLimitation(['limitationValues' => ['/1/2/']]) |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
$repository->getPermissionResolver()->setCurrentUserReference($user); |
157
|
|
|
/* END: Use Case */ |
158
|
|
|
|
159
|
|
|
$this->assertCount(6, $roleService->loadRoles()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Test for the deleteRole() method. |
164
|
|
|
* |
165
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::deleteRole() |
166
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testDeleteRole |
167
|
|
|
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
168
|
|
|
*/ |
169
|
|
|
public function testDeleteRoleThrowsUnauthorizedException() |
170
|
|
|
{ |
171
|
|
|
$this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
172
|
|
|
|
173
|
|
|
$repository = $this->getRepository(); |
174
|
|
|
$roleService = $repository->getRoleService(); |
175
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
176
|
|
|
|
177
|
|
|
/* BEGIN: Use Case */ |
178
|
|
|
$user = $this->createUserVersion1(); |
179
|
|
|
|
180
|
|
|
$role = $this->createRole(); |
181
|
|
|
|
182
|
|
|
// Set "Editor" user as current user. |
183
|
|
|
$permissionResolver->setCurrentUserReference($user); |
184
|
|
|
|
185
|
|
|
// This call will fail with an "UnauthorizedException" |
186
|
|
|
$roleService->deleteRole($role); |
187
|
|
|
/* END: Use Case */ |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Test for the updatePolicy() method. |
192
|
|
|
* |
193
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::updatePolicyByRoleDraft() |
194
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testUpdatePolicyByRoleDraft |
195
|
|
|
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
196
|
|
|
*/ |
197
|
|
|
public function testUpdatePolicyByRoleDraftThrowsUnauthorizedException() |
198
|
|
|
{ |
199
|
|
|
$this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
200
|
|
|
|
201
|
|
|
$repository = $this->getRepository(); |
202
|
|
|
$roleService = $repository->getRoleService(); |
203
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
204
|
|
|
|
205
|
|
|
/* BEGIN: Use Case */ |
206
|
|
|
$user = $this->createUserVersion1(); |
207
|
|
|
|
208
|
|
|
$role = $this->createRole(); |
209
|
|
|
$roleDraft = $roleService->createRoleDraft($role); |
210
|
|
|
// Get first role policy |
211
|
|
|
$policies = $roleDraft->getPolicies(); |
212
|
|
|
$policy = reset($policies); |
213
|
|
|
|
214
|
|
|
// Set "Editor" user as current user. |
215
|
|
|
$permissionResolver->setCurrentUserReference($user); |
216
|
|
|
|
217
|
|
|
// Get a policy update struct and add a limitation |
218
|
|
|
$policyUpdate = $roleService->newPolicyUpdateStruct(); |
219
|
|
|
$policyUpdate->addLimitation( |
220
|
|
|
new SubtreeLimitation( |
221
|
|
|
[ |
222
|
|
|
'limitationValues' => ['/1/'], |
223
|
|
|
] |
224
|
|
|
) |
225
|
|
|
); |
226
|
|
|
|
227
|
|
|
// This call will fail with an "UnauthorizedException" |
228
|
|
|
$roleService->updatePolicyByRoleDraft( |
229
|
|
|
$roleDraft, |
230
|
|
|
$policy, |
231
|
|
|
$policyUpdate |
232
|
|
|
); |
233
|
|
|
$roleService->publishRoleDraft($roleDraft); |
234
|
|
|
/* END: Use Case */ |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Test for the removePolicy() method. |
239
|
|
|
* |
240
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::removePolicy() |
241
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testRemovePolicyByRoleDraft |
242
|
|
|
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
243
|
|
|
*/ |
244
|
|
|
public function testRemovePolicyThrowsUnauthorizedException() |
245
|
|
|
{ |
246
|
|
|
$this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
247
|
|
|
|
248
|
|
|
$repository = $this->getRepository(); |
249
|
|
|
$roleService = $repository->getRoleService(); |
250
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
251
|
|
|
|
252
|
|
|
/* BEGIN: Use Case */ |
253
|
|
|
$user = $this->createUserVersion1(); |
254
|
|
|
|
255
|
|
|
$roleCreate = $roleService->newRoleCreateStruct('newRole'); |
256
|
|
|
|
257
|
|
|
// @todo uncomment when support for multilingual names and descriptions is added EZP-24776 |
258
|
|
|
// $roleCreate->mainLanguageCode = 'eng-US'; |
259
|
|
|
|
260
|
|
|
// Create a new role with two policies |
261
|
|
|
$roleDraft = $roleService->createRole($roleCreate); |
262
|
|
|
$roleService->addPolicyByRoleDraft( |
263
|
|
|
$roleDraft, |
264
|
|
|
$roleService->newPolicyCreateStruct('content', 'create') |
265
|
|
|
); |
266
|
|
|
$roleDraft = $roleService->addPolicyByRoleDraft( |
267
|
|
|
$roleDraft, |
268
|
|
|
$roleService->newPolicyCreateStruct('content', 'delete') |
269
|
|
|
); |
270
|
|
|
|
271
|
|
|
// Set "Editor" user as current user. |
272
|
|
|
$permissionResolver->setCurrentUserReference($user); |
273
|
|
|
|
274
|
|
|
// This call will fail with an "UnauthorizedException" |
275
|
|
|
$roleService->removePolicyByRoleDraft($roleDraft, $roleDraft->getPolicies()[0]); |
|
|
|
|
276
|
|
|
/* END: Use Case */ |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Test for the removePolicyByRoleDraft() method. |
281
|
|
|
* |
282
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::removePolicyByRoleDraft() |
283
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testRemovePolicyByRoleDraft |
284
|
|
|
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
285
|
|
|
*/ |
286
|
|
View Code Duplication |
public function testDeletePolicyThrowsUnauthorizedException() |
287
|
|
|
{ |
288
|
|
|
$this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
289
|
|
|
|
290
|
|
|
$repository = $this->getRepository(); |
291
|
|
|
$roleService = $repository->getRoleService(); |
292
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
293
|
|
|
|
294
|
|
|
/* BEGIN: Use Case */ |
295
|
|
|
$user = $this->createUserVersion1(); |
296
|
|
|
|
297
|
|
|
$role = $this->createRole(); |
298
|
|
|
|
299
|
|
|
// Get first role policy |
300
|
|
|
$roleDraft = $roleService->createRoleDraft($role); |
301
|
|
|
$policiesDrafts = $roleDraft->getPolicies(); |
302
|
|
|
$policyDraft = reset($policiesDrafts); |
303
|
|
|
|
304
|
|
|
// Set "Editor" user as current user. |
305
|
|
|
$permissionResolver->setCurrentUserReference($user); |
306
|
|
|
|
307
|
|
|
// This call will fail with an "UnauthorizedException" |
308
|
|
|
$roleService->removePolicyByRoleDraft($roleDraft, $policyDraft); |
309
|
|
|
/* END: Use Case */ |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Test for the assignRoleToUserGroup() method. |
314
|
|
|
* |
315
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup() |
316
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup |
317
|
|
|
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
318
|
|
|
*/ |
319
|
|
View Code Duplication |
public function testAssignRoleToUserGroupThrowsUnauthorizedException() |
320
|
|
|
{ |
321
|
|
|
$this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
322
|
|
|
|
323
|
|
|
$repository = $this->getRepository(); |
324
|
|
|
$userService = $repository->getUserService(); |
325
|
|
|
$roleService = $repository->getRoleService(); |
326
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
327
|
|
|
|
328
|
|
|
$editorsGroupId = $this->generateId('group', 13); |
329
|
|
|
|
330
|
|
|
/* BEGIN: Use Case */ |
331
|
|
|
$user = $this->createUserVersion1(); |
332
|
|
|
|
333
|
|
|
$role = $this->createRole(); |
334
|
|
|
|
335
|
|
|
// Load the "Editors" user group |
336
|
|
|
$userGroup = $userService->loadUserGroup($editorsGroupId); |
337
|
|
|
|
338
|
|
|
// Set "Editor" user as current user. |
339
|
|
|
$permissionResolver->setCurrentUserReference($user); |
340
|
|
|
|
341
|
|
|
// This call will fail with an "UnauthorizedException" |
342
|
|
|
$roleService->assignRoleToUserGroup($role, $userGroup); |
343
|
|
|
/* END: Use Case */ |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Test for the assignRoleToUserGroup() method. |
348
|
|
|
* |
349
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::assignRoleToUserGroup($role, $userGroup, $roleLimitation) |
350
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUserGroup |
351
|
|
|
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
352
|
|
|
*/ |
353
|
|
|
public function testAssignRoleToUserGroupThrowsUnauthorizedExceptionWithRoleLimitationParameter() |
354
|
|
|
{ |
355
|
|
|
$this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
356
|
|
|
|
357
|
|
|
$repository = $this->getRepository(); |
358
|
|
|
$userService = $repository->getUserService(); |
359
|
|
|
$roleService = $repository->getRoleService(); |
360
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
361
|
|
|
|
362
|
|
|
$editorsGroupId = $this->generateId('group', 13); |
363
|
|
|
|
364
|
|
|
/* BEGIN: Use Case */ |
365
|
|
|
$user = $this->createUserVersion1(); |
366
|
|
|
|
367
|
|
|
$role = $this->createRole(); |
368
|
|
|
|
369
|
|
|
// Load the "Editors" user group |
370
|
|
|
$userGroup = $userService->loadUserGroup($editorsGroupId); |
371
|
|
|
|
372
|
|
|
// Set "Editor" user as current user. |
373
|
|
|
$permissionResolver->setCurrentUserReference($user); |
374
|
|
|
|
375
|
|
|
// Create a subtree role limitation |
376
|
|
|
$limitation = new SubtreeLimitation( |
377
|
|
|
[ |
378
|
|
|
'limitationValues' => ['/1/2/'], |
379
|
|
|
] |
380
|
|
|
); |
381
|
|
|
|
382
|
|
|
// This call will fail with an "UnauthorizedException" |
383
|
|
|
$roleService->assignRoleToUserGroup($role, $userGroup, $limitation); |
384
|
|
|
/* END: Use Case */ |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Test for the removeRoleAssignment() method. |
389
|
|
|
* |
390
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::removeRoleAssignment() |
391
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testRemoveRoleAssignmentFromUserGroup |
392
|
|
|
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
393
|
|
|
*/ |
394
|
|
|
public function testRemoveRoleAssignmentFromUserGroupThrowsUnauthorizedException() |
395
|
|
|
{ |
396
|
|
|
$this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
397
|
|
|
|
398
|
|
|
$repository = $this->getRepository(); |
399
|
|
|
$userService = $repository->getUserService(); |
400
|
|
|
$roleService = $repository->getRoleService(); |
401
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
402
|
|
|
|
403
|
|
|
$editorsGroupId = $this->generateId('group', 13); |
404
|
|
|
|
405
|
|
|
/* BEGIN: Use Case */ |
406
|
|
|
$user = $this->createUserVersion1(); |
407
|
|
|
|
408
|
|
|
$role = $this->createRole(); |
409
|
|
|
|
410
|
|
|
// Load the "Editors" user group |
411
|
|
|
$userGroup = $userService->loadUserGroup($editorsGroupId); |
412
|
|
|
|
413
|
|
|
// Assign new role to "Editors" user group |
414
|
|
|
$roleService->assignRoleToUserGroup($role, $userGroup); |
415
|
|
|
|
416
|
|
|
// Set "Editor" user as current user. |
417
|
|
|
$permissionResolver->setCurrentUserReference($user); |
418
|
|
|
|
419
|
|
|
$roleAssignments = $roleService->getRoleAssignmentsForUserGroup($userGroup); |
420
|
|
|
|
421
|
|
|
// This call will fail with an "UnauthorizedException" |
422
|
|
|
foreach ($roleAssignments as $roleAssignment) { |
423
|
|
|
if ($roleAssignment->role->id === $role->id) { |
424
|
|
|
$roleService->removeRoleAssignment($roleAssignment); |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
/* END: Use Case */ |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Test for the assignRoleToUser() method. |
432
|
|
|
* |
433
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser() |
434
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser |
435
|
|
|
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
436
|
|
|
*/ |
437
|
|
|
public function testAssignRoleToUserThrowsUnauthorizedException() |
438
|
|
|
{ |
439
|
|
|
$this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
440
|
|
|
|
441
|
|
|
$repository = $this->getRepository(); |
442
|
|
|
$roleService = $repository->getRoleService(); |
443
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
444
|
|
|
|
445
|
|
|
/* BEGIN: Use Case */ |
446
|
|
|
$user = $this->createUserVersion1(); |
447
|
|
|
|
448
|
|
|
$role = $this->createRole(); |
449
|
|
|
|
450
|
|
|
// Set "Editor" user as current user. |
451
|
|
|
$permissionResolver->setCurrentUserReference($user); |
452
|
|
|
|
453
|
|
|
// This call will fail with an "UnauthorizedException" |
454
|
|
|
$roleService->assignRoleToUser($role, $user); |
455
|
|
|
/* END: Use Case */ |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Test for the assignRoleToUser() method. |
460
|
|
|
* |
461
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::assignRoleToUser($role, $user, $roleLimitation) |
462
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testAssignRoleToUser |
463
|
|
|
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
464
|
|
|
*/ |
465
|
|
|
public function testAssignRoleToUserThrowsUnauthorizedExceptionWithRoleLimitationParameter() |
466
|
|
|
{ |
467
|
|
|
$this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
468
|
|
|
|
469
|
|
|
$repository = $this->getRepository(); |
470
|
|
|
$roleService = $repository->getRoleService(); |
471
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
472
|
|
|
|
473
|
|
|
/* BEGIN: Use Case */ |
474
|
|
|
$user = $this->createUserVersion1(); |
475
|
|
|
|
476
|
|
|
$role = $this->createRole(); |
477
|
|
|
|
478
|
|
|
// Set "Editor" user as current user. |
479
|
|
|
$permissionResolver->setCurrentUserReference($user); |
480
|
|
|
|
481
|
|
|
// Create a subtree role limitation |
482
|
|
|
$limitation = new SubtreeLimitation( |
483
|
|
|
[ |
484
|
|
|
'limitationValues' => ['/1/2/'], |
485
|
|
|
] |
486
|
|
|
); |
487
|
|
|
|
488
|
|
|
// This call will fail with an "UnauthorizedException" |
489
|
|
|
$roleService->assignRoleToUser($role, $user, $limitation); |
490
|
|
|
/* END: Use Case */ |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* Test for the removeRoleAssignment() method. |
495
|
|
|
* |
496
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::removeRoleAssignment() |
497
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testRemoveRoleAssignment |
498
|
|
|
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
499
|
|
|
*/ |
500
|
|
|
public function testRemoveRoleAssignmentThrowsUnauthorizedException() |
501
|
|
|
{ |
502
|
|
|
$this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
503
|
|
|
|
504
|
|
|
$repository = $this->getRepository(); |
505
|
|
|
$roleService = $repository->getRoleService(); |
506
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
507
|
|
|
|
508
|
|
|
/* BEGIN: Use Case */ |
509
|
|
|
$user = $this->createUserVersion1(); |
510
|
|
|
|
511
|
|
|
$role = $this->createRole(); |
512
|
|
|
|
513
|
|
|
// Assign new role to "Editor" user |
514
|
|
|
$roleService->assignRoleToUser($role, $user); |
515
|
|
|
|
516
|
|
|
// Set "Editor" user as current user. |
517
|
|
|
$permissionResolver->setCurrentUserReference($user); |
518
|
|
|
$roleAssignments = $roleService->getRoleAssignmentsForUser($user); |
519
|
|
|
|
520
|
|
|
// This call will fail with an "UnauthorizedException" |
521
|
|
|
foreach ($roleAssignments as $roleAssignment) { |
522
|
|
|
if ($roleAssignment->role->id === $role->id) { |
523
|
|
|
$roleService->removeRoleAssignment($roleAssignment); |
524
|
|
|
} |
525
|
|
|
} |
526
|
|
|
/* END: Use Case */ |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
/** |
530
|
|
|
* Test for the getRoleAssignments() method. |
531
|
|
|
* |
532
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::getRoleAssignments() |
533
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignments |
534
|
|
|
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
535
|
|
|
*/ |
536
|
|
|
public function testGetRoleAssignmentsThrowsUnauthorizedException() |
537
|
|
|
{ |
538
|
|
|
$this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
539
|
|
|
|
540
|
|
|
$repository = $this->getRepository(); |
541
|
|
|
$roleService = $repository->getRoleService(); |
542
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
543
|
|
|
|
544
|
|
|
/* BEGIN: Use Case */ |
545
|
|
|
$user = $this->createUserVersion1(); |
546
|
|
|
|
547
|
|
|
$role = $this->createRole(); |
548
|
|
|
|
549
|
|
|
// Set "Editor" user as current user. |
550
|
|
|
$permissionResolver->setCurrentUserReference($user); |
551
|
|
|
|
552
|
|
|
// This call will fail with an "UnauthorizedException" |
553
|
|
|
$roleService->getRoleAssignments($role); |
554
|
|
|
/* END: Use Case */ |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
/** |
558
|
|
|
* Test for the getRoleAssignmentsForUser() method. |
559
|
|
|
* |
560
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser() |
561
|
|
|
*/ |
562
|
|
View Code Duplication |
public function testGetRoleAssignmentsForUserLoadsEmptyListForAnonymousUser() |
563
|
|
|
{ |
564
|
|
|
$repository = $this->getRepository(); |
565
|
|
|
$roleService = $repository->getRoleService(); |
566
|
|
|
|
567
|
|
|
/* BEGIN: Use Case */ |
568
|
|
|
$user = $this->createUserVersion1(); |
569
|
|
|
|
570
|
|
|
$this->createRole(); |
571
|
|
|
|
572
|
|
|
// Set "Editor" user as current user. |
573
|
|
|
$repository->getPermissionResolver()->setCurrentUserReference($user); |
574
|
|
|
/* END: Use Case */ |
575
|
|
|
|
576
|
|
|
$this->assertSame([], $roleService->getRoleAssignmentsForUser($user)); |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
/** |
580
|
|
|
* Test for the getRoleAssignmentsForUser() method. |
581
|
|
|
* |
582
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser() |
583
|
|
|
*/ |
584
|
|
|
public function testGetRoleAssignmentsForUserWithSubtreeLimitation() |
585
|
|
|
{ |
586
|
|
|
$repository = $this->getRepository(); |
587
|
|
|
$roleService = $repository->getRoleService(); |
588
|
|
|
|
589
|
|
|
/* BEGIN: Use Case */ |
590
|
|
|
$user = $this->createUserWithPolicies( |
591
|
|
|
'trash_test_user', |
592
|
|
|
[ |
593
|
|
|
['module' => 'role', 'function' => 'read'], |
594
|
|
|
], |
595
|
|
|
new SubtreeLimitation(['limitationValues' => ['/1/2/']]) |
596
|
|
|
); |
597
|
|
|
|
598
|
|
|
$repository->getPermissionResolver()->setCurrentUserReference($user); |
599
|
|
|
/* END: Use Case */ |
600
|
|
|
|
601
|
|
|
$roleAssignments = $roleService->getRoleAssignmentsForUser($user); |
602
|
|
|
$this->assertCount(1, $roleAssignments); |
603
|
|
|
|
604
|
|
|
$roleAssignment = $roleAssignments[0]; |
605
|
|
|
$this->assertSame($user, $roleAssignment->user); |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
/** |
609
|
|
|
* Test for the getRoleAssignmentsForUserGroup() method. |
610
|
|
|
* |
611
|
|
|
* @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUserGroup() |
612
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignmentsForUserGroup |
613
|
|
|
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser |
614
|
|
|
*/ |
615
|
|
|
public function testGetRoleAssignmentsForUserGroupThrowsUnauthorizedException() |
616
|
|
|
{ |
617
|
|
|
$this->expectException(\eZ\Publish\API\Repository\Exceptions\UnauthorizedException::class); |
618
|
|
|
|
619
|
|
|
$repository = $this->getRepository(); |
620
|
|
|
$roleService = $repository->getRoleService(); |
621
|
|
|
$userService = $repository->getUserService(); |
622
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
623
|
|
|
|
624
|
|
|
$editorsGroupId = $this->generateId('group', 13); |
625
|
|
|
|
626
|
|
|
/* BEGIN: Use Case */ |
627
|
|
|
$user = $this->createUserVersion1(); |
628
|
|
|
|
629
|
|
|
$this->createRole(); |
630
|
|
|
|
631
|
|
|
// Load the "Editors" user group |
632
|
|
|
$userGroup = $userService->loadUserGroup($editorsGroupId); |
633
|
|
|
|
634
|
|
|
// Set "Editor" user as current user. |
635
|
|
|
$permissionResolver->setCurrentUserReference($user); |
636
|
|
|
|
637
|
|
|
// This call will fail with an "UnauthorizedException" |
638
|
|
|
$roleService->getRoleAssignmentsForUserGroup($userGroup); |
639
|
|
|
/* END: Use Case */ |
640
|
|
|
} |
641
|
|
|
|
642
|
|
|
/** |
643
|
|
|
* Create a role fixture in a variable named <b>$role</b>,. |
644
|
|
|
* |
645
|
|
|
* @return \eZ\Publish\API\Repository\Values\User\Role |
646
|
|
|
*/ |
647
|
|
View Code Duplication |
private function createRole() |
648
|
|
|
{ |
649
|
|
|
$repository = $this->getRepository(); |
650
|
|
|
|
651
|
|
|
/* BEGIN: Inline */ |
652
|
|
|
// Get the role service |
653
|
|
|
$roleService = $repository->getRoleService(); |
654
|
|
|
|
655
|
|
|
// Get new policy create struct |
656
|
|
|
$policyCreate = $roleService->newPolicyCreateStruct('content', '*'); |
657
|
|
|
|
658
|
|
|
// Get a role create struct instance and set properties |
659
|
|
|
$roleCreate = $roleService->newRoleCreateStruct('testRole'); |
660
|
|
|
|
661
|
|
|
// @todo uncomment when support for multilingual names and descriptions is added EZP-24776 |
662
|
|
|
// $roleCreate->mainLanguageCode = 'eng-GB'; |
663
|
|
|
|
664
|
|
|
$roleCreate->addPolicy($policyCreate); |
665
|
|
|
|
666
|
|
|
// Create a new role instance. |
667
|
|
|
$roleDraft = $roleService->createRole($roleCreate); |
668
|
|
|
$roleService->publishRoleDraft($roleDraft); |
669
|
|
|
$role = $roleService->loadRole($roleDraft->id); |
670
|
|
|
/* END: Inline */ |
671
|
|
|
|
672
|
|
|
return $role; |
673
|
|
|
} |
674
|
|
|
} |
675
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.