Skip to content

Commit 8ef0e70

Browse files
authored
IBX-7809: Fixed creating UserMetadata criterion from UserGroupLimitationType
For more details see https://issues.ibexa.co/browse/IBX-7809 and #403 Key changes: * Fixed passing `locationId` instead of `contentId` when creating `UserMetadata` criterion from `UserGroupLimitationType` level * [Tests] Added integration test coverage
1 parent aa31bb2 commit 8ef0e70

2 files changed

Lines changed: 103 additions & 4 deletions

File tree

eZ/Publish/Core/Limitation/UserGroupLimitationType.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
namespace eZ\Publish\Core\Limitation;
88

9+
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
910
use eZ\Publish\API\Repository\Values\Content\Content;
1011
use eZ\Publish\API\Repository\Values\Content\ContentCreateStruct;
1112
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
@@ -189,10 +190,15 @@ public function getCriterion(APILimitationValue $value, APIUserReference $curren
189190
}
190191

191192
$groupIds = [];
192-
$currentUserLocations = $this->persistence->locationHandler()->loadLocationsByContent($currentUser->getUserId());
193-
if (!empty($currentUserLocations)) {
194-
foreach ($currentUserLocations as $currentUserLocation) {
195-
$groupIds[] = $currentUserLocation->parentId;
193+
$locationHandler = $this->persistence->locationHandler();
194+
$currentUserLocations = $locationHandler->loadLocationsByContent($currentUser->getUserId());
195+
foreach ($currentUserLocations as $currentUserLocation) {
196+
try {
197+
$parentLocation = $locationHandler->load($currentUserLocation->parentId);
198+
$groupIds[] = $parentLocation->contentId;
199+
} catch (NotFoundException $e) {
200+
// there is no need for any action - carrying on with checking other user locations
201+
continue;
196202
}
197203
}
198204

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Tests\Integration\Core\Limitation;
10+
11+
use eZ\Publish\API\Repository\Tests\Limitation\PermissionResolver\BaseLimitationIntegrationTest;
12+
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
13+
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
14+
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
15+
use eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation;
16+
use eZ\Publish\API\Repository\Values\User\Limitation\LocationLimitation;
17+
use eZ\Publish\API\Repository\Values\User\Limitation\UserGroupLimitation;
18+
19+
final class UserGroupLimitationTest extends BaseLimitationIntegrationTest
20+
{
21+
private const FOLDER_CONTENT_TYPE_ID = 1;
22+
23+
public function testHasUserWithUserGroupLimitationAccessToCreatedLocations(): void
24+
{
25+
$repository = $this->getRepository();
26+
27+
$user = $this->createUserWithPolicies('test_user', $this->getPermissions());
28+
$userGroups = $repository->getUserService()->loadUserGroupsOfUser($user);
29+
$userGroupIds = array_column($userGroups, 'id');
30+
31+
$repository->getPermissionResolver()->setCurrentUserReference($user);
32+
33+
$parentFolder = $this->createFolder(
34+
['eng-US' => 'Parent folder'],
35+
2
36+
);
37+
$childFolder = $this->createFolder(
38+
['eng-US' => 'Child folder'],
39+
$parentFolder->contentInfo->getMainLocationId()
40+
);
41+
42+
$this->refreshSearch($repository);
43+
44+
$query = new LocationQuery();
45+
$query->filter = new Criterion\LogicalAnd([
46+
new Criterion\ContentTypeId(self::FOLDER_CONTENT_TYPE_ID),
47+
new Criterion\UserMetadata('group', 'in', $userGroupIds),
48+
]);
49+
50+
$results = $repository->getSearchService()->findLocations($query)->searchHits;
51+
$resultLocationIds = array_map(static function (SearchHit $hit): int {
52+
/** @var \eZ\Publish\API\Repository\Values\Content\Location $location */
53+
$location = $hit->valueObject;
54+
55+
return $location->id;
56+
}, $results);
57+
58+
self::assertContains($parentFolder->contentInfo->getMainLocationId(), $resultLocationIds);
59+
self::assertContains($childFolder->contentInfo->getMainLocationId(), $resultLocationIds);
60+
}
61+
62+
/**
63+
* @return array<array<string, mixed>>
64+
*/
65+
private function getPermissions(): array
66+
{
67+
return [
68+
[
69+
'module' => 'content',
70+
'function' => 'create',
71+
],
72+
[
73+
'module' => 'content',
74+
'function' => 'publish',
75+
],
76+
[
77+
'module' => 'content',
78+
'function' => 'read',
79+
'limitations' => [
80+
new LocationLimitation(['limitationValues' => [2]]),
81+
],
82+
],
83+
[
84+
'module' => 'content',
85+
'function' => 'read',
86+
'limitations' => [
87+
new ContentTypeLimitation(['limitationValues' => [self::FOLDER_CONTENT_TYPE_ID]]),
88+
new UserGroupLimitation(['limitationValues' => [1]]),
89+
],
90+
],
91+
];
92+
}
93+
}

0 commit comments

Comments
 (0)