forked from ezsystems/ezpublish-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoleService.php
1248 lines (1126 loc) · 46.2 KB
/
RoleService.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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* File containing the eZ\Publish\Core\Repository\RoleService class.
*
* @copyright Copyright (C) 1999-2014 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\Repository;
use eZ\Publish\Core\Base\Exceptions\LimitationValidationException;
use eZ\Publish\Core\Repository\Values\User\PolicyUpdateStruct;
use eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct as APIPolicyUpdateStruct;
use eZ\Publish\Core\Repository\Values\User\Policy;
use eZ\Publish\API\Repository\Values\User\Policy as APIPolicy;
use eZ\Publish\API\Repository\Values\User\RoleUpdateStruct;
use eZ\Publish\Core\Repository\Values\User\PolicyCreateStruct;
use eZ\Publish\API\Repository\Values\User\PolicyCreateStruct as APIPolicyCreateStruct;
use eZ\Publish\Core\Repository\Values\User\Role;
use eZ\Publish\API\Repository\Values\User\Role as APIRole;
use eZ\Publish\Core\Repository\Values\User\RoleCreateStruct;
use eZ\Publish\API\Repository\Values\User\RoleCreateStruct as APIRoleCreateStruct;
use eZ\Publish\Core\Repository\Values\User\UserRoleAssignment;
use eZ\Publish\Core\Repository\Values\User\UserGroupRoleAssignment;
use eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation;
use eZ\Publish\API\Repository\Values\User\Limitation;
use eZ\Publish\API\Repository\Values\User\User;
use eZ\Publish\API\Repository\Values\User\UserGroup;
use eZ\Publish\SPI\Persistence\User\Policy as SPIPolicy;
use eZ\Publish\SPI\Persistence\User\RoleAssignment as SPIRoleAssignment;
use eZ\Publish\SPI\Persistence\User\Role as SPIRole;
use eZ\Publish\SPI\Persistence\User\RoleUpdateStruct as SPIRoleUpdateStruct;
use eZ\Publish\API\Repository\RoleService as RoleServiceInterface;
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
use eZ\Publish\SPI\Persistence\User\Handler;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
use eZ\Publish\Core\Base\Exceptions\BadStateException;
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException;
use Exception;
/**
* This service provides methods for managing Roles and Policies
*
* @package eZ\Publish\Core\Repository
*/
class RoleService implements RoleServiceInterface
{
/**
* @var \eZ\Publish\API\Repository\Repository
*/
protected $repository;
/**
* @var \eZ\Publish\SPI\Persistence\User\Handler
*/
protected $userHandler;
/**
* @var array
*/
protected $settings;
/**
* Setups service with reference to repository object that created it & corresponding handler
*
* @param \eZ\Publish\API\Repository\Repository $repository
* @param \eZ\Publish\SPI\Persistence\User\Handler $userHandler
* @param array $settings
*/
public function __construct( RepositoryInterface $repository, Handler $userHandler, array $settings = array() )
{
$this->repository = $repository;
$this->userHandler = $userHandler;
// Union makes sure default settings are ignored if provided in argument
$this->settings = $settings + array(
'limitationTypes' => array(),
'limitationMap' => array(
// @todo Inject these dynamically by activated eZ Controllers, see PR #252
'content' => array(
'read' => array( 'Class' => true, 'Section' => true, 'Owner' => true, 'Group' => true, 'Node' => true, 'Subtree' => true, 'State' => true ),
'diff' => array( 'Class' => true, 'Section' => true, 'Owner' => true, 'Node' => true, 'Subtree' => true ),
'view_embed' => array( 'Class' => true, 'Section' => true, 'Owner' => true, 'Node' => true, 'Subtree' => true ),
'create' => array( 'Class' => true, 'Section' => true, 'ParentOwner' => true, 'ParentGroup' => true, 'ParentClass' => true, 'ParentDepth' => true, 'Node' => true, 'Subtree' => true, 'Language' => true ),
'edit' => array( 'Class' => true, 'Section' => true, 'Owner' => true, 'Group' => true, 'Node' => true, 'Subtree' => true, 'Language' => true, 'State' => true ),
'manage_locations' => array( 'Class' => true, 'Section' => true, 'Owner' => true, 'Subtree' => true ),
'hide' => array( 'Class' => true, 'Section' => true, 'Owner' => true, 'Group' => true, 'Node' => true, 'Subtree' => true, 'Language' => true ),
'translate' => array( 'Class' => true, 'Section' => true, 'Owner' => true, 'Node' => true, 'Subtree' => true, 'Language' => true ),
'remove' => array( 'Class' => true, 'Section' => true, 'Owner' => true, 'Node' => true, 'Subtree' => true, 'State' => true ),
'versionread' => array( 'Class' => true, 'Section' => true, 'Owner' => true, 'Status' => true, 'Node' => true, 'Subtree' => true ),
'versionremove' => array( 'Class' => true, 'Section' => true, 'Owner' => true, 'Status' => true, 'Node' => true, 'Subtree' => true ),
'pdf' => array( 'Class' => true, 'Section' => true, 'Owner' => true, 'Node' => true, 'Subtree' => true ),
),
'section' => array(
'assign' => array( 'Class' => true, 'Section' => true, 'Owner' => true, 'NewSection' => true ),
),
'state' => array(
// @todo 'NewState' Limitation and Limitation type is missing (like 'NewSection')
'assign' => array( 'Class' => true, 'Section' => true, 'Owner' => true, 'Group' => true, 'Node' => true, 'Subtree' => true, 'State' => true, 'NewState' => true ),
),
'user' => array(
'login' => array( 'SiteAccess' => true ),
)
),
);
}
/**
* Creates a new Role
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a role
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the name of the role already exists or if limitation of the
* same type is repeated in the policy create struct or if
* limitation is not allowed on module/function
* @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a policy limitation in the $roleCreateStruct is not valid
*
* @param \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $roleCreateStruct
*
* @return \eZ\Publish\API\Repository\Values\User\Role
*/
public function createRole( APIRoleCreateStruct $roleCreateStruct )
{
if ( !is_string( $roleCreateStruct->identifier ) || empty( $roleCreateStruct->identifier ) )
throw new InvalidArgumentValue( "identifier", $roleCreateStruct->identifier, "RoleCreateStruct" );
if ( $this->repository->hasAccess( 'role', 'create' ) !== true )
throw new UnauthorizedException( 'role', 'create' );
try
{
$existingRole = $this->loadRoleByIdentifier( $roleCreateStruct->identifier );
if ( $existingRole !== null )
throw new InvalidArgumentException( "roleCreateStruct", "role with specified identifier already exists" );
}
catch ( APINotFoundException $e )
{
// Do nothing
}
$limitationValidationErrors = $this->validateRoleCreateStruct( $roleCreateStruct );
if ( !empty( $limitationValidationErrors ) )
{
throw new LimitationValidationException( $limitationValidationErrors );
}
$spiRole = $this->buildPersistenceRoleObject( $roleCreateStruct );
$this->repository->beginTransaction();
try
{
$createdRole = $this->userHandler->createRole( $spiRole );
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
return $this->buildDomainRoleObject( $createdRole );
}
/**
* Updates the name of the role
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to update a role
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the name of the role already exists
*
* @param \eZ\Publish\API\Repository\Values\User\Role $role
* @param \eZ\Publish\API\Repository\Values\User\RoleUpdateStruct $roleUpdateStruct
*
* @return \eZ\Publish\API\Repository\Values\User\Role
*/
public function updateRole( APIRole $role, RoleUpdateStruct $roleUpdateStruct )
{
if ( $roleUpdateStruct->identifier !== null && !is_string( $roleUpdateStruct->identifier ) )
throw new InvalidArgumentValue( "identifier", $roleUpdateStruct->identifier, "RoleUpdateStruct" );
$loadedRole = $this->loadRole( $role->id );
if ( $this->repository->hasAccess( 'role', 'update' ) !== true )
throw new UnauthorizedException( 'role', 'update' );
if ( $roleUpdateStruct->identifier !== null )
{
try
{
$existingRole = $this->loadRoleByIdentifier( $roleUpdateStruct->identifier );
if ( $existingRole->id != $loadedRole->id )
{
throw new InvalidArgumentException(
"\$roleUpdateStruct",
"Role with provided identifier already exists"
);
}
}
catch ( APINotFoundException $e )
{
// Do nothing
}
}
$this->repository->beginTransaction();
try
{
$this->userHandler->updateRole(
new SPIRoleUpdateStruct(
array(
'id' => $loadedRole->id,
'identifier' => $roleUpdateStruct->identifier ?: $loadedRole->identifier
)
)
);
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
return $this->loadRole( $loadedRole->id );
}
/**
* Adds a new policy to the role
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to add a policy
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if limitation of the same type is repeated in policy create
* struct or if limitation is not allowed on module/function
* @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a limitation in the $policyCreateStruct is not valid
*
* @param \eZ\Publish\API\Repository\Values\User\Role $role
* @param \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct $policyCreateStruct
*
* @return \eZ\Publish\API\Repository\Values\User\Role
*/
public function addPolicy( APIRole $role, APIPolicyCreateStruct $policyCreateStruct )
{
if ( !is_string( $policyCreateStruct->module ) || empty( $policyCreateStruct->module ) )
throw new InvalidArgumentValue( "module", $policyCreateStruct->module, "PolicyCreateStruct" );
if ( !is_string( $policyCreateStruct->function ) || empty( $policyCreateStruct->function ) )
throw new InvalidArgumentValue( "function", $policyCreateStruct->function, "PolicyCreateStruct" );
if ( $policyCreateStruct->module === '*' && $policyCreateStruct->function !== '*' )
throw new InvalidArgumentValue( "module", $policyCreateStruct->module, "PolicyCreateStruct" );
if ( $this->repository->hasAccess( 'role', 'update' ) !== true )
throw new UnauthorizedException( 'role', 'update' );
$loadedRole = $this->loadRole( $role->id );
$limitations = $policyCreateStruct->getLimitations();
$limitationValidationErrors = $this->validatePolicy(
$policyCreateStruct->module,
$policyCreateStruct->function,
$limitations
);
if ( !empty( $limitationValidationErrors ) )
{
throw new LimitationValidationException( $limitationValidationErrors );
}
$spiPolicy = $this->buildPersistencePolicyObject(
$policyCreateStruct->module,
$policyCreateStruct->function,
$limitations
);
$this->repository->beginTransaction();
try
{
$this->userHandler->addPolicy( $loadedRole->id, $spiPolicy );
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
return $this->loadRole( $loadedRole->id );
}
/**
* removes a policy from the role
*
* @deprecated since 5.3, use {@link deletePolicy()} instead.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove a policy
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if policy does not belong to the given role
*
* @param \eZ\Publish\API\Repository\Values\User\Role $role
* @param \eZ\Publish\API\Repository\Values\User\Policy $policy the policy to remove from the role
*
* @return \eZ\Publish\API\Repository\Values\User\Role the updated role
*/
public function removePolicy( APIRole $role, APIPolicy $policy )
{
if ( $this->repository->hasAccess( 'role', 'update' ) !== true )
throw new UnauthorizedException( 'role', 'update' );
if ( $policy->roleId != $role->id )
{
throw new InvalidArgumentException( "\$policy", "Policy does not belong to the given role" );
}
$this->internalDeletePolicy( $policy );
return $this->loadRole( $role->id );
}
/**
* Deletes a policy
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove a policy
*
* @param \eZ\Publish\API\Repository\Values\User\Policy $policy the policy to delete
*/
public function deletePolicy( APIPolicy $policy )
{
if ( $this->repository->hasAccess( 'role', 'update' ) !== true )
throw new UnauthorizedException( 'role', 'update' );
$this->internalDeletePolicy( $policy );
}
/**
* Deletes a policy
*
* Used by {@link removePolicy()} and {@link deletePolicy()}
*
* @param APIPolicy $policy
*
* @throws \Exception
*
* @return void
*/
protected function internalDeletePolicy( APIPolicy $policy )
{
$this->repository->beginTransaction();
try
{
$this->userHandler->deletePolicy( $policy->id );
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
}
/**
* Updates the limitations of a policy. The module and function cannot be changed and
* the limitations are replaced by the ones in $roleUpdateStruct
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to update a policy
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if limitation of the same type is repeated in policy update
* struct or if limitation is not allowed on module/function
* @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a limitation in the $policyUpdateStruct is not valid
*
* @param \eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct $policyUpdateStruct
* @param \eZ\Publish\API\Repository\Values\User\Policy $policy
*
* @return \eZ\Publish\API\Repository\Values\User\Policy
*/
public function updatePolicy( APIPolicy $policy, APIPolicyUpdateStruct $policyUpdateStruct )
{
if ( !is_string( $policy->module ) )
throw new InvalidArgumentValue( "module", $policy->module, "Policy" );
if ( !is_string( $policy->function ) )
throw new InvalidArgumentValue( "function", $policy->function, "Policy" );
if ( $this->repository->hasAccess( 'role', 'update' ) !== true )
throw new UnauthorizedException( 'role', 'update' );
$limitations = $policyUpdateStruct->getLimitations();
$limitationValidationErrors = $this->validatePolicy(
$policy->module,
$policy->function,
$limitations
);
if ( !empty( $limitationValidationErrors ) )
{
throw new LimitationValidationException( $limitationValidationErrors );
}
$spiPolicy = $this->buildPersistencePolicyObject(
$policy->module,
$policy->function,
$limitations
);
$spiPolicy->id = $policy->id;
$spiPolicy->roleId = $policy->roleId;
$this->repository->beginTransaction();
try
{
$this->userHandler->updatePolicy( $spiPolicy );
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
return $this->buildDomainPolicyObject( $spiPolicy );
}
/**
* Loads a role for the given id
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read this role
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a role with the given id was not found
*
* @param mixed $id
*
* @return \eZ\Publish\API\Repository\Values\User\Role
*/
public function loadRole( $id )
{
if ( $this->repository->hasAccess( 'role', 'read' ) !== true )
throw new UnauthorizedException( 'role', 'read' );
$spiRole = $this->userHandler->loadRole( $id );
return $this->buildDomainRoleObject( $spiRole );
}
/**
* Loads a role for the given identifier
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read this role
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a role with the given name was not found
*
* @param string $identifier
*
* @return \eZ\Publish\API\Repository\Values\User\Role
*/
public function loadRoleByIdentifier( $identifier )
{
if ( !is_string( $identifier ) )
throw new InvalidArgumentValue( "identifier", $identifier );
if ( $this->repository->hasAccess( 'role', 'read' ) !== true )
throw new UnauthorizedException( 'role', 'read' );
$spiRole = $this->userHandler->loadRoleByIdentifier( $identifier );
return $this->buildDomainRoleObject( $spiRole );
}
/**
* Loads all roles
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read the roles
*
* @return \eZ\Publish\API\Repository\Values\User\Role[]
*/
public function loadRoles()
{
if ( $this->repository->hasAccess( 'role', 'read' ) !== true )
throw new UnauthorizedException( 'role', 'read' );
$spiRoles = $this->userHandler->loadRoles();
$roles = array();
foreach ( $spiRoles as $spiRole )
{
$roles[] = $this->buildDomainRoleObject( $spiRole );
}
return $roles;
}
/**
* Deletes the given role
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to delete this role
*
* @param \eZ\Publish\API\Repository\Values\User\Role $role
*/
public function deleteRole( APIRole $role )
{
if ( $this->repository->hasAccess( 'role', 'delete' ) !== true )
throw new UnauthorizedException( 'role', 'delete' );
$loadedRole = $this->loadRole( $role->id );
$this->repository->beginTransaction();
try
{
$this->userHandler->deleteRole( $loadedRole->id );
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
}
/**
* Loads all policies from roles which are assigned to a user or to user groups to which the user belongs
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a user with the given id was not found
*
* @param mixed $userId
*
* @return \eZ\Publish\API\Repository\Values\User\Policy[]
*/
public function loadPoliciesByUserId( $userId )
{
$spiPolicies = $this->userHandler->loadPoliciesByUserId( $userId );
$policies = array();
foreach ( $spiPolicies as $spiPolicy )
{
$policies[] = $this->buildDomainPolicyObject( $spiPolicy );
}
if ( empty( $policies ) )
$this->userHandler->load( $userId );// For NotFoundException in case userId is invalid
return $policies;
}
/**
* Assigns a role to the given user group
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to assign a role
* @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if $roleLimitation is not valid
*
* @param \eZ\Publish\API\Repository\Values\User\Role $role
* @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
* @param \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $roleLimitation an optional role limitation (which is either a subtree limitation or section limitation)
*/
public function assignRoleToUserGroup( APIRole $role, UserGroup $userGroup, RoleLimitation $roleLimitation = null )
{
if ( $this->repository->canUser( 'role', 'assign', $userGroup, $role ) !== true )
throw new UnauthorizedException( 'role', 'assign' );
if ( $roleLimitation === null )
{
$limitation = null;
}
else
{
$limitationValidationErrors = $this->validateLimitation( $roleLimitation );
if ( !empty( $limitationValidationErrors ) )
{
throw new LimitationValidationException( $limitationValidationErrors );
}
$limitation = array( $roleLimitation->getIdentifier() => $roleLimitation->limitationValues );
}
$loadedRole = $this->loadRole( $role->id );
$loadedUserGroup = $this->repository->getUserService()->loadUserGroup( $userGroup->id );
$this->repository->beginTransaction();
try
{
$this->userHandler->assignRole(
$loadedUserGroup->id,
$loadedRole->id,
$limitation
);
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
}
/**
* removes a role from the given user group.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove a role
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the role is not assigned to the given user group
*
* @param \eZ\Publish\API\Repository\Values\User\Role $role
* @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
*/
public function unassignRoleFromUserGroup( APIRole $role, UserGroup $userGroup )
{
if ( $this->repository->canUser( 'role', 'assign', $userGroup, $role ) !== true )
throw new UnauthorizedException( 'role', 'assign' );
$spiRoleAssignments = $this->userHandler->loadRoleAssignmentsByGroupId( $userGroup->id );
$isAssigned = false;
foreach ( $spiRoleAssignments as $spiRoleAssignment )
{
if ( $spiRoleAssignment->roleId === $role->id )
{
$isAssigned = true;
break;
}
}
if ( !$isAssigned )
{
throw new InvalidArgumentException(
"\$userGroup",
"Role is not assigned to the given UserGroup"
);
}
$this->repository->beginTransaction();
try
{
$this->userHandler->unAssignRole( $userGroup->id, $role->id );
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
}
/**
* Assigns a role to the given user
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to assign a role
* @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if $roleLimitation is not valid
*
* @param \eZ\Publish\API\Repository\Values\User\Role $role
* @param \eZ\Publish\API\Repository\Values\User\User $user
* @param \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $roleLimitation an optional role limitation (which is either a subtree limitation or section limitation)
*/
public function assignRoleToUser( APIRole $role, User $user, RoleLimitation $roleLimitation = null )
{
if ( $this->repository->canUser( 'role', 'assign', $user, $role ) !== true )
throw new UnauthorizedException( 'role', 'assign' );
if ( $roleLimitation === null )
{
$limitation = null;
}
else
{
$limitationValidationErrors = $this->validateLimitation( $roleLimitation );
if ( !empty( $limitationValidationErrors ) )
{
throw new LimitationValidationException( $limitationValidationErrors );
}
$limitation = array( $roleLimitation->getIdentifier() => $roleLimitation->limitationValues );
}
$loadedRole = $this->loadRole( $role->id );
$loadedUser = $this->repository->getUserService()->loadUser( $user->id );
$this->repository->beginTransaction();
try
{
$this->userHandler->assignRole(
$loadedUser->id,
$loadedRole->id,
$limitation
);
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
}
/**
* removes a role from the given user.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove a role
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the role is not assigned to the user
*
* @param \eZ\Publish\API\Repository\Values\User\Role $role
* @param \eZ\Publish\API\Repository\Values\User\User $user
*/
public function unassignRoleFromUser( APIRole $role, User $user )
{
if ( $this->repository->canUser( 'role', 'assign', $user, $role ) !== true )
throw new UnauthorizedException( 'role', 'assign' );
$spiRoleAssignments = $this->userHandler->loadRoleAssignmentsByGroupId( $user->id );
$isAssigned = false;
foreach ( $spiRoleAssignments as $spiRoleAssignment )
{
if ( $spiRoleAssignment->roleId === $role->id )
{
$isAssigned = true;
break;
}
}
if ( !$isAssigned )
{
throw new InvalidArgumentException(
"\$user",
"Role is not assigned to the given User"
);
}
$this->repository->beginTransaction();
try
{
$this->userHandler->unAssignRole( $user->id, $role->id );
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
}
/**
* Returns the assigned user and user groups to this role
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read a role
*
* @param \eZ\Publish\API\Repository\Values\User\Role $role
*
* @return \eZ\Publish\API\Repository\Values\User\RoleAssignment[]
*/
public function getRoleAssignments( APIRole $role )
{
if ( $this->repository->hasAccess( 'role', 'read' ) !== true )
throw new UnauthorizedException( 'role', 'read' );
$userService = $this->repository->getUserService();
$spiRoleAssignments = $this->userHandler->loadRoleAssignmentsByRoleId( $role->id );
$roleAssignments = array();
foreach ( $spiRoleAssignments as $spiRoleAssignment )
{
// First check if the Role is assigned to a User
// If no User is found, see if it belongs to a UserGroup
try
{
$user = $userService->loadUser( $spiRoleAssignment->contentId );
$roleAssignments[] = $this->buildDomainUserRoleAssignmentObject(
$spiRoleAssignment,
$user,
$role
);
}
catch ( APINotFoundException $e )
{
try
{
$userGroup = $userService->loadUserGroup( $spiRoleAssignment->contentId );
$roleAssignments[] = $this->buildDomainUserGroupRoleAssignmentObject(
$spiRoleAssignment,
$userGroup,
$role
);
}
catch ( APINotFoundException $e )
{
// Do nothing
}
}
}
return $roleAssignments;
}
/**
* Returns the roles assigned to the given user
*
* @param \eZ\Publish\API\Repository\Values\User\User $user
* @param boolean $inherited
*
* @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException If the current user is not allowed to read a role
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue On invalid User object
*
* @return \eZ\Publish\API\Repository\Values\User\UserRoleAssignment[]
*/
public function getRoleAssignmentsForUser( User $user, $inherited = false )
{
if ( $this->repository->hasAccess( 'role', 'read' ) !== true )
throw new UnauthorizedException( 'role', 'read' );
$roleAssignments = array();
$spiRoleAssignments = $this->userHandler->loadRoleAssignmentsByGroupId( $user->id, $inherited );
foreach ( $spiRoleAssignments as $spiRoleAssignment )
{
if ( !$inherited || $spiRoleAssignment->contentId == $user->id )
$roleAssignments[] = $this->buildDomainUserRoleAssignmentObject( $spiRoleAssignment, $user );
else
$roleAssignments[] = $this->buildDomainUserGroupRoleAssignmentObject( $spiRoleAssignment );
}
return $roleAssignments;
}
/**
* Returns the roles assigned to the given user group
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read a role
*
* @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
*
* @return \eZ\Publish\API\Repository\Values\User\UserGroupRoleAssignment[]
*/
public function getRoleAssignmentsForUserGroup( UserGroup $userGroup )
{
if ( $this->repository->hasAccess( 'role', 'read' ) !== true )
throw new UnauthorizedException( 'role', 'read' );
$roleAssignments = array();
$spiRoleAssignments = $this->userHandler->loadRoleAssignmentsByGroupId( $userGroup->id );
foreach ( $spiRoleAssignments as $spiRoleAssignment )
{
$roleAssignments[] = $this->buildDomainUserGroupRoleAssignmentObject( $spiRoleAssignment, $userGroup );
}
return $roleAssignments;
}
/**
* Instantiates a role create class
*
* @param string $name
*
* @return \eZ\Publish\API\Repository\Values\User\RoleCreateStruct
*/
public function newRoleCreateStruct( $name )
{
return new RoleCreateStruct(
array(
'identifier' => $name,
'policies' => array()
)
);
}
/**
* Instantiates a policy create class
*
* @param string $module
* @param string $function
*
* @return \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct
*/
public function newPolicyCreateStruct( $module, $function )
{
return new PolicyCreateStruct(
array(
'module' => $module,
'function' => $function,
'limitations' => array()
)
);
}
/**
* Instantiates a policy update class
*
* @return \eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct
*/
public function newPolicyUpdateStruct()
{
return new PolicyUpdateStruct(
array(
'limitations' => array()
)
);
}
/**
* Instantiates a policy update class
*
* @return \eZ\Publish\API\Repository\Values\User\RoleUpdateStruct
*/
public function newRoleUpdateStruct()
{
return new RoleUpdateStruct();
}
/**
* Maps provided SPI Role value object to API Role value object
*
* @param \eZ\Publish\SPI\Persistence\User\Role $role
*
* @return \eZ\Publish\API\Repository\Values\User\Role
*/
protected function buildDomainRoleObject( SPIRole $role )
{
$rolePolicies = array();
foreach ( $role->policies as $spiPolicy )
{
$rolePolicies[] = $this->buildDomainPolicyObject( $spiPolicy, $role );
}
return new Role(
array(
'id' => $role->id,
'identifier' => $role->identifier,
'policies' => $rolePolicies
)
);
}
/**
* Maps provided SPI Policy value object to API Policy value object
*
* @access private Only accessible for other services and the internals of the repository
* @param \eZ\Publish\SPI\Persistence\User\Policy $policy
* @param \eZ\Publish\SPI\Persistence\User\Role|null $role
*
* @return \eZ\Publish\API\Repository\Values\User\Policy
*/
public function buildDomainPolicyObject( SPIPolicy $policy, SPIRole $role = null )
{
$policyLimitations = array();
if ( $policy->module !== '*' && $policy->function !== '*' && $policy->limitations !== '*' )
{
foreach ( $policy->limitations as $identifier => $values )
{
$policyLimitations[] = $this->getLimitationType( $identifier )->buildValue( $values );
}
}
return new Policy(
array(
'id' => $policy->id,
'roleId' => $role !== null ? $role->id : $policy->roleId,
'module' => $policy->module,
'function' => $policy->function,
'limitations' => $policyLimitations
)
);
}
/**
* Builds the API UserRoleAssignment object from provided SPI RoleAssignment object
*
* @param \eZ\Publish\SPI\Persistence\User\RoleAssignment $spiRoleAssignment
* @param \eZ\Publish\API\Repository\Values\User\User $user
* @param \eZ\Publish\API\Repository\Values\User\Role $role
*
* @return \eZ\Publish\API\Repository\Values\User\UserRoleAssignment
*/
public function buildDomainUserRoleAssignmentObject( SPIRoleAssignment $spiRoleAssignment, User $user = null, APIRole $role = null )
{
$limitation = null;
if ( !empty( $spiRoleAssignment->limitationIdentifier ) )
{
$limitation = $this
->getLimitationType( $spiRoleAssignment->limitationIdentifier )
->buildValue( $spiRoleAssignment->values );
}
$user = $user ?: $this->repository->getUserService()->loadUser( $spiRoleAssignment->contentId );
$role = $role ?: $this->loadRole( $spiRoleAssignment->roleId );
return new UserRoleAssignment(
array(
'limitation' => $limitation,
'role' => $role,
'user' => $user
)
);
}
/**
* Builds the API UserGroupRoleAssignment object from provided SPI RoleAssignment object
*
* @param \eZ\Publish\SPI\Persistence\User\RoleAssignment $spiRoleAssignment
* @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
* @param \eZ\Publish\API\Repository\Values\User\Role $role
*
* @return \eZ\Publish\API\Repository\Values\User\UserGroupRoleAssignment
*/
public function buildDomainUserGroupRoleAssignmentObject( SPIRoleAssignment $spiRoleAssignment, UserGroup $userGroup = null, APIRole $role = null )
{
$limitation = null;
if ( !empty( $spiRoleAssignment->limitationIdentifier ) )
{
$limitation = $this
->getLimitationType( $spiRoleAssignment->limitationIdentifier )
->buildValue( $spiRoleAssignment->values );
}
$userGroup = $userGroup ?: $this->repository->getUserService()->loadUserGroup( $spiRoleAssignment->contentId );
$role = $role ?: $this->loadRole( $spiRoleAssignment->roleId );
return new UserGroupRoleAssignment(
array(