Skip to content

Commit 4564bea

Browse files
authored
Adapt element permission service (#153)
* Adapt element permission service * fix: PHP STAN * fix: improve error handling * Apply php-cs-fixer changes * set admin boolean permissions * exclude inspection --------- Co-authored-by: lukmzig <[email protected]>
1 parent d7a41f3 commit 4564bea

15 files changed

+114
-54
lines changed

qodana.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ exclude:
4242
- name: PhpMethodNamingConventionInspection
4343
paths:
4444
- src/Migrations
45+
- name: PhpDqlBuilderUnknownModelInspection
46+
paths:
47+
- src/Repository/IndexQueueRepository.php
48+
- src/Service/SearchIndex/IndexService/ElementTypeAdapter/AssetTypeAdapter.php
49+
- src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php
4550
include:
4651
- name: PhpTaintFunctionInspection
4752
- name: PhpVulnerablePathsInspection

src/Model/Search/Asset/SearchResult/SearchResultItem/Image.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020

2121
class Image extends AssetSearchResultItem
2222
{
23-
private string $thumbnail;
23+
private ?string $thumbnail;
2424

2525
private int $width;
2626

2727
private int $height;
2828

29-
public function getThumbnail(): string
29+
public function getThumbnail(): ?string
3030
{
3131
return $this->thumbnail;
3232
}

src/Service/Permission/ElementPermissionService.php

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,34 @@
1616

1717
namespace Pimcore\Bundle\GenericDataIndexBundle\Service\Permission;
1818

19-
use Exception;
19+
use Pimcore\Bundle\GenericDataIndexBundle\Exception\AssetSearchException;
20+
use Pimcore\Bundle\GenericDataIndexBundle\Exception\DataObjectSearchException;
21+
use Pimcore\Bundle\GenericDataIndexBundle\Exception\DocumentSearchException;
2022
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset\AssetSearchServiceInterface;
2123
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\DataObject\DataObjectSearchServiceInterface;
2224
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Document\DocumentSearchServiceInterface;
25+
use Pimcore\Bundle\GenericDataIndexBundle\Traits\LoggerAwareTrait;
2326
use Pimcore\Model\Asset;
2427
use Pimcore\Model\DataObject;
2528
use Pimcore\Model\Document;
2629
use Pimcore\Model\Element\ElementInterface;
2730
use Pimcore\Model\User;
2831

29-
final readonly class ElementPermissionService implements ElementPermissionServiceInterface
32+
/**
33+
* @internal
34+
*/
35+
final class ElementPermissionService implements ElementPermissionServiceInterface
3036
{
37+
use LoggerAwareTrait;
38+
3139
public function __construct(
32-
private AssetSearchServiceInterface $assetSearchService,
33-
private DataObjectSearchServiceInterface $dataObjectSearchService,
34-
private DocumentSearchServiceInterface $documentSearchService,
35-
private PermissionServiceInterface $permissionService
40+
private readonly AssetSearchServiceInterface $assetSearchService,
41+
private readonly DataObjectSearchServiceInterface $dataObjectSearchService,
42+
private readonly DocumentSearchServiceInterface $documentSearchService,
43+
private readonly PermissionServiceInterface $permissionService
3644
) {
3745
}
3846

39-
/**
40-
* @throws Exception
41-
*/
4247
public function isAllowed(
4348
string $permission,
4449
ElementInterface $element,
@@ -52,15 +57,19 @@ public function isAllowed(
5257
};
5358
}
5459

55-
/**
56-
* @throws Exception
57-
*/
5860
private function isAssetAllowed(
5961
string $permission,
6062
Asset $asset,
6163
User $user
6264
): bool {
63-
$assetResult = $this->assetSearchService->byId($asset->getId(), $user);
65+
try {
66+
$assetResult = $this->assetSearchService->byId($asset->getId(), $user);
67+
} catch (AssetSearchException $e) {
68+
$this->logger->error('Asset search failed in the element permission check: ' . $e->getMessage());
69+
70+
return false;
71+
}
72+
6473
if (!$assetResult) {
6574
return false;
6675
}
@@ -73,15 +82,21 @@ private function isAssetAllowed(
7382
return $this->permissionService->getPermissionValue($assetPermissions, $permission);
7483
}
7584

76-
/**
77-
* @throws Exception
78-
*/
7985
private function isDataObjectAllowed(
8086
DataObject $dataObject,
8187
string $permission,
8288
User $user
8389
): bool {
84-
$dataObjectResult = $this->dataObjectSearchService->byId($dataObject->getId(), $user);
90+
try {
91+
$dataObjectResult = $this->dataObjectSearchService->byId($dataObject->getId(), $user);
92+
} catch (DataObjectSearchException $e) {
93+
$this->logger->error(
94+
'Data Object search failed in the element permission check: ' . $e->getMessage()
95+
);
96+
97+
return false;
98+
}
99+
85100
if (!$dataObjectResult) {
86101
return false;
87102
}
@@ -94,15 +109,21 @@ private function isDataObjectAllowed(
94109
return $this->permissionService->getPermissionValue($permissions, $permission);
95110
}
96111

97-
/**
98-
* @throws Exception
99-
*/
100112
private function isDocumentAllowed(
101113
Document $document,
102114
string $permission,
103115
User $user
104116
): bool {
105-
$documentResult = $this->documentSearchService->byId($document->getId(), $user);
117+
try {
118+
$documentResult = $this->documentSearchService->byId($document->getId(), $user);
119+
} catch (DocumentSearchException $e) {
120+
$this->logger->error(
121+
'Document search failed in the element permission check: ' . $e->getMessage()
122+
);
123+
124+
return false;
125+
}
126+
106127
if (!$documentResult) {
107128
return false;
108129
}

src/Service/Permission/ElementPermissionServiceInterface.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,11 @@
1616

1717
namespace Pimcore\Bundle\GenericDataIndexBundle\Service\Permission;
1818

19-
use Exception;
2019
use Pimcore\Model\Element\ElementInterface;
2120
use Pimcore\Model\User;
2221

23-
/**
24-
* @internal
25-
*/
2622
interface ElementPermissionServiceInterface
2723
{
28-
/**
29-
* @throws Exception
30-
*/
3124
public function isAllowed(
3225
string $permission,
3326
ElementInterface $element,

src/Service/Permission/PermissionService.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,10 @@ private function getAdminUserPermissions(
150150

151151
$properties = $permissions->getClassProperties();
152152
foreach ($properties as $property => $value) {
153-
$setter = 'set' . ucfirst($property);
154-
$permissions->$setter(true);
153+
if (is_bool($value)) {
154+
$setter = 'set' . ucfirst($property);
155+
$permissions->$setter(true);
156+
}
155157
}
156158

157159
return $permissions;

src/Service/Search/SearchService/Asset/AssetSearchService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __construct(
4545
}
4646

4747
/**
48-
* @throws Exception
48+
* @throws AssetSearchException
4949
*/
5050
public function search(SearchInterface $assetSearch): AssetSearchResult
5151
{
@@ -86,7 +86,7 @@ public function search(SearchInterface $assetSearch): AssetSearchResult
8686
}
8787

8888
/**
89-
* @throws Exception
89+
* @throws AssetSearchException
9090
*/
9191
public function byId(
9292
int $id,
@@ -112,7 +112,7 @@ public function byId(
112112
}
113113

114114
/**
115-
* @throws Exception
115+
* @throws AssetSearchException
116116
*/
117117
private function searchAssetById(int $id, ?User $user = null): ?AssetSearchResultItem
118118
{

src/Service/Search/SearchService/Asset/AssetSearchServiceInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

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

19-
use Exception;
19+
use Pimcore\Bundle\GenericDataIndexBundle\Exception\AssetSearchException;
2020
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\AssetSearchResult;
2121
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\AssetSearchResultItem;
2222
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Interfaces\SearchInterface;
@@ -25,12 +25,12 @@
2525
interface AssetSearchServiceInterface
2626
{
2727
/**
28-
* @throws Exception
28+
* @throws AssetSearchException
2929
*/
3030
public function search(SearchInterface $assetSearch): AssetSearchResult;
3131

3232
/**
33-
* @throws Exception
33+
* @throws AssetSearchException
3434
*/
3535
public function byId(int $id, ?User $user = null): ?AssetSearchResultItem;
3636
}

src/Service/Search/SearchService/DataObject/DataObjectSearchService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function search(DataObjectSearchInterface $dataObjectSearch): DataObjectS
8989
}
9090

9191
/**
92-
* @throws Exception
92+
* @throws DataObjectSearchException
9393
*/
9494
public function byId(
9595
int $id,
@@ -115,7 +115,7 @@ public function byId(
115115
}
116116

117117
/**
118-
* @throws Exception
118+
* @throws DataObjectSearchException
119119
*/
120120
private function searchObjectById(int $id, ?User $user = null): ?DataObjectSearchResultItem
121121
{

src/Service/Search/SearchService/DataObject/DataObjectSearchServiceInterface.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

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

19-
use Exception;
2019
use Pimcore\Bundle\GenericDataIndexBundle\Exception\DataObjectSearchException;
2120
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\DataObject\DataObjectSearchInterface;
2221
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\DataObject\SearchResult\DataObjectSearchResult;
@@ -31,7 +30,7 @@ interface DataObjectSearchServiceInterface
3130
public function search(DataObjectSearchInterface $dataObjectSearch): DataObjectSearchResult;
3231

3332
/**
34-
* @throws Exception
33+
* @throws DataObjectSearchException
3534
*/
3635
public function byId(int $id, ?User $user = null): ?DataObjectSearchResultItem;
3736
}

src/Service/Search/SearchService/Document/DocumentSearchService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function search(SearchInterface $documentSearch): DocumentSearchResult
8686
}
8787

8888
/**
89-
* @throws Exception
89+
* @throws DocumentSearchException
9090
*/
9191
public function byId(
9292
int $id,
@@ -112,7 +112,7 @@ public function byId(
112112
}
113113

114114
/**
115-
* @throws Exception
115+
* @throws DocumentSearchException
116116
*/
117117
private function searchDocumentById(int $id, ?User $user = null): ?DocumentSearchResultItem
118118
{

src/Service/Search/SearchService/Document/DocumentSearchServiceInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

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

19-
use Exception;
19+
use Pimcore\Bundle\GenericDataIndexBundle\Exception\DocumentSearchException;
2020
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Document\SearchResult\DocumentSearchResult;
2121
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Document\SearchResult\DocumentSearchResultItem;
2222
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Interfaces\SearchInterface;
@@ -25,12 +25,12 @@
2525
interface DocumentSearchServiceInterface
2626
{
2727
/**
28-
* @throws Exception
28+
* @throws DocumentSearchException
2929
*/
3030
public function search(SearchInterface $documentSearch): DocumentSearchResult;
3131

3232
/**
33-
* @throws Exception
33+
* @throws DocumentSearchException
3434
*/
3535
public function byId(int $id, ?User $user = null): ?DocumentSearchResultItem;
3636
}

src/Service/SearchIndex/IndexQueueService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function updateIndexQueue(
7272

7373
$this->pathService->rewriteChildrenIndexPaths($element);
7474
} catch (Exception $e) {
75-
$this->logger->warning(
75+
$this->logger->error(
7676
sprintf(
7777
'Update indexQueue in database-table %s failed! Error: %s',
7878
IndexQueue::TABLE,

src/Service/Serializer/AssetTypeSerializationHandler/DocumentSerializationHandler.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\FieldCategory\SystemField\Asset\DocumentSystemField;
2121
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\AssetSearchResultItem;
2222
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem;
23+
use Pimcore\Bundle\GenericDataIndexBundle\Traits\LoggerAwareTrait;
2324
use Pimcore\Model\Asset;
2425
use Pimcore\Model\Asset\Document;
2526
use Pimcore\Model\Asset\Image;
2627

2728
class DocumentSerializationHandler extends AbstractHandler
2829
{
30+
use LoggerAwareTrait;
31+
2932
/**
3033
* @throws Exception
3134
*/
@@ -49,8 +52,18 @@ public function createSearchResultModel(array $indexData): AssetSearchResultItem
4952
->setPageCount(DocumentSystemField::PAGE_COUNT->getData($indexData));
5053
}
5154

52-
private function getImageThumbnail(Document $document): string
55+
private function getImageThumbnail(Document $document): ?string
5356
{
54-
return $document->getImageThumbnail(Image\Thumbnail\Config::getPreviewConfig())->getPath();
57+
try {
58+
return $document->getImageThumbnail(Image\Thumbnail\Config::getPreviewConfig())->getPath();
59+
} catch (Exception $e) {
60+
$this->logger->error('Thumbnail generation failed for document asset: ' .
61+
$document->getId() .
62+
' error ' .
63+
$e->getMessage()
64+
);
65+
}
66+
67+
return null;
5568
}
5669
}

src/Service/Serializer/AssetTypeSerializationHandler/ImageSerializationHandler.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
1616

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

19+
use Exception;
1920
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\FieldCategory\SystemField\Asset\ImageSystemField;
2021
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\AssetSearchResultItem;
2122
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem;
23+
use Pimcore\Bundle\GenericDataIndexBundle\Traits\LoggerAwareTrait;
2224
use Pimcore\Model\Asset;
2325
use Pimcore\Model\Asset\Image;
2426

2527
class ImageSerializationHandler extends AbstractHandler
2628
{
29+
use LoggerAwareTrait;
30+
2731
public function getAdditionalSystemFields(Asset $asset): array
2832
{
2933
if(!$asset instanceof Image) {
@@ -45,8 +49,18 @@ public function createSearchResultModel(array $indexData): AssetSearchResultItem
4549
->setHeight(ImageSystemField::HEIGHT->getData($indexData));
4650
}
4751

48-
private function getThumbnail(Image $image): string
52+
private function getThumbnail(Image $image): ?string
4953
{
50-
return $image->getThumbnail(Image\Thumbnail\Config::getPreviewConfig())->getPath();
54+
try {
55+
return $image->getThumbnail(Image\Thumbnail\Config::getPreviewConfig())->getPath();
56+
} catch (Exception $e) {
57+
$this->logger->error('Thumbnail generation failed for image asset: ' .
58+
$image->getId() .
59+
' error ' .
60+
$e->getMessage()
61+
);
62+
}
63+
64+
return null;
5165
}
5266
}

0 commit comments

Comments
 (0)