Skip to content

Commit

Permalink
add encrypted field normalizer (#637)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukmzig authored Dec 13, 2024
1 parent 7cbcc42 commit f1d1c68
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/DataObject/Data/Adapter/EncryptedFieldAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

namespace Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Adapter;

use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\DataNormalizerInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\Model\FieldContextData;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\SetterDataInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataServiceInterface;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\ClassDefinition\Data\EncryptedField as EncryptedFieldDefinition;
use Pimcore\Model\DataObject\Concrete;
Expand All @@ -30,10 +32,11 @@
* @internal
*/
#[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)]
final readonly class EncryptedFieldAdapter implements SetterDataInterface
final readonly class EncryptedFieldAdapter implements SetterDataInterface, DataNormalizerInterface
{
public function __construct(
private DataAdapterServiceInterface $dataAdapterService
private DataAdapterServiceInterface $dataAdapterService,
private DataServiceInterface $dataService
) {
}

Expand Down Expand Up @@ -63,6 +66,25 @@ public function getDataForSetter(
);
}

public function normalize(
mixed $value,
Data $fieldDefinition
): mixed {
if (!$fieldDefinition instanceof EncryptedFieldDefinition) {
return null;
}

if (!$value instanceof EncryptedField) {
return $value;
}

$plainValue = $value->getPlain();
$delegateFieldDefinition = $fieldDefinition->getDelegate();

return $this->dataService->getNormalizedValue($plainValue, $delegateFieldDefinition)
?? $plainValue;
}

private function handleDelegatedField(
Concrete $element,
Data $delegateFieldDefinition,
Expand Down

0 comments on commit f1d1c68

Please sign in to comment.