1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
namespace eZ\Publish\API\Repository\Tests; |
8
|
|
|
|
9
|
|
|
use eZ\Publish\API\Repository\Values\User\Limitation; |
10
|
|
|
use eZ\Publish\API\Repository\Values\ValueObject; |
11
|
|
|
use eZ\Publish\Core\Repository\Values\User\UserReference; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Test case for operations in the PermissionResolver. |
15
|
|
|
* |
16
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver |
17
|
|
|
* @group integration |
18
|
|
|
* @group permission |
19
|
|
|
*/ |
20
|
|
|
class PermissionResolverTest extends BaseTest |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Test for the getCurrentUser() method. |
24
|
|
|
* |
25
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver::getCurrentUserReference() |
26
|
|
|
*/ |
27
|
|
|
public function testGetCurrentUserReferenceReturnsAnonymousUserReference() |
28
|
|
|
{ |
29
|
|
|
$repository = $this->getRepository(); |
30
|
|
|
$anonymousUserId = $this->generateId('user', 10); |
31
|
|
|
$repository->getPermissionResolver()->setCurrentUserReference( |
32
|
|
|
new UserReference($anonymousUserId) |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
/* BEGIN: Use Case */ |
36
|
|
|
// $anonymousUserId is the ID of the "Anonymous" user in a eZ |
37
|
|
|
// Publish demo installation. |
38
|
|
|
// Only a UserReference has previously been set to the $repository |
39
|
|
|
|
40
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
41
|
|
|
$anonymousUserReference = $permissionResolver->getCurrentUserReference(); |
42
|
|
|
/* END: Use Case */ |
43
|
|
|
|
44
|
|
|
$this->assertInstanceOf( |
45
|
|
|
'eZ\Publish\API\Repository\Values\User\UserReference', |
46
|
|
|
$anonymousUserReference |
47
|
|
|
); |
48
|
|
|
$this->assertEquals( |
49
|
|
|
$anonymousUserReference->getUserId(), |
50
|
|
|
$repository->getUserService()->loadUser($anonymousUserId)->id |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Test for the setCurrentUser() method. |
56
|
|
|
* |
57
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver::setCurrentUserReference() |
58
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function testSetCurrentUserReference() |
61
|
|
|
{ |
62
|
|
|
$repository = $this->getRepository(); |
63
|
|
|
$repository->getPermissionResolver()->setCurrentUserReference( |
64
|
|
|
new UserReference( |
65
|
|
|
$this->generateId('user', 10) |
66
|
|
|
) |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$administratorUserId = $this->generateId('user', 14); |
70
|
|
|
|
71
|
|
|
/* BEGIN: Use Case */ |
72
|
|
|
// $administratorUserId contains the ID of the administrator user |
73
|
|
|
|
74
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
75
|
|
|
|
76
|
|
|
$userService = $repository->getUserService(); |
77
|
|
|
|
78
|
|
|
// Load administrator user |
79
|
|
|
$administratorUser = $userService->loadUser($administratorUserId); |
80
|
|
|
|
81
|
|
|
// Set administrator user as current user reference |
82
|
|
|
$permissionResolver->setCurrentUserReference($administratorUser); |
83
|
|
|
/* END: Use Case */ |
84
|
|
|
|
85
|
|
|
$this->assertEquals( |
86
|
|
|
$administratorUserId, |
87
|
|
|
$permissionResolver->getCurrentUserReference()->getUserId() |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$this->assertSame( |
91
|
|
|
$administratorUser, |
92
|
|
|
$permissionResolver->getCurrentUserReference() |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Test for the hasAccess() method. |
98
|
|
|
* |
99
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver::hasAccess() |
100
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function testHasAccessWithAnonymousUserNo() |
103
|
|
|
{ |
104
|
|
|
$repository = $this->getRepository(); |
105
|
|
|
|
106
|
|
|
$anonymousUserId = $this->generateId('user', 10); |
107
|
|
|
|
108
|
|
|
/* BEGIN: Use Case */ |
109
|
|
|
// $anonymousUserId is the ID of the "Anonymous" user in a eZ |
110
|
|
|
// Publish demo installation. |
111
|
|
|
|
112
|
|
|
$userService = $repository->getUserService(); |
113
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
114
|
|
|
|
115
|
|
|
// Load anonymous user |
116
|
|
|
$anonymousUser = $userService->loadUser($anonymousUserId); |
117
|
|
|
|
118
|
|
|
// This call will return false because anonymous user does not have access |
119
|
|
|
// to content removal |
120
|
|
|
$hasAccess = $permissionResolver->hasAccess('content', 'remove', $anonymousUser); |
121
|
|
|
/* END: Use Case */ |
122
|
|
|
|
123
|
|
|
$this->assertFalse($hasAccess); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Test for the hasAccess() method. |
128
|
|
|
* |
129
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver::hasAccess() |
130
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
131
|
|
|
* @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessWithAnonymousUserNo |
132
|
|
|
*/ |
133
|
|
View Code Duplication |
public function testHasAccessForCurrentUserNo() |
134
|
|
|
{ |
135
|
|
|
$repository = $this->getRepository(); |
136
|
|
|
|
137
|
|
|
$anonymousUserId = $this->generateId('user', 10); |
138
|
|
|
|
139
|
|
|
/* BEGIN: Use Case */ |
140
|
|
|
// $anonymousUserId is the ID of the "Anonymous" user in a eZ |
141
|
|
|
// Publish demo installation. |
142
|
|
|
|
143
|
|
|
$userService = $repository->getUserService(); |
144
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
145
|
|
|
|
146
|
|
|
// Load anonymous user |
147
|
|
|
$anonymousUser = $userService->loadUser($anonymousUserId); |
148
|
|
|
|
149
|
|
|
// Set anonymous user as current user reference |
150
|
|
|
$permissionResolver->setCurrentUserReference($anonymousUser); |
151
|
|
|
|
152
|
|
|
// This call will return false because anonymous user does not have access |
153
|
|
|
// to content removal |
154
|
|
|
$hasAccess = $permissionResolver->hasAccess('content', 'remove'); |
155
|
|
|
/* END: Use Case */ |
156
|
|
|
|
157
|
|
|
$this->assertFalse($hasAccess); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Test for the hasAccess() method. |
162
|
|
|
* |
163
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver::hasAccess() |
164
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
165
|
|
|
*/ |
166
|
|
View Code Duplication |
public function testHasAccessWithAdministratorUser() |
167
|
|
|
{ |
168
|
|
|
$repository = $this->getRepository(); |
169
|
|
|
|
170
|
|
|
$administratorUserId = $this->generateId('user', 14); |
171
|
|
|
|
172
|
|
|
/* BEGIN: Use Case */ |
173
|
|
|
// $administratorUserId contains the ID of the administrator user |
174
|
|
|
|
175
|
|
|
$userService = $repository->getUserService(); |
176
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
177
|
|
|
|
178
|
|
|
// Load administrator user |
179
|
|
|
$administratorUser = $userService->loadUser($administratorUserId); |
180
|
|
|
|
181
|
|
|
// This call will return true |
182
|
|
|
$hasAccess = $permissionResolver->hasAccess('content', 'read', $administratorUser); |
183
|
|
|
/* END: Use Case */ |
184
|
|
|
|
185
|
|
|
$this->assertTrue($hasAccess); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Test for the hasAccess() method. |
190
|
|
|
* |
191
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver::hasAccess() |
192
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
193
|
|
|
* @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testSetCurrentUserReference |
194
|
|
|
* @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessWithAdministratorUser |
195
|
|
|
*/ |
196
|
|
View Code Duplication |
public function testHasAccessForCurrentUserYes() |
197
|
|
|
{ |
198
|
|
|
$repository = $this->getRepository(); |
199
|
|
|
|
200
|
|
|
$administratorUserId = $this->generateId('user', 14); |
201
|
|
|
|
202
|
|
|
/* BEGIN: Use Case */ |
203
|
|
|
// $administratorUserId contains the ID of the administrator user |
204
|
|
|
|
205
|
|
|
$userService = $repository->getUserService(); |
206
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
207
|
|
|
|
208
|
|
|
// Load administrator user |
209
|
|
|
$administratorUser = $userService->loadUser($administratorUserId); |
210
|
|
|
|
211
|
|
|
// Set administrator user as current user reference |
212
|
|
|
$permissionResolver->setCurrentUserReference($administratorUser); |
213
|
|
|
|
214
|
|
|
// This call will return true |
215
|
|
|
$hasAccess = $permissionResolver->hasAccess('content', 'read'); |
216
|
|
|
/* END: Use Case */ |
217
|
|
|
|
218
|
|
|
$this->assertTrue($hasAccess); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Test for the hasAccess() method. |
223
|
|
|
* |
224
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver::hasAccess() |
225
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
226
|
|
|
* @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testSetCurrentUserReference |
227
|
|
|
*/ |
228
|
|
View Code Duplication |
public function testHasAccessLimited() |
229
|
|
|
{ |
230
|
|
|
$repository = $this->getRepository(); |
231
|
|
|
|
232
|
|
|
/* BEGIN: Use Case */ |
233
|
|
|
$user = $this->createUserVersion1(); |
234
|
|
|
|
235
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
236
|
|
|
|
237
|
|
|
// Set created user as current user reference |
238
|
|
|
$permissionResolver->setCurrentUserReference($user); |
239
|
|
|
|
240
|
|
|
// This call will return an array of permission sets describing user's access |
241
|
|
|
// to reading content |
242
|
|
|
$permissionSets = $permissionResolver->hasAccess('content', 'read'); |
243
|
|
|
/* END: Use Case */ |
244
|
|
|
|
245
|
|
|
$this->assertInternalType( |
246
|
|
|
'array', |
247
|
|
|
$permissionSets |
248
|
|
|
); |
249
|
|
|
$this->assertNotEmpty($permissionSets); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Test for the canUser() method. |
254
|
|
|
* |
255
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
256
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
257
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
258
|
|
|
* @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessForCurrentUserNo |
259
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
260
|
|
|
*/ |
261
|
|
View Code Duplication |
public function testCanUserForAnonymousUserNo() |
262
|
|
|
{ |
263
|
|
|
$repository = $this->getRepository(); |
264
|
|
|
|
265
|
|
|
$homeId = $this->generateId('object', 57); |
266
|
|
|
|
267
|
|
|
$anonymousUserId = $this->generateId('user', 10); |
268
|
|
|
/* BEGIN: Use Case */ |
269
|
|
|
// $anonymousUserId is the ID of the "Anonymous" user in a eZ |
270
|
|
|
// Publish demo installation. |
271
|
|
|
// $homeId contains the ID of the "Home" frontpage |
272
|
|
|
|
273
|
|
|
$contentService = $repository->getContentService(); |
274
|
|
|
$userService = $repository->getUserService(); |
275
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
276
|
|
|
|
277
|
|
|
// Load anonymous user |
278
|
|
|
$anonymousUser = $userService->loadUser($anonymousUserId); |
279
|
|
|
|
280
|
|
|
// Set anonymous user as current user reference |
281
|
|
|
$permissionResolver->setCurrentUserReference($anonymousUser); |
282
|
|
|
|
283
|
|
|
// Load the ContentInfo for "Home" frontpage |
284
|
|
|
$contentInfo = $contentService->loadContentInfo($homeId); |
285
|
|
|
|
286
|
|
|
// This call will return false because anonymous user does not have access |
287
|
|
|
// to content removal and hence no permission to remove given content |
288
|
|
|
$canUser = $permissionResolver->canUser('content', 'remove', $contentInfo); |
289
|
|
|
|
290
|
|
|
// Performing an action without necessary permissions will fail with "UnauthorizedException" |
291
|
|
|
if (!$canUser) { |
292
|
|
|
$contentService->deleteContent($contentInfo); |
293
|
|
|
} |
294
|
|
|
/* END: Use Case */ |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Test for the canUser() method. |
299
|
|
|
* |
300
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
301
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
302
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
303
|
|
|
* @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessForCurrentUserYes |
304
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException |
305
|
|
|
*/ |
306
|
|
View Code Duplication |
public function testCanUserForAdministratorUser() |
307
|
|
|
{ |
308
|
|
|
$repository = $this->getRepository(); |
309
|
|
|
|
310
|
|
|
$administratorUserId = $this->generateId('user', 14); |
311
|
|
|
$homeId = $this->generateId('object', 57); |
312
|
|
|
|
313
|
|
|
/* BEGIN: Use Case */ |
314
|
|
|
// $administratorUserId contains the ID of the administrator user |
315
|
|
|
// $homeId contains the ID of the "Home" frontpage |
316
|
|
|
|
317
|
|
|
$contentService = $repository->getContentService(); |
318
|
|
|
$userService = $repository->getUserService(); |
319
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
320
|
|
|
|
321
|
|
|
// Load administrator user |
322
|
|
|
$administratorUser = $userService->loadUser($administratorUserId); |
323
|
|
|
|
324
|
|
|
// Set administrator user as current user reference |
325
|
|
|
$permissionResolver->setCurrentUserReference($administratorUser); |
326
|
|
|
|
327
|
|
|
// Load the ContentInfo for "Home" frontpage |
328
|
|
|
$contentInfo = $contentService->loadContentInfo($homeId); |
329
|
|
|
|
330
|
|
|
// This call will return true |
331
|
|
|
$canUser = $permissionResolver->canUser('content', 'remove', $contentInfo); |
332
|
|
|
|
333
|
|
|
// Performing an action having necessary permissions will succeed |
334
|
|
|
$contentService->deleteContent($contentInfo); |
335
|
|
|
/* END: Use Case */ |
336
|
|
|
|
337
|
|
|
$this->assertTrue($canUser); |
338
|
|
|
$contentService->loadContent($homeId); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Test for the canUser() method. |
343
|
|
|
* |
344
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
345
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
346
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
347
|
|
|
* @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
348
|
|
|
*/ |
349
|
|
|
public function testCanUserWithLimitationYes() |
350
|
|
|
{ |
351
|
|
|
$repository = $this->getRepository(); |
352
|
|
|
|
353
|
|
|
$imagesFolderId = $this->generateId('object', 49); |
354
|
|
|
|
355
|
|
|
/* BEGIN: Use Case */ |
356
|
|
|
// $imagesFolderId contains the ID of the "Images" folder |
357
|
|
|
|
358
|
|
|
$user = $this->createUserVersion1(); |
359
|
|
|
|
360
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
361
|
|
|
|
362
|
|
|
// Set created user as current user reference |
363
|
|
|
$permissionResolver->setCurrentUserReference($user); |
364
|
|
|
|
365
|
|
|
$contentService = $repository->getContentService(); |
366
|
|
|
|
367
|
|
|
// Performing an action having necessary permissions will succeed |
368
|
|
|
$imagesFolder = $contentService->loadContent($imagesFolderId); |
369
|
|
|
|
370
|
|
|
// This call will return true |
371
|
|
|
$canUser = $permissionResolver->canUser('content', 'read', $imagesFolder); |
372
|
|
|
/* END: Use Case */ |
373
|
|
|
|
374
|
|
|
$this->assertTrue($canUser); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Test for the canUser() method. |
379
|
|
|
* |
380
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
381
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
382
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
383
|
|
|
* @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
384
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
385
|
|
|
*/ |
386
|
|
View Code Duplication |
public function testCanUserWithLimitationNo() |
387
|
|
|
{ |
388
|
|
|
$repository = $this->getRepository(); |
389
|
|
|
|
390
|
|
|
$administratorUserId = $this->generateId('user', 14); |
391
|
|
|
|
392
|
|
|
/* BEGIN: Use Case */ |
393
|
|
|
// $administratorUserId contains the ID of the administrator user |
394
|
|
|
|
395
|
|
|
$user = $this->createUserVersion1(); |
396
|
|
|
|
397
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
398
|
|
|
|
399
|
|
|
// Set created user as current user reference |
400
|
|
|
$permissionResolver->setCurrentUserReference($user); |
401
|
|
|
|
402
|
|
|
$userService = $repository->getUserService(); |
403
|
|
|
|
404
|
|
|
// Load administrator user using UserService, this does not check for permissions |
405
|
|
|
$administratorUser = $userService->loadUser($administratorUserId); |
406
|
|
|
|
407
|
|
|
// This call will return false as user with Editor role does not have |
408
|
|
|
// permission to read "Users" subtree |
409
|
|
|
$canUser = $permissionResolver->canUser('content', 'read', $administratorUser); |
410
|
|
|
|
411
|
|
|
$contentService = $repository->getContentService(); |
412
|
|
|
|
413
|
|
|
// Performing an action without necessary permissions will fail with "UnauthorizedException" |
414
|
|
|
if (!$canUser) { |
415
|
|
|
$content = $contentService->loadContent($administratorUserId); |
|
|
|
|
416
|
|
|
} |
417
|
|
|
/* END: Use Case */ |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Test for the canUser() method. |
422
|
|
|
* |
423
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
424
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
425
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService |
426
|
|
|
* @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testSetCurrentUserReference |
427
|
|
|
* @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
428
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
429
|
|
|
*/ |
430
|
|
|
public function testCanUserThrowsInvalidArgumentException() |
431
|
|
|
{ |
432
|
|
|
$repository = $this->getRepository(); |
433
|
|
|
|
434
|
|
|
$userGroupContentTypeId = $this->generateId('type', 3); |
435
|
|
|
|
436
|
|
|
/* BEGIN: Use Case */ |
437
|
|
|
// $userGroupContentTypeId contains the ID of the "UserGroup" ContentType |
438
|
|
|
|
439
|
|
|
$user = $this->createUserVersion1(); |
440
|
|
|
|
441
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
442
|
|
|
|
443
|
|
|
// Set created user as current user reference |
444
|
|
|
$permissionResolver->setCurrentUserReference($user); |
445
|
|
|
|
446
|
|
|
$contentTypeService = $repository->getContentTypeService(); |
447
|
|
|
|
448
|
|
|
// Load the "UserGroup" ContentType |
449
|
|
|
$userGroupContentType = $contentTypeService->loadContentType($userGroupContentTypeId); |
450
|
|
|
|
451
|
|
|
// This call will throw "InvalidArgumentException" because $userGroupContentType |
452
|
|
|
// is an instance of \eZ\Publish\API\Repository\Values\ContentType\ContentType, |
453
|
|
|
// which can not be checked for user access |
454
|
|
|
$canUser = $permissionResolver->canUser('content', 'create', $userGroupContentType); |
|
|
|
|
455
|
|
|
/* END: Use Case */ |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Test for the canUser() method. |
460
|
|
|
* |
461
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
462
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
463
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
464
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService |
465
|
|
|
* @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
466
|
|
|
*/ |
467
|
|
View Code Duplication |
public function testCanUserWithTargetYes() |
468
|
|
|
{ |
469
|
|
|
$repository = $this->getRepository(); |
470
|
|
|
|
471
|
|
|
$homeLocationId = $this->generateId('location', 2); |
472
|
|
|
|
473
|
|
|
/* BEGIN: Use Case */ |
474
|
|
|
// $homeLocationId contains the ID of the "Home" location |
475
|
|
|
|
476
|
|
|
$user = $this->createUserVersion1(); |
477
|
|
|
|
478
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
479
|
|
|
|
480
|
|
|
// Set created user as current user reference |
481
|
|
|
$permissionResolver->setCurrentUserReference($user); |
482
|
|
|
|
483
|
|
|
$contentTypeService = $repository->getContentTypeService(); |
484
|
|
|
|
485
|
|
|
$contentType = $contentTypeService->loadContentTypeByIdentifier('forums'); |
486
|
|
|
|
487
|
|
|
$contentService = $repository->getContentService(); |
488
|
|
|
|
489
|
|
|
$contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US'); |
490
|
|
|
$contentCreateStruct->setField('title', 'My awesome forums'); |
491
|
|
|
$contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789'; |
492
|
|
|
$contentCreateStruct->alwaysAvailable = true; |
493
|
|
|
|
494
|
|
|
$locationService = $repository->getLocationService(); |
495
|
|
|
$locationCreateStruct = $locationService->newLocationCreateStruct($homeLocationId); |
496
|
|
|
|
497
|
|
|
// This call will return true |
498
|
|
|
$canUser = $permissionResolver->canUser( |
499
|
|
|
'content', |
500
|
|
|
'create', |
501
|
|
|
$contentCreateStruct, |
502
|
|
|
[$locationCreateStruct] |
503
|
|
|
); |
504
|
|
|
|
505
|
|
|
// Performing an action having necessary permissions will succeed |
506
|
|
|
$contentDraft = $contentService->createContent( |
507
|
|
|
$contentCreateStruct, |
508
|
|
|
array($locationCreateStruct) |
509
|
|
|
); |
510
|
|
|
/* END: Use Case */ |
511
|
|
|
|
512
|
|
|
$this->assertTrue($canUser); |
513
|
|
|
$this->assertEquals( |
514
|
|
|
'My awesome forums', |
515
|
|
|
$contentDraft->getFieldValue('title')->text |
516
|
|
|
); |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* Test for the canUser() method. |
521
|
|
|
* |
522
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
523
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
524
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
525
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService |
526
|
|
|
* @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
527
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
528
|
|
|
*/ |
529
|
|
View Code Duplication |
public function testCanUserWithTargetNo() |
530
|
|
|
{ |
531
|
|
|
$repository = $this->getRepository(); |
532
|
|
|
|
533
|
|
|
$homeLocationId = $this->generateId('location', 2); |
534
|
|
|
|
535
|
|
|
/* BEGIN: Use Case */ |
536
|
|
|
// $homeLocationId contains the ID of the "Home" frontpage location |
537
|
|
|
|
538
|
|
|
$user = $this->createUserVersion1(); |
539
|
|
|
|
540
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
541
|
|
|
|
542
|
|
|
// Set created user as current user reference |
543
|
|
|
$permissionResolver->setCurrentUserReference($user); |
544
|
|
|
|
545
|
|
|
$contentTypeService = $repository->getContentTypeService(); |
546
|
|
|
|
547
|
|
|
$contentType = $contentTypeService->loadContentTypeByIdentifier('forum'); |
548
|
|
|
|
549
|
|
|
$contentService = $repository->getContentService(); |
550
|
|
|
|
551
|
|
|
$contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US'); |
552
|
|
|
$contentCreateStruct->setField('name', 'My awesome forum'); |
553
|
|
|
$contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789'; |
554
|
|
|
$contentCreateStruct->alwaysAvailable = true; |
555
|
|
|
|
556
|
|
|
$locationService = $repository->getLocationService(); |
557
|
|
|
$locationCreateStruct = $locationService->newLocationCreateStruct($homeLocationId); |
558
|
|
|
|
559
|
|
|
// This call will return false because user with Editor role has permission to |
560
|
|
|
// create "forum" type content only under "folder" type content. |
561
|
|
|
$canUser = $permissionResolver->canUser( |
562
|
|
|
'content', |
563
|
|
|
'create', |
564
|
|
|
$contentCreateStruct, |
565
|
|
|
[$locationCreateStruct] |
566
|
|
|
); |
567
|
|
|
|
568
|
|
|
// Performing an action without necessary permissions will fail with "UnauthorizedException" |
569
|
|
|
if (!$canUser) { |
570
|
|
|
$contentDraft = $contentService->createContent( |
|
|
|
|
571
|
|
|
$contentCreateStruct, |
572
|
|
|
array($locationCreateStruct) |
573
|
|
|
); |
574
|
|
|
} |
575
|
|
|
/* END: Use Case */ |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* Test for the canUser() method. |
580
|
|
|
* |
581
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
582
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
583
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
584
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService |
585
|
|
|
* @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
586
|
|
|
*/ |
587
|
|
View Code Duplication |
public function testCanUserWithMultipleTargetsYes() |
588
|
|
|
{ |
589
|
|
|
$repository = $this->getRepository(); |
590
|
|
|
|
591
|
|
|
$imagesLocationId = $this->generateId('location', 51); |
592
|
|
|
$filesLocationId = $this->generateId('location', 52); |
593
|
|
|
|
594
|
|
|
/* BEGIN: Use Case */ |
595
|
|
|
// $imagesLocationId contains the ID of the "Images" location |
596
|
|
|
// $filesLocationId contains the ID of the "Files" location |
597
|
|
|
|
598
|
|
|
$user = $this->createUserVersion1(); |
599
|
|
|
|
600
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
601
|
|
|
|
602
|
|
|
// Set created user as current user reference |
603
|
|
|
$permissionResolver->setCurrentUserReference($user); |
604
|
|
|
|
605
|
|
|
$contentTypeService = $repository->getContentTypeService(); |
606
|
|
|
|
607
|
|
|
$contentType = $contentTypeService->loadContentTypeByIdentifier('folder'); |
608
|
|
|
|
609
|
|
|
$contentService = $repository->getContentService(); |
610
|
|
|
|
611
|
|
|
$contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US'); |
612
|
|
|
$contentCreateStruct->setField('name', 'My multipurpose folder'); |
613
|
|
|
$contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789'; |
614
|
|
|
$contentCreateStruct->alwaysAvailable = true; |
615
|
|
|
|
616
|
|
|
$locationService = $repository->getLocationService(); |
617
|
|
|
$locationCreateStruct1 = $locationService->newLocationCreateStruct($imagesLocationId); |
618
|
|
|
$locationCreateStruct2 = $locationService->newLocationCreateStruct($filesLocationId); |
619
|
|
|
$locationCreateStructs = array($locationCreateStruct1, $locationCreateStruct2); |
620
|
|
|
|
621
|
|
|
// This call will return true |
622
|
|
|
$canUser = $permissionResolver->canUser( |
623
|
|
|
'content', |
624
|
|
|
'create', |
625
|
|
|
$contentCreateStruct, |
626
|
|
|
$locationCreateStructs |
627
|
|
|
); |
628
|
|
|
|
629
|
|
|
// Performing an action having necessary permissions will succeed |
630
|
|
|
$contentDraft = $contentService->createContent($contentCreateStruct, $locationCreateStructs); |
631
|
|
|
/* END: Use Case */ |
632
|
|
|
|
633
|
|
|
$this->assertTrue($canUser); |
634
|
|
|
$this->assertEquals( |
635
|
|
|
'My multipurpose folder', |
636
|
|
|
$contentDraft->getFieldValue('name')->text |
637
|
|
|
); |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* Test for the canUser() method. |
642
|
|
|
* |
643
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
644
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
645
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
646
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService |
647
|
|
|
* @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
648
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
649
|
|
|
*/ |
650
|
|
View Code Duplication |
public function testCanUserWithMultipleTargetsNo() |
651
|
|
|
{ |
652
|
|
|
$repository = $this->getRepository(); |
653
|
|
|
|
654
|
|
|
$homeLocationId = $this->generateId('location', 2); |
655
|
|
|
$administratorUsersLocationId = $this->generateId('location', 13); |
656
|
|
|
|
657
|
|
|
/* BEGIN: Use Case */ |
658
|
|
|
// $homeLocationId contains the ID of the "Home" location |
659
|
|
|
// $administratorUsersLocationId contains the ID of the "Administrator users" location |
660
|
|
|
|
661
|
|
|
$user = $this->createUserVersion1(); |
662
|
|
|
|
663
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
664
|
|
|
|
665
|
|
|
// Set created user as current user reference |
666
|
|
|
$permissionResolver->setCurrentUserReference($user); |
667
|
|
|
|
668
|
|
|
$contentTypeService = $repository->getContentTypeService(); |
669
|
|
|
|
670
|
|
|
$contentType = $contentTypeService->loadContentTypeByIdentifier('forums'); |
671
|
|
|
|
672
|
|
|
$contentService = $repository->getContentService(); |
673
|
|
|
|
674
|
|
|
$contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US'); |
675
|
|
|
$contentCreateStruct->setField('name', 'My awesome forums'); |
676
|
|
|
$contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789'; |
677
|
|
|
$contentCreateStruct->alwaysAvailable = true; |
678
|
|
|
|
679
|
|
|
$locationService = $repository->getLocationService(); |
680
|
|
|
$locationCreateStruct1 = $locationService->newLocationCreateStruct($homeLocationId); |
681
|
|
|
$locationCreateStruct2 = $locationService->newLocationCreateStruct($administratorUsersLocationId); |
682
|
|
|
$locationCreateStructs = array($locationCreateStruct1, $locationCreateStruct2); |
683
|
|
|
|
684
|
|
|
// This call will return false because user with Editor role does not have permission to |
685
|
|
|
// create content in the "Administrator users" location subtree |
686
|
|
|
$canUser = $permissionResolver->canUser( |
687
|
|
|
'content', |
688
|
|
|
'create', |
689
|
|
|
$contentCreateStruct, |
690
|
|
|
$locationCreateStructs |
691
|
|
|
); |
692
|
|
|
|
693
|
|
|
// Performing an action without necessary permissions will fail with "UnauthorizedException" |
694
|
|
|
if (!$canUser) { |
695
|
|
|
$contentDraft = $contentService->createContent($contentCreateStruct, $locationCreateStructs); |
|
|
|
|
696
|
|
|
} |
697
|
|
|
/* END: Use Case */ |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
/** |
701
|
|
|
* Test for the canUser() method. |
702
|
|
|
* |
703
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
704
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
705
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
706
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService |
707
|
|
|
* @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetURLAliasService |
708
|
|
|
* @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testSetCurrentUserReference |
709
|
|
|
* @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited |
710
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
711
|
|
|
*/ |
712
|
|
View Code Duplication |
public function testCanUserWithTargetThrowsInvalidArgumentException() |
713
|
|
|
{ |
714
|
|
|
$repository = $this->getRepository(); |
715
|
|
|
|
716
|
|
|
/* BEGIN: Use Case */ |
717
|
|
|
$user = $this->createUserVersion1(); |
718
|
|
|
|
719
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
720
|
|
|
|
721
|
|
|
// Set created user as current user reference |
722
|
|
|
$permissionResolver->setCurrentUserReference($user); |
723
|
|
|
|
724
|
|
|
$contentTypeService = $repository->getContentTypeService(); |
725
|
|
|
|
726
|
|
|
$contentType = $contentTypeService->loadContentTypeByIdentifier('forum'); |
727
|
|
|
|
728
|
|
|
$contentService = $repository->getContentService(); |
729
|
|
|
|
730
|
|
|
$contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US'); |
731
|
|
|
$contentCreateStruct->setField('name', 'My awesome forum'); |
732
|
|
|
$contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789'; |
733
|
|
|
$contentCreateStruct->alwaysAvailable = true; |
734
|
|
|
|
735
|
|
|
$urlAliasService = $repository->getURLAliasService(); |
736
|
|
|
$rootUrlAlias = $urlAliasService->lookup('/'); |
737
|
|
|
|
738
|
|
|
// This call will throw "InvalidArgumentException" because $rootAlias is not a valid target object |
739
|
|
|
$canUser = $permissionResolver->canUser( |
|
|
|
|
740
|
|
|
'content', |
741
|
|
|
'create', |
742
|
|
|
$contentCreateStruct, |
743
|
|
|
[$rootUrlAlias] |
744
|
|
|
); |
745
|
|
|
/* END: Use Case */ |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
/** |
749
|
|
|
* Test for the canUser() method. |
750
|
|
|
* |
751
|
|
|
* @see \eZ\Publish\API\Repository\PermissionResolver::canUser() |
752
|
|
|
* @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException |
753
|
|
|
*/ |
754
|
|
|
public function testCanUserThrowsBadStateException() |
755
|
|
|
{ |
756
|
|
|
$this->markTestIncomplete( |
757
|
|
|
'Cannot be tested on current fixture since policy with unsupported limitation value is not available.' |
758
|
|
|
); |
759
|
|
|
} |
760
|
|
|
|
761
|
|
|
/** |
762
|
|
|
* Test PermissionResolver::canUser for Users with different Limitations. |
763
|
|
|
* |
764
|
|
|
* @covers \eZ\Publish\API\Repository\PermissionResolver::canUser |
765
|
|
|
* |
766
|
|
|
* @dataProvider getDataForTestCanUserWithLimitations |
767
|
|
|
* |
768
|
|
|
* @param \eZ\Publish\API\Repository\Values\User\Limitation $limitation |
769
|
|
|
* @param string $module |
770
|
|
|
* @param string $function |
771
|
|
|
* @param \eZ\Publish\API\Repository\Values\ValueObject $object |
772
|
|
|
* @param array $targets |
773
|
|
|
* @param bool $expectedResult expected result of canUser check |
774
|
|
|
* |
775
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
776
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
777
|
|
|
*/ |
778
|
|
|
public function testCanUserWithLimitations( |
779
|
|
|
Limitation $limitation, |
780
|
|
|
$module, |
781
|
|
|
$function, |
782
|
|
|
ValueObject $object, |
783
|
|
|
array $targets, |
784
|
|
|
$expectedResult |
785
|
|
|
) { |
786
|
|
|
$repository = $this->getRepository(); |
787
|
|
|
$userService = $repository->getUserService(); |
788
|
|
|
$roleService = $repository->getRoleService(); |
789
|
|
|
$permissionResolver = $repository->getPermissionResolver(); |
790
|
|
|
|
791
|
|
|
$role = $this->createRoleWithPolicies( |
792
|
|
|
'role_' . __FUNCTION__, |
793
|
|
|
[ |
794
|
|
|
['module' => $module, 'function' => $function, 'limitations' => [$limitation]], |
795
|
|
|
] |
796
|
|
|
); |
797
|
|
|
// create user in root user group to avoid overlapping of existing policies and limitations |
798
|
|
|
$user = $this->createUser('user', 'John', 'Doe', $userService->loadUserGroup(4)); |
799
|
|
|
$roleLimitation = $limitation instanceof Limitation\RoleLimitation ? $limitation : null; |
800
|
|
|
$roleService->assignRoleToUser($role, $user, $roleLimitation); |
801
|
|
|
|
802
|
|
|
$permissionResolver->setCurrentUserReference($user); |
803
|
|
|
|
804
|
|
|
self::assertEquals( |
805
|
|
|
$expectedResult, |
806
|
|
|
$permissionResolver->canUser($module, $function, $object, $targets) |
807
|
|
|
); |
808
|
|
|
} |
809
|
|
|
|
810
|
|
|
/** |
811
|
|
|
* Data provider for testCanUserWithLimitations. |
812
|
|
|
* @see testCanUserWithLimitations |
813
|
|
|
* |
814
|
|
|
* @return array |
815
|
|
|
* |
816
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
817
|
|
|
*/ |
818
|
|
|
public function getDataForTestCanUserWithLimitations() |
819
|
|
|
{ |
820
|
|
|
$repository = $this->getRepository(); |
821
|
|
|
$contentService = $repository->getContentService(); |
822
|
|
|
$contentTypeService = $repository->getContentTypeService(); |
823
|
|
|
|
824
|
|
|
$contentType = $contentTypeService->loadContentTypeByIdentifier('folder'); |
825
|
|
|
|
826
|
|
|
$contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US'); |
827
|
|
|
$contentCreateStruct->sectionId = 2; |
828
|
|
|
|
829
|
|
|
// return data sets, numbered for readability and debugging |
830
|
|
|
return [ |
831
|
|
|
0 => [ |
832
|
|
|
new Limitation\SubtreeLimitation(['limitationValues' => ['/1/2/']]), |
833
|
|
|
'content', |
834
|
|
|
'create', |
835
|
|
|
$contentCreateStruct, |
836
|
|
|
[], |
837
|
|
|
false, |
838
|
|
|
], |
839
|
|
|
1 => [ |
840
|
|
|
new Limitation\SectionLimitation(['limitationValues' => [2]]), |
841
|
|
|
'content', |
842
|
|
|
'create', |
843
|
|
|
$contentCreateStruct, |
844
|
|
|
[], |
845
|
|
|
true, |
846
|
|
|
], |
847
|
|
|
2 => [ |
848
|
|
|
new Limitation\ParentContentTypeLimitation(['limitationValues' => [1]]), |
849
|
|
|
'content', |
850
|
|
|
'create', |
851
|
|
|
$contentCreateStruct, |
852
|
|
|
[], |
853
|
|
|
false, |
854
|
|
|
], |
855
|
|
|
]; |
856
|
|
|
} |
857
|
|
|
} |
858
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.