Skip to content

Adapt element permission service #153

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

Merged
merged 6 commits into from
May 7, 2024
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
5 changes: 5 additions & 0 deletions qodana.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ exclude:
- name: PhpMethodNamingConventionInspection
paths:
- src/Migrations
- name: PhpDqlBuilderUnknownModelInspection
paths:
- src/Repository/IndexQueueRepository.php
- src/Service/SearchIndex/IndexService/ElementTypeAdapter/AssetTypeAdapter.php
- src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php
include:
- name: PhpTaintFunctionInspection
- name: PhpVulnerablePathsInspection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@

class Image extends AssetSearchResultItem
{
private string $thumbnail;
private ?string $thumbnail;

private int $width;

private int $height;

public function getThumbnail(): string
public function getThumbnail(): ?string
{
return $this->thumbnail;
}
Expand Down
63 changes: 42 additions & 21 deletions src/Service/Permission/ElementPermissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,34 @@

namespace Pimcore\Bundle\GenericDataIndexBundle\Service\Permission;

use Exception;
use Pimcore\Bundle\GenericDataIndexBundle\Exception\AssetSearchException;
use Pimcore\Bundle\GenericDataIndexBundle\Exception\DataObjectSearchException;
use Pimcore\Bundle\GenericDataIndexBundle\Exception\DocumentSearchException;
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset\AssetSearchServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\DataObject\DataObjectSearchServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Document\DocumentSearchServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Traits\LoggerAwareTrait;
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject;
use Pimcore\Model\Document;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\User;

final readonly class ElementPermissionService implements ElementPermissionServiceInterface
/**
* @internal
*/
final class ElementPermissionService implements ElementPermissionServiceInterface
{
use LoggerAwareTrait;

public function __construct(
private AssetSearchServiceInterface $assetSearchService,
private DataObjectSearchServiceInterface $dataObjectSearchService,
private DocumentSearchServiceInterface $documentSearchService,
private PermissionServiceInterface $permissionService
private readonly AssetSearchServiceInterface $assetSearchService,
private readonly DataObjectSearchServiceInterface $dataObjectSearchService,
private readonly DocumentSearchServiceInterface $documentSearchService,
private readonly PermissionServiceInterface $permissionService
) {
}

/**
* @throws Exception
*/
public function isAllowed(
string $permission,
ElementInterface $element,
Expand All @@ -52,15 +57,19 @@ public function isAllowed(
};
}

/**
* @throws Exception
*/
private function isAssetAllowed(
string $permission,
Asset $asset,
User $user
): bool {
$assetResult = $this->assetSearchService->byId($asset->getId(), $user);
try {
$assetResult = $this->assetSearchService->byId($asset->getId(), $user);
} catch (AssetSearchException $e) {
$this->logger->error('Asset search failed in the element permission check: ' . $e->getMessage());

return false;
}

if (!$assetResult) {
return false;
}
Expand All @@ -73,15 +82,21 @@ private function isAssetAllowed(
return $this->permissionService->getPermissionValue($assetPermissions, $permission);
}

/**
* @throws Exception
*/
private function isDataObjectAllowed(
DataObject $dataObject,
string $permission,
User $user
): bool {
$dataObjectResult = $this->dataObjectSearchService->byId($dataObject->getId(), $user);
try {
$dataObjectResult = $this->dataObjectSearchService->byId($dataObject->getId(), $user);
} catch (DataObjectSearchException $e) {
$this->logger->error(
'Data Object search failed in the element permission check: ' . $e->getMessage()
);

return false;
}

if (!$dataObjectResult) {
return false;
}
Expand All @@ -94,15 +109,21 @@ private function isDataObjectAllowed(
return $this->permissionService->getPermissionValue($permissions, $permission);
}

/**
* @throws Exception
*/
private function isDocumentAllowed(
Document $document,
string $permission,
User $user
): bool {
$documentResult = $this->documentSearchService->byId($document->getId(), $user);
try {
$documentResult = $this->documentSearchService->byId($document->getId(), $user);
} catch (DocumentSearchException $e) {
$this->logger->error(
'Document search failed in the element permission check: ' . $e->getMessage()
);

return false;
}

if (!$documentResult) {
return false;
}
Expand Down
7 changes: 0 additions & 7 deletions src/Service/Permission/ElementPermissionServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@

namespace Pimcore\Bundle\GenericDataIndexBundle\Service\Permission;

use Exception;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\User;

/**
* @internal
*/
interface ElementPermissionServiceInterface
{
/**
* @throws Exception
*/
public function isAllowed(
string $permission,
ElementInterface $element,
Expand Down
6 changes: 4 additions & 2 deletions src/Service/Permission/PermissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ private function getAdminUserPermissions(

$properties = $permissions->getClassProperties();
foreach ($properties as $property => $value) {
$setter = 'set' . ucfirst($property);
$permissions->$setter(true);
if (is_bool($value)) {
$setter = 'set' . ucfirst($property);
$permissions->$setter(true);
}
}

return $permissions;
Expand Down
6 changes: 3 additions & 3 deletions src/Service/Search/SearchService/Asset/AssetSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(
}

/**
* @throws Exception
* @throws AssetSearchException
*/
public function search(SearchInterface $assetSearch): AssetSearchResult
{
Expand Down Expand Up @@ -86,7 +86,7 @@ public function search(SearchInterface $assetSearch): AssetSearchResult
}

/**
* @throws Exception
* @throws AssetSearchException
*/
public function byId(
int $id,
Expand All @@ -112,7 +112,7 @@ public function byId(
}

/**
* @throws Exception
* @throws AssetSearchException
*/
private function searchAssetById(int $id, ?User $user = null): ?AssetSearchResultItem
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset;

use Exception;
use Pimcore\Bundle\GenericDataIndexBundle\Exception\AssetSearchException;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\AssetSearchResult;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\AssetSearchResultItem;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Interfaces\SearchInterface;
Expand All @@ -25,12 +25,12 @@
interface AssetSearchServiceInterface
{
/**
* @throws Exception
* @throws AssetSearchException
*/
public function search(SearchInterface $assetSearch): AssetSearchResult;

/**
* @throws Exception
* @throws AssetSearchException
*/
public function byId(int $id, ?User $user = null): ?AssetSearchResultItem;
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function search(DataObjectSearchInterface $dataObjectSearch): DataObjectS
}

/**
* @throws Exception
* @throws DataObjectSearchException
*/
public function byId(
int $id,
Expand All @@ -115,7 +115,7 @@ public function byId(
}

/**
* @throws Exception
* @throws DataObjectSearchException
*/
private function searchObjectById(int $id, ?User $user = null): ?DataObjectSearchResultItem
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

namespace Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\DataObject;

use Exception;
use Pimcore\Bundle\GenericDataIndexBundle\Exception\DataObjectSearchException;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\DataObject\DataObjectSearchInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\DataObject\SearchResult\DataObjectSearchResult;
Expand All @@ -31,7 +30,7 @@ interface DataObjectSearchServiceInterface
public function search(DataObjectSearchInterface $dataObjectSearch): DataObjectSearchResult;

/**
* @throws Exception
* @throws DataObjectSearchException
*/
public function byId(int $id, ?User $user = null): ?DataObjectSearchResultItem;
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function search(SearchInterface $documentSearch): DocumentSearchResult
}

/**
* @throws Exception
* @throws DocumentSearchException
*/
public function byId(
int $id,
Expand All @@ -112,7 +112,7 @@ public function byId(
}

/**
* @throws Exception
* @throws DocumentSearchException
*/
private function searchDocumentById(int $id, ?User $user = null): ?DocumentSearchResultItem
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Document;

use Exception;
use Pimcore\Bundle\GenericDataIndexBundle\Exception\DocumentSearchException;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Document\SearchResult\DocumentSearchResult;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Document\SearchResult\DocumentSearchResultItem;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Interfaces\SearchInterface;
Expand All @@ -25,12 +25,12 @@
interface DocumentSearchServiceInterface
{
/**
* @throws Exception
* @throws DocumentSearchException
*/
public function search(SearchInterface $documentSearch): DocumentSearchResult;

/**
* @throws Exception
* @throws DocumentSearchException
*/
public function byId(int $id, ?User $user = null): ?DocumentSearchResultItem;
}
2 changes: 1 addition & 1 deletion src/Service/SearchIndex/IndexQueueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function updateIndexQueue(

$this->pathService->rewriteChildrenIndexPaths($element);
} catch (Exception $e) {
$this->logger->warning(
$this->logger->error(
sprintf(
'Update indexQueue in database-table %s failed! Error: %s',
IndexQueue::TABLE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\FieldCategory\SystemField\Asset\DocumentSystemField;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\AssetSearchResultItem;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem;
use Pimcore\Bundle\GenericDataIndexBundle\Traits\LoggerAwareTrait;
use Pimcore\Model\Asset;
use Pimcore\Model\Asset\Document;
use Pimcore\Model\Asset\Image;

class DocumentSerializationHandler extends AbstractHandler
{
use LoggerAwareTrait;

/**
* @throws Exception
*/
Expand All @@ -49,8 +52,18 @@ public function createSearchResultModel(array $indexData): AssetSearchResultItem
->setPageCount(DocumentSystemField::PAGE_COUNT->getData($indexData));
}

private function getImageThumbnail(Document $document): string
private function getImageThumbnail(Document $document): ?string
{
return $document->getImageThumbnail(Image\Thumbnail\Config::getPreviewConfig())->getPath();
try {
return $document->getImageThumbnail(Image\Thumbnail\Config::getPreviewConfig())->getPath();
} catch (Exception $e) {
$this->logger->error('Thumbnail generation failed for document asset: ' .
$document->getId() .
' error ' .
$e->getMessage()
);
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@

namespace Pimcore\Bundle\GenericDataIndexBundle\Service\Serializer\AssetTypeSerializationHandler;

use Exception;
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\FieldCategory\SystemField\Asset\ImageSystemField;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\AssetSearchResultItem;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem;
use Pimcore\Bundle\GenericDataIndexBundle\Traits\LoggerAwareTrait;
use Pimcore\Model\Asset;
use Pimcore\Model\Asset\Image;

class ImageSerializationHandler extends AbstractHandler
{
use LoggerAwareTrait;

public function getAdditionalSystemFields(Asset $asset): array
{
if(!$asset instanceof Image) {
Expand All @@ -45,8 +49,18 @@ public function createSearchResultModel(array $indexData): AssetSearchResultItem
->setHeight(ImageSystemField::HEIGHT->getData($indexData));
}

private function getThumbnail(Image $image): string
private function getThumbnail(Image $image): ?string
{
return $image->getThumbnail(Image\Thumbnail\Config::getPreviewConfig())->getPath();
try {
return $image->getThumbnail(Image\Thumbnail\Config::getPreviewConfig())->getPath();
} catch (Exception $e) {
$this->logger->error('Thumbnail generation failed for image asset: ' .
$image->getId() .
' error ' .
$e->getMessage()
);
}

return null;
}
}
Loading
Loading