From 5615ef7fc9f341aecbe547fadfac5cdd8f0870b0 Mon Sep 17 00:00:00 2001 From: Bartek Date: Fri, 10 Dec 2021 09:50:44 +0100 Subject: [PATCH 1/2] IBX-1310: Created subscriber methods for assigning/unassigning user to groups (#269) --- .../API/Repository/Tests/UserServiceTest.php | 75 +++++++++++++++++++ .../EventSubscriber/UserEventSubscriber.php | 54 +++++++++++++ 2 files changed, 129 insertions(+) diff --git a/eZ/Publish/API/Repository/Tests/UserServiceTest.php b/eZ/Publish/API/Repository/Tests/UserServiceTest.php index 5c3a194b8f..3617a27fc5 100644 --- a/eZ/Publish/API/Repository/Tests/UserServiceTest.php +++ b/eZ/Publish/API/Repository/Tests/UserServiceTest.php @@ -2264,6 +2264,39 @@ public function testAssignUserToUserGroupThrowsInvalidArgumentException() /* END: Use Case */ } + /** + * @covers \eZ\Publish\API\Repository\UserService::assignUserToUserGroup + */ + public function testAssignUserToGroupWithLocationsValidation(): void + { + $repository = $this->getRepository(); + $userService = $repository->getUserService(); + $locationService = $repository->getLocationService(); + + $administratorGroupId = $this->generateId('group', 12); + + $user = $this->createUserVersion1(); + + $group = $userService->loadUserGroup($administratorGroupId); + $groupLocation = $locationService->loadLocation($group->contentInfo->mainLocationId); + + // Count number of child locations before assigning user to group + $count = $locationService->getLocationChildCount($groupLocation); + $expectedCount = $count + 1; + + $userService->assignUserToUserGroup( + $user, + $group + ); + + $this->refreshSearch($repository); + + // Count number of child locations after assigning the user to a group + $actualCount = $locationService->getLocationChildCount($groupLocation); + + self::assertEquals($expectedCount, $actualCount); + } + /** * Test for the unAssignUssrFromUserGroup() method. * @@ -2361,6 +2394,48 @@ public function testUnAssignUserFromUserGroupThrowsBadStateArgumentException() /* END: Use Case */ } + /** + * @covers \eZ\Publish\API\Repository\UserService::unAssignUserFromUserGroup + */ + public function testUnAssignUserToGroupWithLocationValidation(): void + { + $repository = $this->getRepository(); + $userService = $repository->getUserService(); + $locationService = $repository->getLocationService(); + + $editorsGroupId = $this->generateId('group', 13); + $anonymousGroupId = $this->generateId('group', 42); + + $user = $this->createUserVersion1(); + + $this->refreshSearch($repository); + + $group = $userService->loadUserGroup($editorsGroupId); + $groupLocation = $locationService->loadLocation($group->contentInfo->mainLocationId); + + // Count number of child locations before unassigning the user from a group + $count = $locationService->getLocationChildCount($groupLocation); + $expectedCount = $count - 1; + + // Assigning user to a different group to avoid removing all groups from the user + $userService->assignUserToUserGroup( + $user, + $userService->loadUserGroup($anonymousGroupId) + ); + + $userService->unAssignUserFromUserGroup( + $user, + $userService->loadUserGroup($editorsGroupId) + ); + + $this->refreshSearch($repository); + + // Count number of child locations after unassigning the user from a group + $actualCount = $locationService->getLocationChildCount($groupLocation); + + self::assertEquals($expectedCount, $actualCount); + } + /** * Test that multi-language logic for the loadUserGroup method respects prioritized language list. * diff --git a/eZ/Publish/Core/Search/Common/EventSubscriber/UserEventSubscriber.php b/eZ/Publish/Core/Search/Common/EventSubscriber/UserEventSubscriber.php index 514a16798c..d36e9642d0 100644 --- a/eZ/Publish/Core/Search/Common/EventSubscriber/UserEventSubscriber.php +++ b/eZ/Publish/Core/Search/Common/EventSubscriber/UserEventSubscriber.php @@ -6,13 +6,17 @@ */ namespace eZ\Publish\Core\Search\Common\EventSubscriber; +use eZ\Publish\API\Repository\Events\User\AssignUserToUserGroupEvent; +use eZ\Publish\API\Repository\Events\User\BeforeUnAssignUserFromUserGroupEvent; use eZ\Publish\API\Repository\Events\User\CreateUserEvent; use eZ\Publish\API\Repository\Events\User\CreateUserGroupEvent; use eZ\Publish\API\Repository\Events\User\DeleteUserEvent; use eZ\Publish\API\Repository\Events\User\DeleteUserGroupEvent; use eZ\Publish\API\Repository\Events\User\MoveUserGroupEvent; +use eZ\Publish\API\Repository\Events\User\UnAssignUserFromUserGroupEvent; use eZ\Publish\API\Repository\Events\User\UpdateUserEvent; use eZ\Publish\API\Repository\Events\User\UpdateUserGroupEvent; +use eZ\Publish\SPI\Repository\Event\AfterEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class UserEventSubscriber extends AbstractSearchEventSubscriber implements EventSubscriberInterface @@ -27,6 +31,9 @@ public static function getSubscribedEvents(): array MoveUserGroupEvent::class => 'onMoveUserGroup', UpdateUserEvent::class => 'onUpdateUser', UpdateUserGroupEvent::class => 'onUpdateUserGroup', + AssignUserToUserGroupEvent::class => 'onAssignUserToUserGroup', + UnAssignUserFromUserGroupEvent::class => 'onUnAssignUserFromUserGroup', + BeforeUnAssignUserFromUserGroupEvent::class => 'onBeforeUnAssignUserFromUserGroup', ]; } @@ -144,4 +151,51 @@ public function onUpdateUserGroup(UpdateUserGroupEvent $event) $this->searchHandler->indexLocation($location); } } + + public function onAssignUserToUserGroup(AssignUserToUserGroupEvent $event): void + { + $this->indexUserContentWithLocation($event); + } + + public function onUnAssignUserFromUserGroup(UnAssignUserFromUserGroupEvent $event): void + { + $this->indexUserContentWithLocation($event); + } + + public function onBeforeUnAssignUserFromUserGroup(BeforeUnAssignUserFromUserGroupEvent $event): void + { + $userContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo( + $event->getUser()->id + ); + + $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent( + $userContentInfo->id + ); + + foreach ($locations as $location) { + $this->searchHandler->deleteLocation($location->id, $userContentInfo->id); + } + } + + private function indexUserContentWithLocation(AfterEvent $event): void + { + $userContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo( + $event->getUser()->id + ); + + $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent( + $userContentInfo->id + ); + + $this->searchHandler->indexContent( + $this->persistenceHandler->contentHandler()->load( + $userContentInfo->id, + $userContentInfo->currentVersionNo + ) + ); + + foreach ($locations as $location) { + $this->searchHandler->indexLocation($location); + } + } } From da90d0059e69e67e3600def32a0b212caaf458dc Mon Sep 17 00:00:00 2001 From: Bartek Date: Fri, 10 Dec 2021 10:40:04 +0100 Subject: [PATCH 2/2] IBX-1310: Created slots for assigning/unassigning user to groups (#3125) --- .../API/Repository/Tests/UserServiceTest.php | 75 +++++++++++++++++++ .../Common/Slot/AssignUserToUserGroup.php | 25 +++++++ .../Common/Slot/UnAssignUserFromUserGroup.php | 26 +++++++ 3 files changed, 126 insertions(+) create mode 100644 eZ/Publish/Core/Search/Common/Slot/AssignUserToUserGroup.php create mode 100644 eZ/Publish/Core/Search/Common/Slot/UnAssignUserFromUserGroup.php diff --git a/eZ/Publish/API/Repository/Tests/UserServiceTest.php b/eZ/Publish/API/Repository/Tests/UserServiceTest.php index 20f7f4141b..0065fa41c1 100644 --- a/eZ/Publish/API/Repository/Tests/UserServiceTest.php +++ b/eZ/Publish/API/Repository/Tests/UserServiceTest.php @@ -2101,6 +2101,39 @@ public function testAssignUserToUserGroup() ); } + /** + * @covers \eZ\Publish\API\Repository\UserService::assignUserToUserGroup + */ + public function testAssignUserToGroupWithLocationsValidation(): void + { + $repository = $this->getRepository(); + $userService = $repository->getUserService(); + $locationService = $repository->getLocationService(); + + $administratorGroupId = $this->generateId('group', 12); + + $user = $this->createUserVersion1(); + + $group = $userService->loadUserGroup($administratorGroupId); + $groupLocation = $locationService->loadLocation($group->contentInfo->mainLocationId); + + // Count number of child locations before assigning user to group + $count = $locationService->getLocationChildCount($groupLocation); + $expectedCount = $count + 1; + + $userService->assignUserToUserGroup( + $user, + $group + ); + + $this->refreshSearch($repository); + + // Count number of child locations after assigning user to group + $actualCount = $locationService->getLocationChildCount($groupLocation); + + self::assertEquals($expectedCount, $actualCount); + } + /** * Test for the assignUserToUserGroup() method. * @@ -2224,6 +2257,48 @@ public function testUnAssignUserFromUserGroupThrowsBadStateArgumentException() /* END: Use Case */ } + /** + * @covers \eZ\Publish\API\Repository\UserService::unAssignUserFromUserGroup + */ + public function testUnAssignUserToGroupWithLocationValidation(): void + { + $repository = $this->getRepository(); + $userService = $repository->getUserService(); + $locationService = $repository->getLocationService(); + + $editorsGroupId = $this->generateId('group', 13); + $anonymousGroupId = $this->generateId('group', 42); + + $user = $this->createUserVersion1(); + + $this->refreshSearch($repository); + + $group = $userService->loadUserGroup($editorsGroupId); + $groupLocation = $locationService->loadLocation($group->contentInfo->mainLocationId); + + // Count number of child locations before unassigning user from group + $count = $locationService->getLocationChildCount($groupLocation); + $expectedCount = $count - 1; + + // Assigning user to different group to avoid removing all groups from user + $userService->assignUserToUserGroup( + $user, + $userService->loadUserGroup($anonymousGroupId) + ); + + $userService->unAssignUserFromUserGroup( + $user, + $userService->loadUserGroup($editorsGroupId) + ); + + $this->refreshSearch($repository); + + // Count number of child locations after unassigning user from group + $actualCount = $locationService->getLocationChildCount($groupLocation); + + self::assertEquals($expectedCount, $actualCount); + } + /** * Test that multi-language logic for the loadUserGroup method respects prioritized language list. * diff --git a/eZ/Publish/Core/Search/Common/Slot/AssignUserToUserGroup.php b/eZ/Publish/Core/Search/Common/Slot/AssignUserToUserGroup.php new file mode 100644 index 0000000000..7e573dca6f --- /dev/null +++ b/eZ/Publish/Core/Search/Common/Slot/AssignUserToUserGroup.php @@ -0,0 +1,25 @@ +persistenceHandler->contentHandler()->load($signal->userId); + $this->searchHandler->indexContent($content); + } +} diff --git a/eZ/Publish/Core/Search/Common/Slot/UnAssignUserFromUserGroup.php b/eZ/Publish/Core/Search/Common/Slot/UnAssignUserFromUserGroup.php new file mode 100644 index 0000000000..9ff159730a --- /dev/null +++ b/eZ/Publish/Core/Search/Common/Slot/UnAssignUserFromUserGroup.php @@ -0,0 +1,26 @@ +persistenceHandler->contentHandler()->load($signal->userId); + $this->searchHandler->indexContent($content); + } +}