From 8ef0e70c49bc2a9ceaaf55321b88b91f42fa2b14 Mon Sep 17 00:00:00 2001 From: Konrad Oboza Date: Fri, 8 Mar 2024 10:02:10 +0100 Subject: [PATCH] IBX-7809: Fixed creating `UserMetadata` criterion from `UserGroupLimitationType` For more details see https://issues.ibexa.co/browse/IBX-7809 and https://github.com/ezsystems/ezplatform-kernel/pull/403 Key changes: * Fixed passing `locationId` instead of `contentId` when creating `UserMetadata` criterion from `UserGroupLimitationType` level * [Tests] Added integration test coverage --- .../Limitation/UserGroupLimitationType.php | 14 ++- .../Limitation/UserGroupLimitationTest.php | 93 +++++++++++++++++++ 2 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 tests/integration/Core/Limitation/UserGroupLimitationTest.php diff --git a/eZ/Publish/Core/Limitation/UserGroupLimitationType.php b/eZ/Publish/Core/Limitation/UserGroupLimitationType.php index 8b7efd422c..8b2d380733 100644 --- a/eZ/Publish/Core/Limitation/UserGroupLimitationType.php +++ b/eZ/Publish/Core/Limitation/UserGroupLimitationType.php @@ -6,6 +6,7 @@ */ namespace eZ\Publish\Core\Limitation; +use eZ\Publish\API\Repository\Exceptions\NotFoundException; use eZ\Publish\API\Repository\Values\Content\Content; use eZ\Publish\API\Repository\Values\Content\ContentCreateStruct; use eZ\Publish\API\Repository\Values\Content\ContentInfo; @@ -189,10 +190,15 @@ public function getCriterion(APILimitationValue $value, APIUserReference $curren } $groupIds = []; - $currentUserLocations = $this->persistence->locationHandler()->loadLocationsByContent($currentUser->getUserId()); - if (!empty($currentUserLocations)) { - foreach ($currentUserLocations as $currentUserLocation) { - $groupIds[] = $currentUserLocation->parentId; + $locationHandler = $this->persistence->locationHandler(); + $currentUserLocations = $locationHandler->loadLocationsByContent($currentUser->getUserId()); + foreach ($currentUserLocations as $currentUserLocation) { + try { + $parentLocation = $locationHandler->load($currentUserLocation->parentId); + $groupIds[] = $parentLocation->contentId; + } catch (NotFoundException $e) { + // there is no need for any action - carrying on with checking other user locations + continue; } } diff --git a/tests/integration/Core/Limitation/UserGroupLimitationTest.php b/tests/integration/Core/Limitation/UserGroupLimitationTest.php new file mode 100644 index 0000000000..38fbaf1b74 --- /dev/null +++ b/tests/integration/Core/Limitation/UserGroupLimitationTest.php @@ -0,0 +1,93 @@ +getRepository(); + + $user = $this->createUserWithPolicies('test_user', $this->getPermissions()); + $userGroups = $repository->getUserService()->loadUserGroupsOfUser($user); + $userGroupIds = array_column($userGroups, 'id'); + + $repository->getPermissionResolver()->setCurrentUserReference($user); + + $parentFolder = $this->createFolder( + ['eng-US' => 'Parent folder'], + 2 + ); + $childFolder = $this->createFolder( + ['eng-US' => 'Child folder'], + $parentFolder->contentInfo->getMainLocationId() + ); + + $this->refreshSearch($repository); + + $query = new LocationQuery(); + $query->filter = new Criterion\LogicalAnd([ + new Criterion\ContentTypeId(self::FOLDER_CONTENT_TYPE_ID), + new Criterion\UserMetadata('group', 'in', $userGroupIds), + ]); + + $results = $repository->getSearchService()->findLocations($query)->searchHits; + $resultLocationIds = array_map(static function (SearchHit $hit): int { + /** @var \eZ\Publish\API\Repository\Values\Content\Location $location */ + $location = $hit->valueObject; + + return $location->id; + }, $results); + + self::assertContains($parentFolder->contentInfo->getMainLocationId(), $resultLocationIds); + self::assertContains($childFolder->contentInfo->getMainLocationId(), $resultLocationIds); + } + + /** + * @return array> + */ + private function getPermissions(): array + { + return [ + [ + 'module' => 'content', + 'function' => 'create', + ], + [ + 'module' => 'content', + 'function' => 'publish', + ], + [ + 'module' => 'content', + 'function' => 'read', + 'limitations' => [ + new LocationLimitation(['limitationValues' => [2]]), + ], + ], + [ + 'module' => 'content', + 'function' => 'read', + 'limitations' => [ + new ContentTypeLimitation(['limitationValues' => [self::FOLDER_CONTENT_TYPE_ID]]), + new UserGroupLimitation(['limitationValues' => [1]]), + ], + ], + ]; + } +}