From e19dc0347ad18269973fa75f82133b16aaa7e6b1 Mon Sep 17 00:00:00 2001 From: Marco Perberschlager Date: Tue, 15 Jul 2025 12:21:35 +0200 Subject: [PATCH 1/4] GDI adapter implementation --- composer.json | 1 + .../FrontendPermissionToolkitExtension.php | 1 + .../DynamicPermissionResourceAdapter.php | 38 ++++++++++ .../PermissionRelationAdapter.php | 71 +++++++++++++++++++ .../PermissionResourceAdapter.php | 30 ++++++++ src/Resources/config/generic-data-index.yaml | 21 ++++++ 6 files changed, 162 insertions(+) create mode 100644 src/GenericDataIndex/FieldDefinitionAdapter/DynamicPermissionResourceAdapter.php create mode 100644 src/GenericDataIndex/FieldDefinitionAdapter/PermissionRelationAdapter.php create mode 100644 src/GenericDataIndex/FieldDefinitionAdapter/PermissionResourceAdapter.php create mode 100644 src/Resources/config/generic-data-index.yaml diff --git a/composer.json b/composer.json index 7224681..e13fb71 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "require": { "php": "~8.3.0 || ~8.4.0", "pimcore/pimcore": "^12.0", + "pimcore/generic-data-index-bundle": "^2.1", "symfony/event-dispatcher-contracts": "^3.0" }, "conflict": { diff --git a/src/DependencyInjection/FrontendPermissionToolkitExtension.php b/src/DependencyInjection/FrontendPermissionToolkitExtension.php index 4789eed..e4508d0 100644 --- a/src/DependencyInjection/FrontendPermissionToolkitExtension.php +++ b/src/DependencyInjection/FrontendPermissionToolkitExtension.php @@ -30,5 +30,6 @@ public function load(array $configs, ContainerBuilder $container): void ); $loader->load('services.yml'); + $loader->load('generic-data-index.yaml'); } } diff --git a/src/GenericDataIndex/FieldDefinitionAdapter/DynamicPermissionResourceAdapter.php b/src/GenericDataIndex/FieldDefinitionAdapter/DynamicPermissionResourceAdapter.php new file mode 100644 index 0000000..b535687 --- /dev/null +++ b/src/GenericDataIndex/FieldDefinitionAdapter/DynamicPermissionResourceAdapter.php @@ -0,0 +1,38 @@ + [ + 'key' => [ + 'type' => AttributeType::TEXT, + ], + 'value' => [ + 'type' => AttributeType::TEXT, + ], + ], + ]; + } +} diff --git a/src/GenericDataIndex/FieldDefinitionAdapter/PermissionRelationAdapter.php b/src/GenericDataIndex/FieldDefinitionAdapter/PermissionRelationAdapter.php new file mode 100644 index 0000000..3d507ee --- /dev/null +++ b/src/GenericDataIndex/FieldDefinitionAdapter/PermissionRelationAdapter.php @@ -0,0 +1,71 @@ + [ + 'object' => [ + 'type' => AttributeType::LONG->value, + ], + 'asset' => [ + 'type' => AttributeType::LONG->value, + ], + 'document' => [ + 'type' => AttributeType::LONG->value, + ], + ], + ]; + } + + public function normalize(mixed $value): ?array + { + $fieldDefinition = $this->getFieldDefinition(); + if (!$fieldDefinition instanceof NormalizerInterface) { + return null; + } + + $returnValue = [ + 'object' => [], + 'asset' => [], + 'document' => [], + ]; + $normalizedValues = $fieldDefinition->normalize($value); + + if (is_array($normalizedValues)) { + // Mapping For ManyToOne + if (isset($normalizedValues['type'], $normalizedValues['id'])) { + $returnValue[$normalizedValues['type']][] = $normalizedValues['id']; + } + + foreach ($normalizedValues as $normalizedValue) { + if (isset($normalizedValue['type'], $normalizedValue['id'])) { + $returnValue[$normalizedValue['type']][] = $normalizedValue['id']; + } + } + } + + return $returnValue; + } +} diff --git a/src/GenericDataIndex/FieldDefinitionAdapter/PermissionResourceAdapter.php b/src/GenericDataIndex/FieldDefinitionAdapter/PermissionResourceAdapter.php new file mode 100644 index 0000000..3d68c59 --- /dev/null +++ b/src/GenericDataIndex/FieldDefinitionAdapter/PermissionResourceAdapter.php @@ -0,0 +1,30 @@ + AttributeType::TEXT, + ]; + } +} diff --git a/src/Resources/config/generic-data-index.yaml b/src/Resources/config/generic-data-index.yaml new file mode 100644 index 0000000..87c4ec8 --- /dev/null +++ b/src/Resources/config/generic-data-index.yaml @@ -0,0 +1,21 @@ +services: + _defaults: + autowire: true + autoconfigure: true + public: false + + FrontendPermissionToolkitBundle\GenericDataIndex\FieldDefinitionAdapter\PermissionResourceAdapter: + shared: false + tags: + - { name: "pimcore.generic_data_index.data-object.search_index_field_definition", type: "permissionResource" } + + FrontendPermissionToolkitBundle\GenericDataIndex\FieldDefinitionAdapter\DynamicPermissionResourceAdapter: + shared: false + tags: + - { name: "pimcore.generic_data_index.data-object.search_index_field_definition", type: "dynamicPermissionResource" } + + FrontendPermissionToolkitBundle\GenericDataIndex\FieldDefinitionAdapter\PermissionRelationAdapter: + shared: false + tags: + - { name: "pimcore.generic_data_index.data-object.search_index_field_definition", type: "permissionManyToManyRelation" } + - { name: "pimcore.generic_data_index.data-object.search_index_field_definition", type: "permissionManyToOneRelation" } \ No newline at end of file From ec8226739f1021f4c078a8a8707da39bfed15320 Mon Sep 17 00:00:00 2001 From: Marco Perberschlager Date: Tue, 15 Jul 2025 16:06:56 +0200 Subject: [PATCH 2/4] Updated adapter to reuse existing code --- .../DynamicPermissionResourceAdapter.php | 43 ++++++++--- .../PermissionRelationAdapter.php | 71 ------------------- .../PermissionResourceAdapter.php | 30 -------- src/Resources/config/generic-data-index.yaml | 16 +++-- 4 files changed, 42 insertions(+), 118 deletions(-) delete mode 100644 src/GenericDataIndex/FieldDefinitionAdapter/PermissionRelationAdapter.php delete mode 100644 src/GenericDataIndex/FieldDefinitionAdapter/PermissionResourceAdapter.php diff --git a/src/GenericDataIndex/FieldDefinitionAdapter/DynamicPermissionResourceAdapter.php b/src/GenericDataIndex/FieldDefinitionAdapter/DynamicPermissionResourceAdapter.php index b535687..b2d9908 100644 --- a/src/GenericDataIndex/FieldDefinitionAdapter/DynamicPermissionResourceAdapter.php +++ b/src/GenericDataIndex/FieldDefinitionAdapter/DynamicPermissionResourceAdapter.php @@ -13,8 +13,12 @@ namespace FrontendPermissionToolkitBundle\GenericDataIndex\FieldDefinitionAdapter; +use FrontendPermissionToolkitBundle\CoreExtensions\ClassDefinitions\DynamicPermissionResource; use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\DefaultSearch\AttributeType; +use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\DataObject\FieldDefinitionServiceInterface; use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\DefaultSearch\DataObject\FieldDefinitionAdapter\AbstractAdapter; +use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\IndexMappingServiceInterface; +use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\SearchIndexConfigServiceInterface; use Pimcore\Normalizer\NormalizerInterface; /** @@ -22,17 +26,36 @@ */ final class DynamicPermissionResourceAdapter extends AbstractAdapter { + public function __construct( + protected SearchIndexConfigServiceInterface $searchIndexConfigService, + protected FieldDefinitionServiceInterface $fieldDefinitionService, + private readonly IndexMappingServiceInterface $indexMappingService, + ) { + parent::__construct( + $searchIndexConfigService, + $fieldDefinitionService + ); + } + public function getIndexMapping(): array { - return [ - 'properties' => [ - 'key' => [ - 'type' => AttributeType::TEXT, - ], - 'value' => [ - 'type' => AttributeType::TEXT, - ], - ], - ]; + return $this->indexMappingService->getMappingForTextKeyword( + $this->searchIndexConfigService->getSearchAnalyzerAttributes() + ); + } + + public function normalize(mixed $value): ?array + { + $fieldDefinition = $this->getFieldDefinition(); + if (!$fieldDefinition instanceof DynamicPermissionResource) { + return null; + } + + $normalizedValues = []; + foreach($value as $item) { + $normalizedValues[] = $item; + } + + return $normalizedValues; } } diff --git a/src/GenericDataIndex/FieldDefinitionAdapter/PermissionRelationAdapter.php b/src/GenericDataIndex/FieldDefinitionAdapter/PermissionRelationAdapter.php deleted file mode 100644 index 3d507ee..0000000 --- a/src/GenericDataIndex/FieldDefinitionAdapter/PermissionRelationAdapter.php +++ /dev/null @@ -1,71 +0,0 @@ - [ - 'object' => [ - 'type' => AttributeType::LONG->value, - ], - 'asset' => [ - 'type' => AttributeType::LONG->value, - ], - 'document' => [ - 'type' => AttributeType::LONG->value, - ], - ], - ]; - } - - public function normalize(mixed $value): ?array - { - $fieldDefinition = $this->getFieldDefinition(); - if (!$fieldDefinition instanceof NormalizerInterface) { - return null; - } - - $returnValue = [ - 'object' => [], - 'asset' => [], - 'document' => [], - ]; - $normalizedValues = $fieldDefinition->normalize($value); - - if (is_array($normalizedValues)) { - // Mapping For ManyToOne - if (isset($normalizedValues['type'], $normalizedValues['id'])) { - $returnValue[$normalizedValues['type']][] = $normalizedValues['id']; - } - - foreach ($normalizedValues as $normalizedValue) { - if (isset($normalizedValue['type'], $normalizedValue['id'])) { - $returnValue[$normalizedValue['type']][] = $normalizedValue['id']; - } - } - } - - return $returnValue; - } -} diff --git a/src/GenericDataIndex/FieldDefinitionAdapter/PermissionResourceAdapter.php b/src/GenericDataIndex/FieldDefinitionAdapter/PermissionResourceAdapter.php deleted file mode 100644 index 3d68c59..0000000 --- a/src/GenericDataIndex/FieldDefinitionAdapter/PermissionResourceAdapter.php +++ /dev/null @@ -1,30 +0,0 @@ - AttributeType::TEXT, - ]; - } -} diff --git a/src/Resources/config/generic-data-index.yaml b/src/Resources/config/generic-data-index.yaml index 87c4ec8..0137295 100644 --- a/src/Resources/config/generic-data-index.yaml +++ b/src/Resources/config/generic-data-index.yaml @@ -4,18 +4,20 @@ services: autoconfigure: true public: false - FrontendPermissionToolkitBundle\GenericDataIndex\FieldDefinitionAdapter\PermissionResourceAdapter: + pimcore.front_permission_toolkit.generic_data_index.data_object.relation-adapter: + class: Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\DefaultSearch\DataObject\FieldDefinitionAdapter\RelationAdapter shared: false tags: - - { name: "pimcore.generic_data_index.data-object.search_index_field_definition", type: "permissionResource" } + - { name: "pimcore.generic_data_index.data-object.search_index_field_definition", type: "permissionManyToManyRelation" } + - { name: "pimcore.generic_data_index.data-object.search_index_field_definition", type: "permissionManyToOneRelation" } - FrontendPermissionToolkitBundle\GenericDataIndex\FieldDefinitionAdapter\DynamicPermissionResourceAdapter: + pimcore.front_permission_toolkit.generic_data_index.data_object.dynamic-permission-resource-adapter: + class: Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\DefaultSearch\DataObject\FieldDefinitionAdapter\TextKeywordAdapter shared: false tags: - - { name: "pimcore.generic_data_index.data-object.search_index_field_definition", type: "dynamicPermissionResource" } + - { name: "pimcore.generic_data_index.data-object.search_index_field_definition", type: "permissionResource" } - FrontendPermissionToolkitBundle\GenericDataIndex\FieldDefinitionAdapter\PermissionRelationAdapter: + FrontendPermissionToolkitBundle\GenericDataIndex\FieldDefinitionAdapter\DynamicPermissionResourceAdapter: shared: false tags: - - { name: "pimcore.generic_data_index.data-object.search_index_field_definition", type: "permissionManyToManyRelation" } - - { name: "pimcore.generic_data_index.data-object.search_index_field_definition", type: "permissionManyToOneRelation" } \ No newline at end of file + - { name: "pimcore.generic_data_index.data-object.search_index_field_definition", type: "dynamicPermissionResource" } From fd79c2d104184f403be6e777bb3bd932528b2fbc Mon Sep 17 00:00:00 2001 From: Marco Perberschlager Date: Tue, 15 Jul 2025 16:21:47 +0200 Subject: [PATCH 3/4] Use array_values for simplification --- .../DynamicPermissionResourceAdapter.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/GenericDataIndex/FieldDefinitionAdapter/DynamicPermissionResourceAdapter.php b/src/GenericDataIndex/FieldDefinitionAdapter/DynamicPermissionResourceAdapter.php index b2d9908..8a77e6f 100644 --- a/src/GenericDataIndex/FieldDefinitionAdapter/DynamicPermissionResourceAdapter.php +++ b/src/GenericDataIndex/FieldDefinitionAdapter/DynamicPermissionResourceAdapter.php @@ -51,11 +51,6 @@ public function normalize(mixed $value): ?array return null; } - $normalizedValues = []; - foreach($value as $item) { - $normalizedValues[] = $item; - } - - return $normalizedValues; + return array_values($value); } } From e3baa077fdc07807173c65c8ea4360d7ddd4ef0a Mon Sep 17 00:00:00 2001 From: Marco Perberschlager Date: Tue, 15 Jul 2025 16:23:26 +0200 Subject: [PATCH 4/4] Removed unused namespaces --- .../FieldDefinitionAdapter/DynamicPermissionResourceAdapter.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/GenericDataIndex/FieldDefinitionAdapter/DynamicPermissionResourceAdapter.php b/src/GenericDataIndex/FieldDefinitionAdapter/DynamicPermissionResourceAdapter.php index 8a77e6f..83e8032 100644 --- a/src/GenericDataIndex/FieldDefinitionAdapter/DynamicPermissionResourceAdapter.php +++ b/src/GenericDataIndex/FieldDefinitionAdapter/DynamicPermissionResourceAdapter.php @@ -14,12 +14,10 @@ namespace FrontendPermissionToolkitBundle\GenericDataIndex\FieldDefinitionAdapter; use FrontendPermissionToolkitBundle\CoreExtensions\ClassDefinitions\DynamicPermissionResource; -use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\DefaultSearch\AttributeType; use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\DataObject\FieldDefinitionServiceInterface; use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\DefaultSearch\DataObject\FieldDefinitionAdapter\AbstractAdapter; use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\IndexMappingServiceInterface; use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\SearchIndexConfigServiceInterface; -use Pimcore\Normalizer\NormalizerInterface; /** * @internal