Skip to content

Commit 422e13e

Browse files
authored
[Improvement]Handle special language workspace permissions (#280)
* add logic to consider and return special language permissions * Apply php-cs-fixer changes * fix: unit tests * fix: unit tests --------- Co-authored-by: lukmzig <[email protected]>
1 parent a7cc518 commit 422e13e

File tree

9 files changed

+105
-34
lines changed

9 files changed

+105
-34
lines changed

doc/01_Installation/02_Upgrade.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Following steps are necessary during updating to newer versions.
1717
- Removed deprecated class `Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\OpenSearch\DataObject\FieldDefinitionAdapter\AbstractAdapter` please use `Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\DefaultSearch\DataObject\FieldDefinitionAdapter\AbstractAdapter` instead
1818
- Added default prefix `data-object_` prefix to all data object class definition index names. This change is necessary to avoid conflicts with other index names.
1919
- Add element type to the `getIds` method of `Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Element\SearchResult\ElementSearchResult`
20+
- Added `getSpecialPermissions` method to `Pimcore\Bundle\GenericDataIndexBundle\Service\Permission\ElementPermissionServiceInterface` to get special permissions workspace language permissions for elements
21+
- Removed layout permission from `Pimcore\Bundle\GenericDataIndexBundle\Permission\DataObjectPermissions` as they are not index relevant
2022

2123
#### Interface changes
2224
- Added `PermissionTypes $permissionType` parameter with default type `PermissionTypes::LIST` to

src/Permission/DataObjectPermissions.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ final class DataObjectPermissions extends BasePermissions
2929

3030
private ?string $localizedView = null;
3131

32-
private ?string $layouts = null;
33-
3432
public function isSave(): bool
3533
{
3634
return $this->save;
@@ -71,16 +69,6 @@ public function setLocalizedView(?string $localizedView): void
7169
$this->localizedView = $localizedView;
7270
}
7371

74-
public function isLayouts(): ?string
75-
{
76-
return $this->layouts;
77-
}
78-
79-
public function setLayouts(?string $layout): void
80-
{
81-
$this->layouts = $layout;
82-
}
83-
8472
public function getClassProperties(array $properties = []): array
8573
{
8674
return parent::getClassProperties(get_object_vars($this));

src/Permission/Workspace/AbstractWorkspace.php

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

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

19-
use Pimcore\Bundle\GenericDataIndexBundle\Permission\BasePermissions;
19+
use Pimcore\Bundle\GenericDataIndexBundle\Permission\AssetPermissions;
20+
use Pimcore\Bundle\GenericDataIndexBundle\Permission\DataObjectPermissions;
21+
use Pimcore\Bundle\GenericDataIndexBundle\Permission\DocumentPermissions;
2022
use Pimcore\Model\User\Workspace;
2123

2224
/**
@@ -38,14 +40,14 @@ public function getPath(): string
3840
return $this->path;
3941
}
4042

41-
public function getPermissions(): BasePermissions
43+
public function getPermissions(): AssetPermissions|DataObjectPermissions|DocumentPermissions
4244
{
4345
return $this->permissions;
4446
}
4547

4648
protected function setWorkspacePermissions(
4749
Workspace\Asset|Workspace\DataObject|Workspace\Document $userPermissions,
48-
BasePermissions $workspacePermissions
50+
AssetPermissions|DataObjectPermissions|DocumentPermissions $workspacePermissions
4951
): void {
5052
$properties = $workspacePermissions->getClassProperties();
5153
foreach ($properties as $property => $value) {

src/Permission/Workspace/WorkspaceInterface.php

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

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

19-
use Pimcore\Bundle\GenericDataIndexBundle\Permission\BasePermissions;
19+
use Pimcore\Bundle\GenericDataIndexBundle\Permission\AssetPermissions;
20+
use Pimcore\Bundle\GenericDataIndexBundle\Permission\DataObjectPermissions;
21+
use Pimcore\Bundle\GenericDataIndexBundle\Permission\DocumentPermissions;
2022

2123
/**
2224
* @internal
@@ -25,5 +27,5 @@ interface WorkspaceInterface
2527
{
2628
public function getPath(): string;
2729

28-
public function getPermissions(): BasePermissions;
30+
public function getPermissions(): AssetPermissions|DataObjectPermissions|DocumentPermissions;
2931
}

src/Service/Permission/ElementPermissionService.php

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

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

19+
use Pimcore\Bundle\GenericDataIndexBundle\Permission\DataObjectPermissions;
1920
use Pimcore\Bundle\GenericDataIndexBundle\Service\Transformer\SearchResultItem\AssetToSearchResultItemTransformerInterface;
2021
use Pimcore\Bundle\GenericDataIndexBundle\Service\Transformer\SearchResultItem\DataObjectToSearchResultItemTransformerInterface;
2122
use Pimcore\Bundle\GenericDataIndexBundle\Service\Transformer\SearchResultItem\DocumentToSearchResultItemTransformerInterface;
@@ -41,16 +42,31 @@ public function __construct(
4142
public function isAllowed(
4243
string $permission,
4344
ElementInterface $element,
44-
User $user
45+
User $user,
46+
?string $specialPermission = null
4547
): bool {
4648
return match (true) {
4749
$element instanceof Asset => $this->isAssetAllowed($permission, $element, $user),
48-
$element instanceof DataObject => $this->isDataObjectAllowed($element, $permission, $user),
50+
$element instanceof DataObject => $this->isDataObjectAllowed(
51+
$element,
52+
$permission,
53+
$user,
54+
$specialPermission
55+
),
4956
$element instanceof Document => $this->isDocumentAllowed($element, $permission, $user),
5057
default => false,
5158
};
5259
}
5360

61+
public function getSpecialPermissions(DataObject $dataObject, User $user, string $permission): array
62+
{
63+
64+
return $this->permissionService->getSpecialPermissionValues(
65+
$this->getPermissionsFromDataObject($dataObject, $user),
66+
$permission
67+
);
68+
}
69+
5470
private function isAssetAllowed(
5571
string $permission,
5672
Asset $asset,
@@ -69,16 +85,15 @@ private function isAssetAllowed(
6985
private function isDataObjectAllowed(
7086
DataObject $dataObject,
7187
string $permission,
72-
User $user
88+
User $user,
89+
?string $specialPermission = null
7390
): bool {
74-
$dataObjectSearchResultItem = $this->dataObjectTransformer->transform($dataObject, $user);
7591

76-
$permissions = $this->permissionService->getDataObjectPermissions(
77-
$dataObjectSearchResultItem,
78-
$user
92+
return $this->permissionService->getPermissionValue(
93+
$this->getPermissionsFromDataObject($dataObject, $user),
94+
$permission,
95+
$specialPermission
7996
);
80-
81-
return $this->permissionService->getPermissionValue($permissions, $permission);
8297
}
8398

8499
private function isDocumentAllowed(
@@ -95,4 +110,14 @@ private function isDocumentAllowed(
95110

96111
return $this->permissionService->getPermissionValue($permissions, $permission);
97112
}
113+
114+
private function getPermissionsFromDataObject(DataObject $dataObject, User $user): DataObjectPermissions
115+
{
116+
$dataObjectSearchResultItem = $this->dataObjectTransformer->transform($dataObject, $user);
117+
118+
return $this->permissionService->getDataObjectPermissions(
119+
$dataObjectSearchResultItem,
120+
$user
121+
);
122+
}
98123
}

src/Service/Permission/ElementPermissionServiceInterface.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
use Pimcore\Model\DataObject;
1920
use Pimcore\Model\Element\ElementInterface;
2021
use Pimcore\Model\User;
2122

@@ -24,6 +25,9 @@ interface ElementPermissionServiceInterface
2425
public function isAllowed(
2526
string $permission,
2627
ElementInterface $element,
27-
User $user
28+
User $user,
29+
?string $specialPermission = null
2830
): bool;
31+
32+
public function getSpecialPermissions(DataObject $dataObject, User $user, string $permission): array;
2933
}

src/Service/Permission/PermissionService.php

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Pimcore\Bundle\GenericDataIndexBundle\Permission\Workspace\DocumentWorkspace;
2929
use Pimcore\Bundle\GenericDataIndexBundle\Permission\Workspace\WorkspaceInterface;
3030
use Pimcore\Bundle\GenericDataIndexBundle\Service\EventServiceInterface;
31+
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\LanguageServiceInterface;
3132
use Pimcore\Bundle\GenericDataIndexBundle\Service\Workspace\WorkspaceServiceInterface;
3233
use Pimcore\Model\User;
3334

@@ -36,8 +37,14 @@
3637
*/
3738
final readonly class PermissionService implements PermissionServiceInterface
3839
{
40+
private const SPECIAL_PERMISSIONS = [
41+
'localizedView',
42+
'localizedEdit',
43+
];
44+
3945
public function __construct(
4046
private EventServiceInterface $eventService,
47+
private LanguageServiceInterface $languageService,
4148
private WorkspaceServiceInterface $workspaceService,
4249
) {
4350
}
@@ -102,14 +109,41 @@ public function checkWorkspacePermission(
102109
return $this->getPermissionValue($permissions, $permission);
103110
}
104111

105-
public function getPermissionValue(BasePermissions $permissions, string $permission): bool
112+
public function getPermissionValue(
113+
AssetPermissions|DocumentPermissions|DataObjectPermissions $permissions,
114+
string $permission,
115+
?string $permissionValueKey = null
116+
): bool {
117+
$getter = 'is' . ucfirst($permission);
118+
if (!method_exists($permissions, $getter)) {
119+
return false;
120+
}
121+
122+
$value = $permissions->$getter();
123+
if ($permissions instanceof DataObjectPermissions && !is_bool($value)) {
124+
return in_array(
125+
$permissionValueKey,
126+
$this->getSpecialPermissionValues($permissions, $permission),
127+
true
128+
);
129+
}
130+
131+
return $value;
132+
}
133+
134+
public function getSpecialPermissionValues(DataObjectPermissions $permissions, string $permission): array
106135
{
136+
if (!in_array($permission, self::SPECIAL_PERMISSIONS)) {
137+
return [];
138+
}
139+
107140
$getter = 'is' . ucfirst($permission);
108-
if (method_exists($permissions, $getter)) {
109-
return $permissions->$getter();
141+
$permissionValues = $permissions->$getter();
142+
if ($permissionValues === null) {
143+
return [];
110144
}
111145

112-
return false;
146+
return explode(',', $permissionValues);
113147
}
114148

115149
private function getPermissions(
@@ -155,9 +189,15 @@ private function getAdminUserPermissions(
155189

156190
$properties = $permissions->getClassProperties();
157191
foreach ($properties as $property => $value) {
192+
$setter = 'set' . ucfirst($property);
158193
if (is_bool($value)) {
159-
$setter = 'set' . ucfirst($property);
160194
$permissions->$setter(true);
195+
196+
continue;
197+
}
198+
199+
if (in_array($property, self::SPECIAL_PERMISSIONS, true)) {
200+
$permissions->$setter(implode(',', $this->languageService->getValidLanguages()));
161201
}
162202
}
163203

src/Service/Permission/PermissionServiceInterface.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\DataObject\SearchResult\DataObjectSearchResultItem;
2121
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Document\SearchResult\DocumentSearchResultItem;
2222
use Pimcore\Bundle\GenericDataIndexBundle\Permission\AssetPermissions;
23-
use Pimcore\Bundle\GenericDataIndexBundle\Permission\BasePermissions;
2423
use Pimcore\Bundle\GenericDataIndexBundle\Permission\DataObjectPermissions;
2524
use Pimcore\Bundle\GenericDataIndexBundle\Permission\DocumentPermissions;
2625
use Pimcore\Bundle\GenericDataIndexBundle\Permission\Workspace\WorkspaceInterface;
@@ -51,5 +50,11 @@ public function checkWorkspacePermission(
5150
string $permission
5251
): bool;
5352

54-
public function getPermissionValue(BasePermissions $permissions, string $permission): bool;
53+
public function getPermissionValue(
54+
AssetPermissions|DocumentPermissions|DataObjectPermissions $permissions,
55+
string $permission,
56+
?string $permissionValueKey = null
57+
): bool;
58+
59+
public function getSpecialPermissionValues(DataObjectPermissions $permissions, string $permission): array;
5560
}

tests/Unit/Service/Permission/PermissionServiceTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Pimcore\Bundle\GenericDataIndexBundle\Permission\Workspace\DocumentWorkspace;
2828
use Pimcore\Bundle\GenericDataIndexBundle\Service\EventService;
2929
use Pimcore\Bundle\GenericDataIndexBundle\Service\Permission\PermissionService;
30+
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\LanguageServiceInterface;
3031
use Pimcore\Bundle\GenericDataIndexBundle\Service\Workspace\WorkspaceService;
3132
use Pimcore\Bundle\StaticResolverBundle\Models\User\UserResolver;
3233
use Pimcore\Bundle\StaticResolverBundle\Models\User\UserResolverInterface;
@@ -473,6 +474,7 @@ private function getPermissionServiceWithUser(): PermissionService
473474
{
474475
return new PermissionService(
475476
$this->getEventService(),
477+
$this->makeEmpty(LanguageServiceInterface::class),
476478
new WorkspaceService(
477479
$this->makeEmpty(UserResolverInterface::class, [
478480
'getUserRoleById' => $this->role,
@@ -485,6 +487,7 @@ private function getPermissionServiceWithoutUser(): PermissionService
485487
{
486488
return new PermissionService(
487489
$this->getEventService(),
490+
$this->makeEmpty(LanguageServiceInterface::class),
488491
new WorkspaceService(
489492
new UserResolver()
490493
)

0 commit comments

Comments
 (0)