Skip to content

Commit 5615ef7

Browse files
authored
IBX-1310: Created subscriber methods for assigning/unassigning user to groups (#269)
1 parent e9ff880 commit 5615ef7

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

eZ/Publish/API/Repository/Tests/UserServiceTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,6 +2264,39 @@ public function testAssignUserToUserGroupThrowsInvalidArgumentException()
22642264
/* END: Use Case */
22652265
}
22662266

2267+
/**
2268+
* @covers \eZ\Publish\API\Repository\UserService::assignUserToUserGroup
2269+
*/
2270+
public function testAssignUserToGroupWithLocationsValidation(): void
2271+
{
2272+
$repository = $this->getRepository();
2273+
$userService = $repository->getUserService();
2274+
$locationService = $repository->getLocationService();
2275+
2276+
$administratorGroupId = $this->generateId('group', 12);
2277+
2278+
$user = $this->createUserVersion1();
2279+
2280+
$group = $userService->loadUserGroup($administratorGroupId);
2281+
$groupLocation = $locationService->loadLocation($group->contentInfo->mainLocationId);
2282+
2283+
// Count number of child locations before assigning user to group
2284+
$count = $locationService->getLocationChildCount($groupLocation);
2285+
$expectedCount = $count + 1;
2286+
2287+
$userService->assignUserToUserGroup(
2288+
$user,
2289+
$group
2290+
);
2291+
2292+
$this->refreshSearch($repository);
2293+
2294+
// Count number of child locations after assigning the user to a group
2295+
$actualCount = $locationService->getLocationChildCount($groupLocation);
2296+
2297+
self::assertEquals($expectedCount, $actualCount);
2298+
}
2299+
22672300
/**
22682301
* Test for the unAssignUssrFromUserGroup() method.
22692302
*
@@ -2361,6 +2394,48 @@ public function testUnAssignUserFromUserGroupThrowsBadStateArgumentException()
23612394
/* END: Use Case */
23622395
}
23632396

2397+
/**
2398+
* @covers \eZ\Publish\API\Repository\UserService::unAssignUserFromUserGroup
2399+
*/
2400+
public function testUnAssignUserToGroupWithLocationValidation(): void
2401+
{
2402+
$repository = $this->getRepository();
2403+
$userService = $repository->getUserService();
2404+
$locationService = $repository->getLocationService();
2405+
2406+
$editorsGroupId = $this->generateId('group', 13);
2407+
$anonymousGroupId = $this->generateId('group', 42);
2408+
2409+
$user = $this->createUserVersion1();
2410+
2411+
$this->refreshSearch($repository);
2412+
2413+
$group = $userService->loadUserGroup($editorsGroupId);
2414+
$groupLocation = $locationService->loadLocation($group->contentInfo->mainLocationId);
2415+
2416+
// Count number of child locations before unassigning the user from a group
2417+
$count = $locationService->getLocationChildCount($groupLocation);
2418+
$expectedCount = $count - 1;
2419+
2420+
// Assigning user to a different group to avoid removing all groups from the user
2421+
$userService->assignUserToUserGroup(
2422+
$user,
2423+
$userService->loadUserGroup($anonymousGroupId)
2424+
);
2425+
2426+
$userService->unAssignUserFromUserGroup(
2427+
$user,
2428+
$userService->loadUserGroup($editorsGroupId)
2429+
);
2430+
2431+
$this->refreshSearch($repository);
2432+
2433+
// Count number of child locations after unassigning the user from a group
2434+
$actualCount = $locationService->getLocationChildCount($groupLocation);
2435+
2436+
self::assertEquals($expectedCount, $actualCount);
2437+
}
2438+
23642439
/**
23652440
* Test that multi-language logic for the loadUserGroup method respects prioritized language list.
23662441
*

eZ/Publish/Core/Search/Common/EventSubscriber/UserEventSubscriber.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
*/
77
namespace eZ\Publish\Core\Search\Common\EventSubscriber;
88

9+
use eZ\Publish\API\Repository\Events\User\AssignUserToUserGroupEvent;
10+
use eZ\Publish\API\Repository\Events\User\BeforeUnAssignUserFromUserGroupEvent;
911
use eZ\Publish\API\Repository\Events\User\CreateUserEvent;
1012
use eZ\Publish\API\Repository\Events\User\CreateUserGroupEvent;
1113
use eZ\Publish\API\Repository\Events\User\DeleteUserEvent;
1214
use eZ\Publish\API\Repository\Events\User\DeleteUserGroupEvent;
1315
use eZ\Publish\API\Repository\Events\User\MoveUserGroupEvent;
16+
use eZ\Publish\API\Repository\Events\User\UnAssignUserFromUserGroupEvent;
1417
use eZ\Publish\API\Repository\Events\User\UpdateUserEvent;
1518
use eZ\Publish\API\Repository\Events\User\UpdateUserGroupEvent;
19+
use eZ\Publish\SPI\Repository\Event\AfterEvent;
1620
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1721

1822
class UserEventSubscriber extends AbstractSearchEventSubscriber implements EventSubscriberInterface
@@ -27,6 +31,9 @@ public static function getSubscribedEvents(): array
2731
MoveUserGroupEvent::class => 'onMoveUserGroup',
2832
UpdateUserEvent::class => 'onUpdateUser',
2933
UpdateUserGroupEvent::class => 'onUpdateUserGroup',
34+
AssignUserToUserGroupEvent::class => 'onAssignUserToUserGroup',
35+
UnAssignUserFromUserGroupEvent::class => 'onUnAssignUserFromUserGroup',
36+
BeforeUnAssignUserFromUserGroupEvent::class => 'onBeforeUnAssignUserFromUserGroup',
3037
];
3138
}
3239

@@ -144,4 +151,51 @@ public function onUpdateUserGroup(UpdateUserGroupEvent $event)
144151
$this->searchHandler->indexLocation($location);
145152
}
146153
}
154+
155+
public function onAssignUserToUserGroup(AssignUserToUserGroupEvent $event): void
156+
{
157+
$this->indexUserContentWithLocation($event);
158+
}
159+
160+
public function onUnAssignUserFromUserGroup(UnAssignUserFromUserGroupEvent $event): void
161+
{
162+
$this->indexUserContentWithLocation($event);
163+
}
164+
165+
public function onBeforeUnAssignUserFromUserGroup(BeforeUnAssignUserFromUserGroupEvent $event): void
166+
{
167+
$userContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo(
168+
$event->getUser()->id
169+
);
170+
171+
$locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent(
172+
$userContentInfo->id
173+
);
174+
175+
foreach ($locations as $location) {
176+
$this->searchHandler->deleteLocation($location->id, $userContentInfo->id);
177+
}
178+
}
179+
180+
private function indexUserContentWithLocation(AfterEvent $event): void
181+
{
182+
$userContentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo(
183+
$event->getUser()->id
184+
);
185+
186+
$locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent(
187+
$userContentInfo->id
188+
);
189+
190+
$this->searchHandler->indexContent(
191+
$this->persistenceHandler->contentHandler()->load(
192+
$userContentInfo->id,
193+
$userContentInfo->currentVersionNo
194+
)
195+
);
196+
197+
foreach ($locations as $location) {
198+
$this->searchHandler->indexLocation($location);
199+
}
200+
}
147201
}

0 commit comments

Comments
 (0)