-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d477d5f
commit 22ca7e9
Showing
12 changed files
with
273 additions
and
60 deletions.
There are no files selected for viewing
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
40 changes: 40 additions & 0 deletions
40
src/Model/Search/Modifier/Filter/Workspaces/ElementWorkspacesQuery.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,40 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Workspaces; | ||
|
||
use Pimcore\Bundle\GenericDataIndexBundle\Enum\Permission\PermissionTypes; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\SearchModifierInterface; | ||
use Pimcore\Model\User; | ||
|
||
final readonly class ElementWorkspacesQuery implements SearchModifierInterface | ||
{ | ||
public function __construct( | ||
private ?User $user = null, | ||
private ?string $permission = null | ||
) { | ||
} | ||
|
||
public function getUser(): ?User | ||
{ | ||
return $this->user; | ||
} | ||
|
||
public function getPermission(): ?string | ||
{ | ||
return $this->permission ?? PermissionTypes::LIST->value; | ||
} | ||
} |
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
82 changes: 82 additions & 0 deletions
82
src/SearchIndexAdapter/OpenSearch/Workspace/ElementWorkspacesQueryService.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,82 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\OpenSearch\Workspace; | ||
|
||
use Pimcore\Bundle\GenericDataIndexBundle\Enum\Permission\UserPermissionTypes; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\ElementType; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\FieldCategory\SystemField; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\OpenSearch\ConditionType; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Query\BoolQuery; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Query\TermFilter; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Query\TermsFilter; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Permission\Workspace\AssetWorkspace; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Permission\Workspace\DataObjectWorkspace; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Permission\Workspace\DocumentWorkspace; | ||
use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\Workspace\ElementWorkspacesQueryServiceInterface; | ||
use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\Workspace\QueryServiceInterface; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Service\Permission\UserPermissionServiceInterface; | ||
use Pimcore\Model\User; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final readonly class ElementWorkspacesQueryService implements ElementWorkspacesQueryServiceInterface | ||
{ | ||
private const WORKSPACE_PERMISSION_MAPPING = [ | ||
AssetWorkspace::WORKSPACE_TYPE => UserPermissionTypes::ASSETS->value, | ||
DataObjectWorkspace::WORKSPACE_TYPE => UserPermissionTypes::OBJECTS->value, | ||
DocumentWorkspace::WORKSPACE_TYPE => UserPermissionTypes::DOCUMENTS->value, | ||
]; | ||
|
||
public function __construct( | ||
private QueryServiceInterface $workspaceQueryService, | ||
private UserPermissionServiceInterface $userPermissionService | ||
) { | ||
} | ||
|
||
public function getWorkspaceQuery(?User $user, string $permission): BoolQuery | ||
{ | ||
$boolQuery = new BoolQuery(); | ||
if ($user?->isAdmin()) { | ||
return $boolQuery; | ||
} | ||
|
||
$boolQuery->addCondition( | ||
ConditionType::FILTER->value, | ||
$this->getAllowedElementTypesFilter($user)->toArrayAsSubQuery() | ||
); | ||
|
||
$workspacesQuery = new BoolQuery(); | ||
foreach (array_keys(self::WORKSPACE_PERMISSION_MAPPING) as $workspaceType) { | ||
$workspaceQuery = new BoolQuery([ | ||
ConditionType::FILTER->value => [ | ||
new TermFilter( | ||
SystemField::ELEMENT_TYPE->getPath(), ElementType::fromShortValue($workspaceType)->value | ||
), | ||
$this->workspaceQueryService->getWorkspaceQuery($workspaceType, $user, $permission) | ||
] | ||
]); | ||
|
||
$workspacesQuery->addCondition(ConditionType::SHOULD->value, $workspaceQuery->toArray(true)); | ||
} | ||
|
||
if (!$workspacesQuery->isEmpty()) { | ||
$boolQuery->addCondition(ConditionType::MUST->value, $workspacesQuery->toArray(true)); | ||
} | ||
|
||
return $boolQuery; | ||
} | ||
|
||
private function getAllowedElementTypesFilter(User $user): TermsFilter | ||
{ | ||
$allowedElementTypes = []; | ||
foreach (self::WORKSPACE_PERMISSION_MAPPING as $workspaceType => $permission) { | ||
if ($this->userPermissionService->hasPermission($user, $permission)) { | ||
$allowedElementTypes[] = ElementType::fromShortValue($workspaceType)->value; | ||
} | ||
} | ||
|
||
return new TermsFilter(SystemField::ELEMENT_TYPE->getPath(), $allowedElementTypes); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/SearchIndexAdapter/Workspace/ElementWorkspacesQueryServiceInterface.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,18 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\Workspace; | ||
|
||
use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Query\BoolQuery; | ||
use Pimcore\Model\User; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
interface ElementWorkspacesQueryServiceInterface | ||
{ | ||
/** | ||
* Returns a query which respects the workspace permissions for all element types. | ||
*/ | ||
public function getWorkspaceQuery(?User $user, string $permission): BoolQuery; | ||
} |
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
20 changes: 20 additions & 0 deletions
20
src/Service/Search/SearchService/Element/ElementSearchHelperInterface.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,20 @@ | ||
<?php | ||
|
||
namespace Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Element; | ||
|
||
|
||
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Interfaces\SearchInterface; | ||
use Pimcore\Bundle\GenericDataIndexBundle\Model\SearchIndexAdapter\SearchResult; | ||
use Pimcore\Model\User; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
interface ElementSearchHelperInterface | ||
{ | ||
public function addSearchRestrictions(SearchInterface $search): SearchInterface; | ||
|
||
public function performSearch(SearchInterface $search, string $indexName): SearchResult; | ||
|
||
public function hydrateSearchResultHits(SearchResult $searchResult, array $childrenCounts, ?User $user = null): array; | ||
} |
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
Oops, something went wrong.