Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-7809: Fixed passing locationId instead of contentId when creating UserMetadata criterion from UserGroupLimitationType level #403

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions eZ/Publish/Core/Limitation/UserGroupLimitationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

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

/**
* @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 eZ\Publish\API\Repository\Tests\Limitation\PermissionResolver\BaseLimitationIntegrationTest;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
use eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation;
use eZ\Publish\API\Repository\Values\User\Limitation\LocationLimitation;
use eZ\Publish\API\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');

$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<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]]),
],
],
];
}
}
Loading