vendor/ezsystems/ezplatform-kernel/eZ/Publish/Core/Event/UserService.php line 44

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\User\BeforeUpdateUserPasswordEvent;
  9. use eZ\Publish\API\Repository\Events\User\UpdateUserPasswordEvent;
  10. use eZ\Publish\API\Repository\UserService as UserServiceInterface;
  11. use eZ\Publish\API\Repository\Values\User\User;
  12. use eZ\Publish\API\Repository\Values\User\UserCreateStruct;
  13. use eZ\Publish\API\Repository\Values\User\UserGroup;
  14. use eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct;
  15. use eZ\Publish\API\Repository\Values\User\UserGroupUpdateStruct;
  16. use eZ\Publish\API\Repository\Values\User\UserTokenUpdateStruct;
  17. use eZ\Publish\API\Repository\Values\User\UserUpdateStruct;
  18. use eZ\Publish\API\Repository\Events\User\AssignUserToUserGroupEvent;
  19. use eZ\Publish\API\Repository\Events\User\BeforeAssignUserToUserGroupEvent;
  20. use eZ\Publish\API\Repository\Events\User\BeforeCreateUserEvent;
  21. use eZ\Publish\API\Repository\Events\User\BeforeCreateUserGroupEvent;
  22. use eZ\Publish\API\Repository\Events\User\BeforeDeleteUserEvent;
  23. use eZ\Publish\API\Repository\Events\User\BeforeDeleteUserGroupEvent;
  24. use eZ\Publish\API\Repository\Events\User\BeforeMoveUserGroupEvent;
  25. use eZ\Publish\API\Repository\Events\User\BeforeUnAssignUserFromUserGroupEvent;
  26. use eZ\Publish\API\Repository\Events\User\BeforeUpdateUserEvent;
  27. use eZ\Publish\API\Repository\Events\User\BeforeUpdateUserGroupEvent;
  28. use eZ\Publish\API\Repository\Events\User\BeforeUpdateUserTokenEvent;
  29. use eZ\Publish\API\Repository\Events\User\CreateUserEvent;
  30. use eZ\Publish\API\Repository\Events\User\CreateUserGroupEvent;
  31. use eZ\Publish\API\Repository\Events\User\DeleteUserEvent;
  32. use eZ\Publish\API\Repository\Events\User\DeleteUserGroupEvent;
  33. use eZ\Publish\API\Repository\Events\User\MoveUserGroupEvent;
  34. use eZ\Publish\API\Repository\Events\User\UnAssignUserFromUserGroupEvent;
  35. use eZ\Publish\API\Repository\Events\User\UpdateUserEvent;
  36. use eZ\Publish\API\Repository\Events\User\UpdateUserGroupEvent;
  37. use eZ\Publish\API\Repository\Events\User\UpdateUserTokenEvent;
  38. use eZ\Publish\SPI\Repository\Decorator\UserServiceDecorator;
  39. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  40. class UserService extends UserServiceDecorator
  41. {
  42.     /** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
  43.     protected $eventDispatcher;
  44.     public function __construct(
  45.         UserServiceInterface $innerService,
  46.         EventDispatcherInterface $eventDispatcher
  47.     ) {
  48.         parent::__construct($innerService);
  49.         $this->eventDispatcher $eventDispatcher;
  50.     }
  51.     public function createUserGroup(
  52.         UserGroupCreateStruct $userGroupCreateStruct,
  53.         UserGroup $parentGroup
  54.     ): UserGroup {
  55.         $eventData = [
  56.             $userGroupCreateStruct,
  57.             $parentGroup,
  58.         ];
  59.         $beforeEvent = new BeforeCreateUserGroupEvent(...$eventData);
  60.         $this->eventDispatcher->dispatch($beforeEvent);
  61.         if ($beforeEvent->isPropagationStopped()) {
  62.             return $beforeEvent->getUserGroup();
  63.         }
  64.         $userGroup $beforeEvent->hasUserGroup()
  65.             ? $beforeEvent->getUserGroup()
  66.             : $this->innerService->createUserGroup($userGroupCreateStruct$parentGroup);
  67.         $this->eventDispatcher->dispatch(
  68.             new CreateUserGroupEvent($userGroup, ...$eventData)
  69.         );
  70.         return $userGroup;
  71.     }
  72.     public function deleteUserGroup(UserGroup $userGroup): iterable
  73.     {
  74.         $eventData = [$userGroup];
  75.         $beforeEvent = new BeforeDeleteUserGroupEvent(...$eventData);
  76.         $this->eventDispatcher->dispatch($beforeEvent);
  77.         if ($beforeEvent->isPropagationStopped()) {
  78.             return $beforeEvent->getLocations();
  79.         }
  80.         $locations $beforeEvent->hasLocations()
  81.             ? $beforeEvent->getLocations()
  82.             : $this->innerService->deleteUserGroup($userGroup);
  83.         $this->eventDispatcher->dispatch(
  84.             new DeleteUserGroupEvent($locations, ...$eventData)
  85.         );
  86.         return $locations;
  87.     }
  88.     public function moveUserGroup(
  89.         UserGroup $userGroup,
  90.         UserGroup $newParent
  91.     ): void {
  92.         $eventData = [
  93.             $userGroup,
  94.             $newParent,
  95.         ];
  96.         $beforeEvent = new BeforeMoveUserGroupEvent(...$eventData);
  97.         $this->eventDispatcher->dispatch($beforeEvent);
  98.         if ($beforeEvent->isPropagationStopped()) {
  99.             return;
  100.         }
  101.         $this->innerService->moveUserGroup($userGroup$newParent);
  102.         $this->eventDispatcher->dispatch(
  103.             new MoveUserGroupEvent(...$eventData)
  104.         );
  105.     }
  106.     public function updateUserGroup(
  107.         UserGroup $userGroup,
  108.         UserGroupUpdateStruct $userGroupUpdateStruct
  109.     ): UserGroup {
  110.         $eventData = [
  111.             $userGroup,
  112.             $userGroupUpdateStruct,
  113.         ];
  114.         $beforeEvent = new BeforeUpdateUserGroupEvent(...$eventData);
  115.         $this->eventDispatcher->dispatch($beforeEvent);
  116.         if ($beforeEvent->isPropagationStopped()) {
  117.             return $beforeEvent->getUpdatedUserGroup();
  118.         }
  119.         $updatedUserGroup $beforeEvent->hasUpdatedUserGroup()
  120.             ? $beforeEvent->getUpdatedUserGroup()
  121.             : $this->innerService->updateUserGroup($userGroup$userGroupUpdateStruct);
  122.         $this->eventDispatcher->dispatch(
  123.             new UpdateUserGroupEvent($updatedUserGroup, ...$eventData)
  124.         );
  125.         return $updatedUserGroup;
  126.     }
  127.     public function createUser(
  128.         UserCreateStruct $userCreateStruct,
  129.         array $parentGroups
  130.     ): User {
  131.         $eventData = [
  132.             $userCreateStruct,
  133.             $parentGroups,
  134.         ];
  135.         $beforeEvent = new BeforeCreateUserEvent(...$eventData);
  136.         $this->eventDispatcher->dispatch($beforeEvent);
  137.         if ($beforeEvent->isPropagationStopped()) {
  138.             return $beforeEvent->getUser();
  139.         }
  140.         $user $beforeEvent->hasUser()
  141.             ? $beforeEvent->getUser()
  142.             : $this->innerService->createUser($userCreateStruct$parentGroups);
  143.         $this->eventDispatcher->dispatch(
  144.             new CreateUserEvent($user, ...$eventData)
  145.         );
  146.         return $user;
  147.     }
  148.     public function deleteUser(User $user): iterable
  149.     {
  150.         $eventData = [$user];
  151.         $beforeEvent = new BeforeDeleteUserEvent(...$eventData);
  152.         $this->eventDispatcher->dispatch($beforeEvent);
  153.         if ($beforeEvent->isPropagationStopped()) {
  154.             return $beforeEvent->getLocations();
  155.         }
  156.         $locations $beforeEvent->hasLocations()
  157.             ? $beforeEvent->getLocations()
  158.             : $this->innerService->deleteUser($user);
  159.         $this->eventDispatcher->dispatch(
  160.             new DeleteUserEvent($locations, ...$eventData)
  161.         );
  162.         return $locations;
  163.     }
  164.     public function updateUser(
  165.         User $user,
  166.         UserUpdateStruct $userUpdateStruct
  167.     ): User {
  168.         $eventData = [
  169.             $user,
  170.             $userUpdateStruct,
  171.         ];
  172.         $beforeEvent = new BeforeUpdateUserEvent(...$eventData);
  173.         $this->eventDispatcher->dispatch($beforeEvent);
  174.         if ($beforeEvent->isPropagationStopped()) {
  175.             return $beforeEvent->getUpdatedUser();
  176.         }
  177.         $updatedUser $beforeEvent->hasUpdatedUser()
  178.             ? $beforeEvent->getUpdatedUser()
  179.             : $this->innerService->updateUser($user$userUpdateStruct);
  180.         $this->eventDispatcher->dispatch(
  181.             new UpdateUserEvent($updatedUser, ...$eventData)
  182.         );
  183.         return $updatedUser;
  184.     }
  185.     public function updateUserPassword(
  186.         User $user,
  187.         string $newPassword
  188.     ): User {
  189.         $eventData = [
  190.             $user,
  191.             new UserUpdateStruct([
  192.                 'password' => $newPassword,
  193.             ]),
  194.         ];
  195.         /**
  196.          * @deprecated since eZ Platform by Ibexa v3.1. listening on BeforeUpdateUserEvent when updating password has been deprecated. Use BeforeUpdateUserPasswordEvent instead.
  197.          */
  198.         $beforeEvent = new BeforeUpdateUserEvent(...$eventData);
  199.         $this->eventDispatcher->dispatch($beforeEvent);
  200.         if ($beforeEvent->isPropagationStopped()) {
  201.             return $beforeEvent->getUpdatedUser();
  202.         }
  203.         $beforePasswordEvent = new BeforeUpdateUserPasswordEvent($user$newPassword);
  204.         $this->eventDispatcher->dispatch($beforePasswordEvent);
  205.         if ($beforePasswordEvent->isPropagationStopped()) {
  206.             return $beforePasswordEvent->getUpdatedUser();
  207.         }
  208.         if ($beforeEvent->hasUpdatedUser()) {
  209.             $updatedUser $beforeEvent->getUpdatedUser();
  210.         } elseif ($beforePasswordEvent->hasUpdatedUser()) {
  211.             $updatedUser $beforePasswordEvent->getUpdatedUser();
  212.         } else {
  213.             $updatedUser $this->innerService->updateUserPassword($user$newPassword);
  214.         }
  215.         /**
  216.          * @deprecated since eZ Platform by Ibexa v3.1. Listening on UpdateUserEvent when updating password has been deprecated. Use UpdateUserPasswordEvent instead.
  217.          */
  218.         $afterEvent = new UpdateUserEvent($updatedUser, ...$eventData);
  219.         $this->eventDispatcher->dispatch(
  220.             $afterEvent
  221.         );
  222.         $afterPasswordEvent = new UpdateUserPasswordEvent($updatedUser$user$newPassword);
  223.         $this->eventDispatcher->dispatch(
  224.             $afterPasswordEvent
  225.         );
  226.         return $updatedUser;
  227.     }
  228.     public function updateUserToken(
  229.         User $user,
  230.         UserTokenUpdateStruct $userTokenUpdateStruct
  231.     ): User {
  232.         $eventData = [
  233.             $user,
  234.             $userTokenUpdateStruct,
  235.         ];
  236.         $beforeEvent = new BeforeUpdateUserTokenEvent(...$eventData);
  237.         $this->eventDispatcher->dispatch($beforeEvent);
  238.         if ($beforeEvent->isPropagationStopped()) {
  239.             return $beforeEvent->getUpdatedUser();
  240.         }
  241.         $updatedUser $beforeEvent->hasUpdatedUser()
  242.             ? $beforeEvent->getUpdatedUser()
  243.             : $this->innerService->updateUserToken($user$userTokenUpdateStruct);
  244.         $this->eventDispatcher->dispatch(
  245.             new UpdateUserTokenEvent($updatedUser, ...$eventData)
  246.         );
  247.         return $updatedUser;
  248.     }
  249.     public function assignUserToUserGroup(
  250.         User $user,
  251.         UserGroup $userGroup
  252.     ): void {
  253.         $eventData = [
  254.             $user,
  255.             $userGroup,
  256.         ];
  257.         $beforeEvent = new BeforeAssignUserToUserGroupEvent(...$eventData);
  258.         $this->eventDispatcher->dispatch($beforeEvent);
  259.         if ($beforeEvent->isPropagationStopped()) {
  260.             return;
  261.         }
  262.         $this->innerService->assignUserToUserGroup($user$userGroup);
  263.         $this->eventDispatcher->dispatch(
  264.             new AssignUserToUserGroupEvent(...$eventData)
  265.         );
  266.     }
  267.     public function unAssignUserFromUserGroup(
  268.         User $user,
  269.         UserGroup $userGroup
  270.     ): void {
  271.         $eventData = [
  272.             $user,
  273.             $userGroup,
  274.         ];
  275.         $beforeEvent = new BeforeUnAssignUserFromUserGroupEvent(...$eventData);
  276.         $this->eventDispatcher->dispatch($beforeEvent);
  277.         if ($beforeEvent->isPropagationStopped()) {
  278.             return;
  279.         }
  280.         $this->innerService->unAssignUserFromUserGroup($user$userGroup);
  281.         $this->eventDispatcher->dispatch(
  282.             new UnAssignUserFromUserGroupEvent(...$eventData)
  283.         );
  284.     }
  285. }