Skip to content

Commit 8133257

Browse files
committed
Merge branch '1.3' of ezsystems/ezplatform-kernel into 4.5
2 parents a321523 + 8ef0e70 commit 8133257

File tree

2 files changed

+103
-4
lines changed

2 files changed

+103
-4
lines changed

src/lib/Limitation/UserGroupLimitationType.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Ibexa\Core\Limitation;
88

99
use Ibexa\Contracts\Core\Limitation\Type as SPILimitationTypeInterface;
10+
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
1011
use Ibexa\Contracts\Core\Repository\Exceptions\NotImplementedException;
1112
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
1213
use Ibexa\Contracts\Core\Repository\Values\Content\ContentCreateStruct;
@@ -190,10 +191,15 @@ public function getCriterion(APILimitationValue $value, APIUserReference $curren
190191
}
191192

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

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 Ibexa\Tests\Integration\Core\Repository\Limitation\PermissionResolver\BaseLimitationIntegrationTest;
12+
use Ibexa\Contracts\Core\Repository\Values\Content\LocationQuery;
13+
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
14+
use Ibexa\Contracts\Core\Repository\Values\Content\Search\SearchHit;
15+
use Ibexa\Contracts\Core\Repository\Values\User\Limitation\ContentTypeLimitation;
16+
use Ibexa\Contracts\Core\Repository\Values\User\Limitation\LocationLimitation;
17+
use Ibexa\Contracts\Core\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 \Ibexa\Contracts\Core\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)