vendor/ezsystems/ezplatform-kernel/eZ/Publish/Core/Event/RoleService.php line 53

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace eZ\Publish\Core\Event;
  8. use eZ\Publish\API\Repository\Events\Role\BeforeCopyRoleEvent;
  9. use eZ\Publish\API\Repository\Events\Role\CopyRoleEvent;
  10. use eZ\Publish\API\Repository\RoleService as RoleServiceInterface;
  11. use eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation;
  12. use eZ\Publish\API\Repository\Values\User\PolicyCreateStruct;
  13. use eZ\Publish\API\Repository\Values\User\PolicyDraft;
  14. use eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct;
  15. use eZ\Publish\API\Repository\Values\User\Role;
  16. use eZ\Publish\API\Repository\Values\User\RoleAssignment;
  17. use eZ\Publish\API\Repository\Values\User\RoleCopyStruct;
  18. use eZ\Publish\API\Repository\Values\User\RoleCreateStruct;
  19. use eZ\Publish\API\Repository\Values\User\RoleDraft;
  20. use eZ\Publish\API\Repository\Values\User\RoleUpdateStruct;
  21. use eZ\Publish\API\Repository\Values\User\User;
  22. use eZ\Publish\API\Repository\Values\User\UserGroup;
  23. use eZ\Publish\API\Repository\Events\Role\AddPolicyByRoleDraftEvent;
  24. use eZ\Publish\API\Repository\Events\Role\AssignRoleToUserEvent;
  25. use eZ\Publish\API\Repository\Events\Role\AssignRoleToUserGroupEvent;
  26. use eZ\Publish\API\Repository\Events\Role\BeforeAddPolicyByRoleDraftEvent;
  27. use eZ\Publish\API\Repository\Events\Role\BeforeAssignRoleToUserEvent;
  28. use eZ\Publish\API\Repository\Events\Role\BeforeAssignRoleToUserGroupEvent;
  29. use eZ\Publish\API\Repository\Events\Role\BeforeCreateRoleDraftEvent;
  30. use eZ\Publish\API\Repository\Events\Role\BeforeCreateRoleEvent;
  31. use eZ\Publish\API\Repository\Events\Role\BeforeDeleteRoleDraftEvent;
  32. use eZ\Publish\API\Repository\Events\Role\BeforeDeleteRoleEvent;
  33. use eZ\Publish\API\Repository\Events\Role\BeforePublishRoleDraftEvent;
  34. use eZ\Publish\API\Repository\Events\Role\BeforeRemovePolicyByRoleDraftEvent;
  35. use eZ\Publish\API\Repository\Events\Role\BeforeRemoveRoleAssignmentEvent;
  36. use eZ\Publish\API\Repository\Events\Role\BeforeUpdatePolicyByRoleDraftEvent;
  37. use eZ\Publish\API\Repository\Events\Role\BeforeUpdateRoleDraftEvent;
  38. use eZ\Publish\API\Repository\Events\Role\CreateRoleDraftEvent;
  39. use eZ\Publish\API\Repository\Events\Role\CreateRoleEvent;
  40. use eZ\Publish\API\Repository\Events\Role\DeleteRoleDraftEvent;
  41. use eZ\Publish\API\Repository\Events\Role\DeleteRoleEvent;
  42. use eZ\Publish\API\Repository\Events\Role\PublishRoleDraftEvent;
  43. use eZ\Publish\API\Repository\Events\Role\RemovePolicyByRoleDraftEvent;
  44. use eZ\Publish\API\Repository\Events\Role\RemoveRoleAssignmentEvent;
  45. use eZ\Publish\API\Repository\Events\Role\UpdatePolicyByRoleDraftEvent;
  46. use eZ\Publish\API\Repository\Events\Role\UpdateRoleDraftEvent;
  47. use eZ\Publish\SPI\Repository\Decorator\RoleServiceDecorator;
  48. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  49. class RoleService extends RoleServiceDecorator
  50. {
  51.     /** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
  52.     protected $eventDispatcher;
  53.     public function __construct(
  54.         RoleServiceInterface $innerService,
  55.         EventDispatcherInterface $eventDispatcher
  56.     ) {
  57.         parent::__construct($innerService);
  58.         $this->eventDispatcher $eventDispatcher;
  59.     }
  60.     public function createRole(RoleCreateStruct $roleCreateStruct): RoleDraft
  61.     {
  62.         $eventData = [$roleCreateStruct];
  63.         $beforeEvent = new BeforeCreateRoleEvent(...$eventData);
  64.         $this->eventDispatcher->dispatch($beforeEvent);
  65.         if ($beforeEvent->isPropagationStopped()) {
  66.             return $beforeEvent->getRoleDraft();
  67.         }
  68.         $roleDraft $beforeEvent->hasRoleDraft()
  69.             ? $beforeEvent->getRoleDraft()
  70.             : $this->innerService->createRole($roleCreateStruct);
  71.         $this->eventDispatcher->dispatch(
  72.             new CreateRoleEvent($roleDraft, ...$eventData)
  73.         );
  74.         return $roleDraft;
  75.     }
  76.     public function createRoleDraft(Role $role): RoleDraft
  77.     {
  78.         $eventData = [$role];
  79.         $beforeEvent = new BeforeCreateRoleDraftEvent(...$eventData);
  80.         $this->eventDispatcher->dispatch($beforeEvent);
  81.         if ($beforeEvent->isPropagationStopped()) {
  82.             return $beforeEvent->getRoleDraft();
  83.         }
  84.         $roleDraft $beforeEvent->hasRoleDraft()
  85.             ? $beforeEvent->getRoleDraft()
  86.             : $this->innerService->createRoleDraft($role);
  87.         $this->eventDispatcher->dispatch(
  88.             new CreateRoleDraftEvent($roleDraft, ...$eventData)
  89.         );
  90.         return $roleDraft;
  91.     }
  92.     public function copyRole(
  93.         Role $role,
  94.         RoleCopyStruct $roleCopyStruct
  95.     ): Role {
  96.         $eventData = [
  97.             $role,
  98.             $roleCopyStruct,
  99.         ];
  100.         $beforeEvent = new BeforeCopyRoleEvent(...$eventData);
  101.         $this->eventDispatcher->dispatch($beforeEvent);
  102.         if ($beforeEvent->isPropagationStopped()) {
  103.             return $beforeEvent->getCopiedRole();
  104.         }
  105.         $copiedRole $beforeEvent->hasCopiedRole()
  106.             ? $beforeEvent->getCopiedRole()
  107.             : $this->innerService->copyRole($role$roleCopyStruct);
  108.         $this->eventDispatcher->dispatch(
  109.             new CopyRoleEvent($copiedRole, ...$eventData)
  110.         );
  111.         return $copiedRole;
  112.     }
  113.     public function updateRoleDraft(
  114.         RoleDraft $roleDraft,
  115.         RoleUpdateStruct $roleUpdateStruct
  116.     ): RoleDraft {
  117.         $eventData = [
  118.             $roleDraft,
  119.             $roleUpdateStruct,
  120.         ];
  121.         $beforeEvent = new BeforeUpdateRoleDraftEvent(...$eventData);
  122.         $this->eventDispatcher->dispatch($beforeEvent);
  123.         if ($beforeEvent->isPropagationStopped()) {
  124.             return $beforeEvent->getUpdatedRoleDraft();
  125.         }
  126.         $updatedRoleDraft $beforeEvent->hasUpdatedRoleDraft()
  127.             ? $beforeEvent->getUpdatedRoleDraft()
  128.             : $this->innerService->updateRoleDraft($roleDraft$roleUpdateStruct);
  129.         $this->eventDispatcher->dispatch(
  130.             new UpdateRoleDraftEvent($updatedRoleDraft, ...$eventData)
  131.         );
  132.         return $updatedRoleDraft;
  133.     }
  134.     public function addPolicyByRoleDraft(
  135.         RoleDraft $roleDraft,
  136.         PolicyCreateStruct $policyCreateStruct
  137.     ): RoleDraft {
  138.         $eventData = [
  139.             $roleDraft,
  140.             $policyCreateStruct,
  141.         ];
  142.         $beforeEvent = new BeforeAddPolicyByRoleDraftEvent(...$eventData);
  143.         $this->eventDispatcher->dispatch($beforeEvent);
  144.         if ($beforeEvent->isPropagationStopped()) {
  145.             return $beforeEvent->getUpdatedRoleDraft();
  146.         }
  147.         $updatedRoleDraft $beforeEvent->hasUpdatedRoleDraft()
  148.             ? $beforeEvent->getUpdatedRoleDraft()
  149.             : $this->innerService->addPolicyByRoleDraft($roleDraft$policyCreateStruct);
  150.         $this->eventDispatcher->dispatch(
  151.             new AddPolicyByRoleDraftEvent($updatedRoleDraft, ...$eventData)
  152.         );
  153.         return $updatedRoleDraft;
  154.     }
  155.     public function removePolicyByRoleDraft(
  156.         RoleDraft $roleDraft,
  157.         PolicyDraft $policyDraft
  158.     ): RoleDraft {
  159.         $eventData = [
  160.             $roleDraft,
  161.             $policyDraft,
  162.         ];
  163.         $beforeEvent = new BeforeRemovePolicyByRoleDraftEvent(...$eventData);
  164.         $this->eventDispatcher->dispatch($beforeEvent);
  165.         if ($beforeEvent->isPropagationStopped()) {
  166.             return $beforeEvent->getUpdatedRoleDraft();
  167.         }
  168.         $updatedRoleDraft $beforeEvent->hasUpdatedRoleDraft()
  169.             ? $beforeEvent->getUpdatedRoleDraft()
  170.             : $this->innerService->removePolicyByRoleDraft($roleDraft$policyDraft);
  171.         $this->eventDispatcher->dispatch(
  172.             new RemovePolicyByRoleDraftEvent($updatedRoleDraft, ...$eventData)
  173.         );
  174.         return $updatedRoleDraft;
  175.     }
  176.     public function updatePolicyByRoleDraft(
  177.         RoleDraft $roleDraft,
  178.         PolicyDraft $policy,
  179.         PolicyUpdateStruct $policyUpdateStruct
  180.     ): PolicyDraft {
  181.         $eventData = [
  182.             $roleDraft,
  183.             $policy,
  184.             $policyUpdateStruct,
  185.         ];
  186.         $beforeEvent = new BeforeUpdatePolicyByRoleDraftEvent(...$eventData);
  187.         $this->eventDispatcher->dispatch($beforeEvent);
  188.         if ($beforeEvent->isPropagationStopped()) {
  189.             return $beforeEvent->getUpdatedPolicyDraft();
  190.         }
  191.         $updatedPolicyDraft $beforeEvent->hasUpdatedPolicyDraft()
  192.             ? $beforeEvent->getUpdatedPolicyDraft()
  193.             : $this->innerService->updatePolicyByRoleDraft($roleDraft$policy$policyUpdateStruct);
  194.         $this->eventDispatcher->dispatch(
  195.             new UpdatePolicyByRoleDraftEvent($updatedPolicyDraft, ...$eventData)
  196.         );
  197.         return $updatedPolicyDraft;
  198.     }
  199.     public function deleteRoleDraft(RoleDraft $roleDraft): void
  200.     {
  201.         $eventData = [$roleDraft];
  202.         $beforeEvent = new BeforeDeleteRoleDraftEvent(...$eventData);
  203.         $this->eventDispatcher->dispatch($beforeEvent);
  204.         if ($beforeEvent->isPropagationStopped()) {
  205.             return;
  206.         }
  207.         $this->innerService->deleteRoleDraft($roleDraft);
  208.         $this->eventDispatcher->dispatch(
  209.             new DeleteRoleDraftEvent(...$eventData)
  210.         );
  211.     }
  212.     public function publishRoleDraft(RoleDraft $roleDraft): void
  213.     {
  214.         $eventData = [$roleDraft];
  215.         $beforeEvent = new BeforePublishRoleDraftEvent(...$eventData);
  216.         $this->eventDispatcher->dispatch($beforeEvent);
  217.         if ($beforeEvent->isPropagationStopped()) {
  218.             return;
  219.         }
  220.         $this->innerService->publishRoleDraft($roleDraft);
  221.         $this->eventDispatcher->dispatch(
  222.             new PublishRoleDraftEvent(...$eventData)
  223.         );
  224.     }
  225.     public function deleteRole(Role $role): void
  226.     {
  227.         $eventData = [$role];
  228.         $beforeEvent = new BeforeDeleteRoleEvent(...$eventData);
  229.         $this->eventDispatcher->dispatch($beforeEvent);
  230.         if ($beforeEvent->isPropagationStopped()) {
  231.             return;
  232.         }
  233.         $this->innerService->deleteRole($role);
  234.         $this->eventDispatcher->dispatch(
  235.             new DeleteRoleEvent(...$eventData)
  236.         );
  237.     }
  238.     public function assignRoleToUserGroup(
  239.         Role $role,
  240.         UserGroup $userGroup,
  241.         RoleLimitation $roleLimitation null
  242.     ): void {
  243.         $eventData = [
  244.             $role,
  245.             $userGroup,
  246.             $roleLimitation,
  247.         ];
  248.         $beforeEvent = new BeforeAssignRoleToUserGroupEvent(...$eventData);
  249.         $this->eventDispatcher->dispatch($beforeEvent);
  250.         if ($beforeEvent->isPropagationStopped()) {
  251.             return;
  252.         }
  253.         $this->innerService->assignRoleToUserGroup($role$userGroup$roleLimitation);
  254.         $this->eventDispatcher->dispatch(
  255.             new AssignRoleToUserGroupEvent(...$eventData)
  256.         );
  257.     }
  258.     public function assignRoleToUser(
  259.         Role $role,
  260.         User $user,
  261.         RoleLimitation $roleLimitation null
  262.     ): void {
  263.         $eventData = [
  264.             $role,
  265.             $user,
  266.             $roleLimitation,
  267.         ];
  268.         $beforeEvent = new BeforeAssignRoleToUserEvent(...$eventData);
  269.         $this->eventDispatcher->dispatch($beforeEvent);
  270.         if ($beforeEvent->isPropagationStopped()) {
  271.             return;
  272.         }
  273.         $this->innerService->assignRoleToUser($role$user$roleLimitation);
  274.         $this->eventDispatcher->dispatch(
  275.             new AssignRoleToUserEvent(...$eventData)
  276.         );
  277.     }
  278.     public function removeRoleAssignment(RoleAssignment $roleAssignment): void
  279.     {
  280.         $eventData = [$roleAssignment];
  281.         $beforeEvent = new BeforeRemoveRoleAssignmentEvent(...$eventData);
  282.         $this->eventDispatcher->dispatch($beforeEvent);
  283.         if ($beforeEvent->isPropagationStopped()) {
  284.             return;
  285.         }
  286.         $this->innerService->removeRoleAssignment($roleAssignment);
  287.         $this->eventDispatcher->dispatch(
  288.             new RemoveRoleAssignmentEvent(...$eventData)
  289.         );
  290.     }
  291. }