-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/4.6'
- Loading branch information
Showing
16 changed files
with
856 additions
and
0 deletions.
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
src/bundle/Controller/Permission/UsersWithPermissionInfoController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?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\Bundle\AdminUi\Controller\Permission; | ||
|
||
use Ibexa\AdminUi\Permission\Mapper\UsersWithPermissionInfoMapper; | ||
use Ibexa\AdminUi\Permission\PermissionCheckContextResolverInterface; | ||
use Ibexa\Contracts\Core\Exception\InvalidArgumentException; | ||
use Ibexa\Contracts\Core\Repository\SearchService; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Query; | ||
use Ibexa\Core\QueryType\QueryType; | ||
use Ibexa\Rest\Server\Controller; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\ParameterBag; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
final class UsersWithPermissionInfoController extends Controller | ||
{ | ||
private const PARAM_LIMIT = 'limit'; | ||
private const PARAM_OFFSET = 'offset'; | ||
|
||
private QueryType $userQueryType; | ||
|
||
private PermissionCheckContextResolverInterface $permissionCheckContextResolver; | ||
|
||
private SearchService $searchService; | ||
|
||
private UsersWithPermissionInfoMapper $userWithPermissionsMapper; | ||
|
||
private int $limit; | ||
|
||
public function __construct( | ||
QueryType $userQueryType, | ||
PermissionCheckContextResolverInterface $permissionCheckContextResolver, | ||
SearchService $searchService, | ||
UsersWithPermissionInfoMapper $userWithPermissionsMapper, | ||
int $limit | ||
) { | ||
$this->userQueryType = $userQueryType; | ||
$this->permissionCheckContextResolver = $permissionCheckContextResolver; | ||
$this->searchService = $searchService; | ||
$this->userWithPermissionsMapper = $userWithPermissionsMapper; | ||
$this->limit = $limit; | ||
} | ||
|
||
/** | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException | ||
*/ | ||
public function listAction( | ||
Request $request, | ||
string $module, | ||
string $function | ||
): JsonResponse { | ||
$context = $this->permissionCheckContextResolver->resolve($module, $function, $request); | ||
$searchQuery = $this->getQuery( | ||
$request->query, | ||
$context->getCriteria() | ||
); | ||
$users = $this->searchService->findContentInfo($searchQuery, [], false); | ||
|
||
$response = $this->userWithPermissionsMapper->mapSearchResults( | ||
$users, | ||
$context, | ||
$module, | ||
$function | ||
); | ||
|
||
return new JsonResponse($response); | ||
} | ||
|
||
/** | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
*/ | ||
private function getQuery( | ||
ParameterBag $query, | ||
?Query\CriterionInterface $criteria | ||
): Query { | ||
$parameters = [ | ||
'phrase' => $query->get('phrase'), | ||
'extra_criteria' => $criteria, | ||
'limit' => $this->limit, | ||
]; | ||
|
||
if ($query->has(self::PARAM_LIMIT)) { | ||
$limit = $query->getInt(self::PARAM_LIMIT); | ||
|
||
if ($limit <= 0) { | ||
throw new InvalidArgumentException( | ||
self::PARAM_LIMIT, | ||
'Value should be greater than zero' | ||
); | ||
} | ||
|
||
$parameters[self::PARAM_LIMIT] = $limit; | ||
} | ||
|
||
if ($query->has(self::PARAM_OFFSET)) { | ||
$offset = $query->getInt(self::PARAM_OFFSET); | ||
|
||
if ($offset < 0) { | ||
throw new InvalidArgumentException( | ||
self::PARAM_OFFSET, | ||
'Value should be greater or equal zero' | ||
); | ||
} | ||
|
||
$parameters[self::PARAM_OFFSET] = $offset; | ||
} | ||
|
||
return $this->userQueryType->getQuery($parameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/contracts/Permission/PermissionCheckContextProviderInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?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\Contracts\AdminUi\Permission; | ||
|
||
use Ibexa\Contracts\AdminUi\Values\PermissionCheckContext; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
interface PermissionCheckContextProviderInterface | ||
{ | ||
public function supports(string $module, string $function): bool; | ||
|
||
public function getPermissionCheckContext(string $module, string $function, Request $request): PermissionCheckContext; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?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\Contracts\AdminUi\Values; | ||
|
||
use Ibexa\Contracts\Core\Repository\Values\Content\Query\CriterionInterface; | ||
use Ibexa\Contracts\Core\Repository\Values\ValueObject; | ||
|
||
final class PermissionCheckContext | ||
{ | ||
private ValueObject $subject; | ||
|
||
/** @var array<\Ibexa\Contracts\Core\Repository\Values\ValueObject> */ | ||
private array $targets; | ||
|
||
private ?CriterionInterface $criteria; | ||
|
||
/** | ||
* @param array<\Ibexa\Contracts\Core\Repository\Values\ValueObject> $targets | ||
*/ | ||
public function __construct( | ||
ValueObject $subject, | ||
array $targets, | ||
?CriterionInterface $criteria = null | ||
) { | ||
$this->subject = $subject; | ||
$this->targets = $targets; | ||
$this->criteria = $criteria; | ||
} | ||
|
||
public function getSubject(): ValueObject | ||
{ | ||
return $this->subject; | ||
} | ||
|
||
/** | ||
* @return array<\Ibexa\Contracts\Core\Repository\Values\ValueObject> | ||
*/ | ||
public function getTargets(): array | ||
{ | ||
return $this->targets; | ||
} | ||
|
||
public function getCriteria(): ?CriterionInterface | ||
{ | ||
return $this->criteria; | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
src/lib/Permission/ContextProvider/ContentItemContextProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?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\AdminUi\Permission\ContextProvider; | ||
|
||
use Ibexa\Contracts\AdminUi\Permission\PermissionCheckContextProviderInterface; | ||
use Ibexa\Contracts\AdminUi\Values\PermissionCheckContext; | ||
use Ibexa\Contracts\Core\Exception\InvalidArgumentException; | ||
use Ibexa\Contracts\Core\Repository\ContentService; | ||
use Ibexa\Contracts\Core\Repository\LocationService; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Query\CriterionInterface; | ||
use Symfony\Component\HttpFoundation\ParameterBag; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
final class ContentItemContextProvider implements PermissionCheckContextProviderInterface | ||
{ | ||
private const POLICY_MODULE_CONTENT = 'content'; | ||
|
||
private ContentService $contentService; | ||
|
||
private LocationService $locationService; | ||
|
||
/** @var array<string> */ | ||
private array $userContentTypeIdentifiers; | ||
|
||
/** | ||
* @param array<string> $userContentTypeIdentifiers | ||
*/ | ||
public function __construct( | ||
ContentService $contentService, | ||
LocationService $locationService, | ||
array $userContentTypeIdentifiers | ||
) { | ||
$this->contentService = $contentService; | ||
$this->locationService = $locationService; | ||
$this->userContentTypeIdentifiers = $userContentTypeIdentifiers; | ||
} | ||
|
||
public function supports(string $module, string $function): bool | ||
{ | ||
return self::POLICY_MODULE_CONTENT === $module; | ||
} | ||
|
||
/** | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidCriterionArgumentException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException | ||
*/ | ||
public function getPermissionCheckContext( | ||
string $module, | ||
string $function, | ||
Request $request | ||
): PermissionCheckContext { | ||
$query = $request->query; | ||
|
||
$contentInfo = $this->getContentInfo($query); | ||
$targets = $this->getTargets($query); | ||
$criteria = $this->createCriteria(); | ||
|
||
return new PermissionCheckContext($contentInfo, $targets, $criteria); | ||
} | ||
|
||
private function getContentInfo(ParameterBag $query): ContentInfo | ||
{ | ||
$contentId = $query->getInt('contentId'); | ||
|
||
return $this->contentService->loadContentInfo($contentId); | ||
} | ||
|
||
/** | ||
* @return array<\Ibexa\Contracts\Core\Repository\Values\ValueObject> | ||
* | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException | ||
* @throws \Ibexa\Contracts\Core\Exception\InvalidArgumentException | ||
*/ | ||
private function getTargets(ParameterBag $query): array | ||
{ | ||
if (!$query->has('locationId')) { | ||
return []; | ||
} | ||
|
||
$locationId = $query->getInt('locationId'); | ||
if ($locationId <= 0) { | ||
throw new InvalidArgumentException( | ||
'locationId', | ||
'Expected value should be greater than 0.' | ||
); | ||
} | ||
|
||
$location = $this->locationService->loadLocation($locationId); | ||
|
||
return [$location]; | ||
} | ||
|
||
/** | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidCriterionArgumentException | ||
*/ | ||
private function createCriteria(): CriterionInterface | ||
{ | ||
$criteria = [new Criterion\ContentTypeIdentifier($this->userContentTypeIdentifiers)]; | ||
|
||
return new Criterion\LogicalAnd($criteria); | ||
} | ||
} |
Oops, something went wrong.