forked from ezsystems/ezpublish-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserHandler.php
676 lines (614 loc) · 22.2 KB
/
UserHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
<?php
/**
* File containing the User Handler inMemory impl
*
* @copyright Copyright (C) 1999-2013 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
*/
namespace eZ\Publish\Core\Persistence\InMemory;
use eZ\Publish\SPI\Persistence\User\Handler as UserHandlerInterface;
use eZ\Publish\SPI\Persistence\User;
use eZ\Publish\SPI\Persistence\User\Role;
use eZ\Publish\SPI\Persistence\User\RoleAssignment;
use eZ\Publish\SPI\Persistence\User\RoleUpdateStruct;
use eZ\Publish\SPI\Persistence\User\Policy;
use eZ\Publish\SPI\Persistence\Content;
use eZ\Publish\Core\Base\Exceptions\NotFoundException as NotFound;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
use LogicException;
/**
* Storage Engine handler for user module
*/
class UserHandler implements UserHandlerInterface
{
/**
* @var \eZ\Publish\Core\Persistence\InMemory\Handler
*/
protected $handler;
/**
* @var \eZ\Publish\Core\Persistence\InMemory\Backend
*/
protected $backend;
/**
* Setups current handler instance with reference to Handler object that created it.
*
* @param \eZ\Publish\Core\Persistence\InMemory\Handler $handler
* @param \eZ\Publish\Core\Persistence\InMemory\Backend $backend The storage engine backend
*/
public function __construct( Handler $handler, Backend $backend )
{
$this->handler = $handler;
$this->backend = $backend;
}
/**
* Create a user
*
* The User struct used to create the user will contain an ID which is used
* to reference the user.
*
* @param \eZ\Publish\SPI\Persistence\User $user
*
* @throws LogicException If no id was provided or if it already exists
*
* @return \eZ\Publish\SPI\Persistence\User
*/
public function create( User $user )
{
$userArr = (array)$user;
return $this->backend->create( 'User', $userArr, false );
}
/**
* Loads user with user ID.
*
* @param mixed $userId
*
* @return \eZ\Publish\SPI\Persistence\User
*/
public function load( $userId )
{
return $this->backend->load( 'User', $userId );
}
/**
* Loads user with user login.
*
* @param string $login
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If user is not found
*
* @return \eZ\Publish\SPI\Persistence\User
*/
public function loadByLogin( $login )
{
$users = $this->backend->find( 'User', array( 'login' => $login ) );
if ( empty( $users ) )
throw new NotFound( 'User', $login );
if ( isset( $users[1] ) )
throw new LogicException( "Found more then one user with login '{$login}'" );
return $users[0];
}
/**
* Loads user(s) with user email.
*
* As earlier eZ Publish versions supported several users having same email (ini config),
* this function may return several users.
*
* @param string $email
*
* @return \eZ\Publish\SPI\Persistence\User[]
*/
public function loadByEmail( $email )
{
return $this->backend->find( 'User', array( 'email' => $email ) );
}
/**
* Update the user information specified by the user struct
*
* @param \eZ\Publish\SPI\Persistence\User $user
*/
public function update( User $user )
{
$userArr = (array)$user;
$this->backend->update( 'User', $userArr['id'], $userArr );
}
/**
* Delete user with the given ID.
*
* @param mixed $userId
*/
public function delete( $userId )
{
try
{
$this->backend->delete( 'User', $userId );
$this->backend->deleteByMatch( 'User\\RoleAssignment', array( 'contentId' => $userId ) );
}
catch ( NotFound $e )
{
// Do nothing, we do not throw if User or RoleAssignments do not exist
}
}
/**
* Create new role
*
* @param \eZ\Publish\SPI\Persistence\User\Role $role
*
* @return \eZ\Publish\SPI\Persistence\User\Role
*/
public function createRole( Role $role )
{
if ( !$role->identifier )
throw new InvalidArgumentValue( '$role->identifier', $role->identifier );
$roleArr = (array)$role;
$roleArr['policies'] = array();
$newRole = $this->backend->create( 'User\\Role', $roleArr );
foreach ( $role->policies as $policy )
{
$newRole->policies[] = $this->addPolicy( $newRole->id, $policy );
}
return $newRole;
}
/**
* Loads a specified role by id
*
* @param mixed $roleId
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If role is not found
*
* @return \eZ\Publish\SPI\Persistence\User\Role
*/
public function loadRole( $roleId )
{
$list = $this->backend->find(
'User\\Role',
array( 'id' => $roleId ),
array(
'policies' => array(
'type' => 'User\\Policy',
'match' => array( 'roleId' => 'id' )
)
)
);
if ( !$list )
throw new NotFound( 'User\\Role', $roleId );
return $list[0];
}
/**
* Loads a specified role by $identifier
*
* @param string $identifier
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If role is not found
*
* @return \eZ\Publish\SPI\Persistence\User\Role
*/
public function loadRoleByIdentifier( $identifier )
{
$list = $this->backend->find(
'User\\Role',
array( 'identifier' => $identifier ),
array(
'policies' => array(
'type' => 'User\\Policy',
'match' => array( 'roleId' => 'id' )
)
)
);
if ( !$list )
throw new NotFound( 'User\\Role', $identifier );
return $list[0];
}
/**
* Loads all roles
*
* @return \eZ\Publish\SPI\Persistence\User\Role[]
*/
public function loadRoles()
{
return $this->backend->find(
'User\\Role',
array(),
array(
'policies' => array(
'type' => 'User\\Policy',
'match' => array( 'roleId' => 'id' )
)
)
);
}
/**
* Loads roles assignments Role
*
* Role Assignments with same roleId and limitationIdentifier will be merged together into one.
*
* @param mixed $roleId
*
* @return \eZ\Publish\SPI\Persistence\User\RoleAssignment[]
*/
public function loadRoleAssignmentsByRoleId( $roleId )
{
throw new \eZ\Publish\API\Repository\Exceptions\NotImplementedException( __METHOD__ );
}
/**
* Loads roles assignments to a user/group
*
* Role Assignments with same roleId and limitationIdentifier will be merged together into one.
*
* @param mixed $groupId In legacy storage engine this is the content object id roles are assigned to in ezuser_role.
* By the nature of legacy this can currently also be used to get by $userId.
* @param boolean $inherit If true also return inherited role assignments from user groups.
*
* @throws \LogicException Internal data corruption error
* @uses getRoleAssignmentForContent()
*
* @return \eZ\Publish\SPI\Persistence\User\RoleAssignment[]
*/
public function loadRoleAssignmentsByGroupId( $groupId, $inherit = false )
{
if ( $inherit === false )
return $this->getRoleAssignmentForContent( array( $groupId ) );
$contentIds = array( $groupId );
$locations = $this->handler->locationHandler()->loadLocationsByContent( $groupId );
if ( empty( $locations ) )
return array();
// crawl up path on all locations
foreach ( $locations as $location )
{
$parentIds = array_reverse( explode( '/', trim( $location->pathString, '/' ) ) );
foreach ( $parentIds as $parentId )
{
if ( $parentId == $location->id )
continue;
$location = $this->backend->load( 'Content\\Location', $parentId );
$list = $this->backend->find(
'Content',
array( 'id' => $location->contentId ),
array(
'versionInfo' => array(
'type' => 'Content\\VersionInfo',
'match' => array( '_contentId' => 'id', 'versionNo' => '_currentVersionNo' ),
'single' => true,
'sub' => array(
'contentInfo' => array(
'type' => 'Content\\ContentInfo',
'match' => array( 'id' => '_contentId' ),
'single' => true
),
)
),
)
);
if ( isset( $list[1] ) )
throw new LogicException( "'content tree' logic error, there is more than one item with parentId: $parentId" );
if ( $list )
$contentIds[] = $list[0]->versionInfo->contentInfo->id;
}
}
return $this->getRoleAssignmentForContent( $contentIds );
}
/**
* @param array $contentIds
*
* @return \eZ\Publish\SPI\Persistence\User\RoleAssignment[]
*/
protected function getRoleAssignmentForContent( array $contentIds )
{
/** fetch possible roles assigned to this object
* @var \eZ\Publish\SPI\Persistence\User\RoleAssignment[] $list
*/
$list = $this->backend->find(
'User\\RoleAssignment',
array( 'contentId' => $contentIds )
);
// merge policies
$data = array();
foreach ( $list as $new )
{
// if user already have full access to a role, continue
if ( isset( $data[$new->roleId][$new->contentId] )
&& $data[$new->roleId][$new->contentId] instanceof RoleAssignment )
continue;
if ( !empty( $new->limitationIdentifier ) )
{
if ( !isset( $data[$new->roleId][$new->contentId][$new->limitationIdentifier] ) )
{
$new->values = array( $new->values );
$data[$new->roleId][$new->contentId][$new->limitationIdentifier] = $new;
}
// merge limitation values
else
{
$data[$new->roleId][$new->contentId][$new->limitationIdentifier]->values[] = $new->values;
}
}
else
{
$data[$new->roleId][$new->contentId] = $new;
}
}
// flatten structure
$roleAssignments = array();
array_walk_recursive(
$data,
function ( $roleAssignment ) use ( &$roleAssignments )
{
$roleAssignments[] = $roleAssignment;
}
);
return $roleAssignments;
}
/**
* Update role
*
* @param \eZ\Publish\SPI\Persistence\User\RoleUpdateStruct $role
*/
public function updateRole( RoleUpdateStruct $role )
{
if ( !$role->id )
throw new InvalidArgumentValue( '$role->id', $role->id );
if ( !$role->identifier )
throw new InvalidArgumentValue( '$role->identifier', $role->identifier );
$roleArr = (array)$role;
$this->backend->update( 'User\\Role', $roleArr['id'], $roleArr );
}
/**
* Delete the specified role
*
* @param mixed $roleId
*/
public function deleteRole( $roleId )
{
$this->backend->delete( 'User\\Role', $roleId );
$this->backend->deleteByMatch( 'User\\Policy', array( 'roleId' => $roleId ) );
$this->backend->deleteByMatch( 'User\\RoleAssignment', array( 'roleId' => $roleId ) );
}
/**
* Adds a policy to a role
*
* @param mixed $roleId
* @param \eZ\Publish\SPI\Persistence\User\Policy $policy
*
* @return \eZ\Publish\SPI\Persistence\User\Policy
* @todo Throw on invalid Role Id?
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue If $policy->limitation is empty (null, empty string/array..)
*/
public function addPolicy( $roleId, Policy $policy )
{
if ( empty( $policy->limitations ) )
throw new InvalidArgumentValue( '->limitations', $policy->limitations, get_class( $policy ) );
$policyArr = array( 'roleId' => $roleId ) + ( (array)$policy );
return $this->backend->create( 'User\\Policy', $policyArr );
}
/**
* Update a policy
*
* Replaces limitations values with new values.
*
* @param \eZ\Publish\SPI\Persistence\User\Policy $policy
*
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue If $policy->limitation is empty (null, empty string/array..)
*/
public function updatePolicy( Policy $policy )
{
if ( empty( $policy->limitations ) )
throw new InvalidArgumentValue( '->limitations', $policy->limitations, get_class( $policy ) );
$policyArr = (array)$policy;
$this->backend->update( 'User\\Policy', $policyArr['id'], $policyArr, false );
}
/**
* Removes a policy from a role
*
* @param mixed $roleId
* @param mixed $policyId
*
* @todo Throw exception on missing role / policy?
*
* @return void
*/
public function removePolicy( $roleId, $policyId )
{
$this->backend->deleteByMatch( 'User\\Policy', array( 'id' => $policyId, 'roleId' => $roleId ) );
}
/**
* Returns the user policies associated with the user (including inherited policies from user groups)
*
* @param mixed $userId
*
* @return \eZ\Publish\SPI\Persistence\User\Policy[]
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If user (it's content object atm) is not found
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If user is not of user Content Type
*/
public function loadPoliciesByUserId( $userId )
{
$list = $this->backend->find(
'Content',
array( 'id' => $userId ),
array(
'versionInfo' => array(
'type' => 'Content\\VersionInfo',
'match' => array( '_contentId' => 'id', 'versionNo' => '_currentVersionNo' ),
'single' => true,
'sub' => array(
'contentInfo' => array(
'type' => 'Content\\ContentInfo',
'match' => array( 'id' => '_contentId' ),
'single' => true
),
)
)
)
);
if ( !$list )
throw new NotFound( 'User', $userId );
$policies = array();
$this->getPermissionsForObject( $list[0], 4, $policies );
// crawl up path on all locations
$locations = $this->handler->locationHandler()->loadLocationsByContent(
$list[0]->versionInfo->contentInfo->id
);
foreach ( $locations as $location )
{
$parentIds = array_reverse( explode( '/', trim( $location->pathString, '/' ) ) );
foreach ( $parentIds as $parentId )
{
if ( $parentId == $location->id )
continue;
$location = $this->backend->load( 'Content\\Location', $parentId );
$list2 = $this->backend->find(
'Content',
array( 'id' => $location->contentId ),
array(
'versionInfo' => array(
'type' => 'Content\\VersionInfo',
'match' => array( '_contentId' => 'id', 'versionNo' => '_currentVersionNo' ),
'single' => true,
'sub' => array(
'contentInfo' => array(
'type' => 'Content\\ContentInfo',
'match' => array( 'id' => '_contentId' ),
'single' => true
),
)
)
)
);
if ( isset( $list2[1] ) )
throw new LogicException( "'content tree' logic error, there is more than one item with parentId: $parentId" );
if ( $list2 )
$this->getPermissionsForObject( $list2[0], 3, $policies );
}
}
return array_values( $policies );
}
/**
* @param \eZ\Publish\SPI\Persistence\Content $content
* @param mixed $typeId
* @param array $policies
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If $content is not type $typeId
*
* @return void
*/
protected function getPermissionsForObject( Content $content, $typeId, array &$policies )
{
if ( $content->versionInfo->contentInfo->contentTypeId != $typeId )
throw new NotFound( "Content with TypeId:$typeId", $content->versionInfo->contentInfo->id );
// fetch possible roles assigned to this object
$list = $this->backend->find(
'User\\Role',
array( 'groupIds' => $content->versionInfo->contentInfo->id ),
array(
'policies' => array(
'type' => 'User\\Policy',
'match' => array( 'roleId' => 'id' )
)
)
);
// merge policies
foreach ( $list as $role )
{
foreach ( $role->policies as $policy )
{
if ( !isset( $policies[$policy->id] ) )
$policies[$policy->id] = $policy;
}
}
}
/**
* Assigns role to a user or user group with given limitations
*
* The limitation array looks like:
* <code>
* array(
* 'Subtree' => array(
* '/1/2/',
* '/1/4/',
* ),
* 'Foo' => array( 'Bar' ),
* …
* )
* </code>
*
* Where the keys are the limitation identifiers, and the respective values
* are an array of limitation values. The limitation parameter is optional.
*
* @param mixed $contentId The groupId or userId to assign the role to.
* @param mixed $roleId
* @param array $limitation
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If group or role is not found
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If group is not of user_group Content Type
*/
public function assignRole( $contentId, $roleId, array $limitation = null )
{
$content = $this->backend->load( 'Content\\ContentInfo', $contentId );
if ( !$content )
throw new NotFound( 'User Group', $contentId );
$role = $this->backend->load( 'User\\Role', $roleId );
if ( !$role )
throw new NotFound( 'Role', $roleId );
// @todo Use eZ Publish settings for this, and maybe a better exception
if ( $content->contentTypeId != 3 && $content->contentTypeId != 4 )
throw new NotFound( "Content", $contentId );
if ( is_array( $limitation ) )
{
foreach ( $limitation as $limitIdentifier => $limitValues )
{
$this->backend->create(
'User\\RoleAssignment',
array(
'roleId' => $roleId,
'contentId' => $contentId,
'limitationIdentifier' => $limitIdentifier,
'values' => $limitValues
)
);
}
}
else
{
$this->backend->create(
'User\\RoleAssignment',
array(
'roleId' => $roleId,
'contentId' => $contentId,
'limitationIdentifier' => null,
'values' => null
)
);
}
$role->groupIds[] = $contentId;
$this->backend->update( 'User\\Role', $roleId, (array)$role );
}
/**
* Un-assign a role
*
* @param mixed $contentId The user or user group Id to un-assign the role from.
* @param mixed $roleId
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If group or role is not found
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If group is not of user[_group] Content Type
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue If group does not contain role
*/
public function unAssignRole( $contentId, $roleId )
{
$content = $this->backend->load( 'Content\\ContentInfo', $contentId );
if ( !$content )
throw new NotFound( 'User Group', $contentId );
$role = $this->backend->load( 'User\\Role', $roleId );
if ( !$role )
throw new NotFound( 'Role', $roleId );
// @todo Use eZ Publish settings for this, and maybe a better exception
if ( $content->contentTypeId != 3 && $content->contentTypeId != 4 )
throw new NotFound( "3 or 4", $contentId );
$roleAssignments = $this->backend->find(
'User\\RoleAssignment',
array( 'roleId' => $roleId, 'contentId' => $contentId )
);
if ( empty( $roleAssignments ) )
throw new InvalidArgumentValue( '$roleId', $roleId );
$this->backend->deleteByMatch( 'User\\RoleAssignment', array( 'roleId' => $roleId, 'contentId' => $contentId ) );
$role->groupIds = array_values( array_diff( $role->groupIds, array( $contentId ) ) );
$this->backend->update( 'User\\Role', $roleId, (array)$role );
}
}