Skip to content

Commit

Permalink
Merge branch '1.3' of ezsystems/ezplatform-kernel into 4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
konradoboza committed Mar 8, 2024
2 parents a321523 + 8ef0e70 commit 6bb2e27
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/lib/Limitation/UserGroupLimitationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,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) {

Check failure on line 199 in src/lib/Limitation/UserGroupLimitationType.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.0)

Caught class Ibexa\Core\Limitation\NotFoundException not found.

Check failure on line 199 in src/lib/Limitation/UserGroupLimitationType.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.4)

Caught class Ibexa\Core\Limitation\NotFoundException not found.

Check failure on line 199 in src/lib/Limitation/UserGroupLimitationType.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.1)

Caught class Ibexa\Core\Limitation\NotFoundException not found.
// there is no need for any action - carrying on with checking other user locations
continue;
}
}

Expand Down
93 changes: 93 additions & 0 deletions tests/integration/Core/Limitation/UserGroupLimitationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

Check warning on line 1 in tests/integration/Core/Limitation/UserGroupLimitationTest.php

View workflow job for this annotation

GitHub Actions / Run code style check (8.0)

Found violation(s) of type: ordered_imports

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Integration\Core\Limitation;

use Ibexa\Tests\Integration\Core\Repository\Limitation\PermissionResolver\BaseLimitationIntegrationTest;
use Ibexa\Contracts\Core\Repository\Values\Content\LocationQuery;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Search\SearchHit;
use Ibexa\Contracts\Core\Repository\Values\User\Limitation\ContentTypeLimitation;
use Ibexa\Contracts\Core\Repository\Values\User\Limitation\LocationLimitation;
use Ibexa\Contracts\Core\Repository\Values\User\Limitation\UserGroupLimitation;

final class UserGroupLimitationTest extends BaseLimitationIntegrationTest
{
private const FOLDER_CONTENT_TYPE_ID = 1;

public function testHasUserWithUserGroupLimitationAccessToCreatedLocations(): void
{
$repository = $this->getRepository();

$user = $this->createUserWithPolicies('test_user', $this->getPermissions());
$userGroups = $repository->getUserService()->loadUserGroupsOfUser($user);
$userGroupIds = array_column($userGroups, 'id');

Check failure on line 29 in tests/integration/Core/Limitation/UserGroupLimitationTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.0)

Parameter #1 $array of function array_column expects array, iterable<Ibexa\Contracts\Core\Repository\Values\User\UserGroup> given.

Check failure on line 29 in tests/integration/Core/Limitation/UserGroupLimitationTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.4)

Parameter #1 $array of function array_column expects array, iterable<Ibexa\Contracts\Core\Repository\Values\User\UserGroup> given.

Check failure on line 29 in tests/integration/Core/Limitation/UserGroupLimitationTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.1)

Parameter #1 $array of function array_column expects array, iterable<Ibexa\Contracts\Core\Repository\Values\User\UserGroup> given.

$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 \Ibexa\Contracts\Core\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<array<string, mixed>>
*/
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]]),
],
],
];
}
}

0 comments on commit 6bb2e27

Please sign in to comment.