Skip to content

Commit

Permalink
TASK: Add setting to ignore values of certain form element types in F…
Browse files Browse the repository at this point in the history
…inisher

It may not be desirable to store data for certain form element types
in a Node-based form. For example, for security reasons you might not
want to save the content of Password fields or you might want to
skip saving a captcha value.

To achieve this, a new configuration "nodeTypesIgnoredInFinisher"
is added, by default containing

- Neos.Form.Builder:Section
- Neos.Form.Builder:StaticText
  • Loading branch information
lorenzulrich committed May 18, 2022
1 parent 895cdba commit cfe008d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
25 changes: 18 additions & 7 deletions Classes/Finishers/DatabaseStorageFinisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use Wegmeister\DatabaseStorage\Domain\Model\DatabaseStorage;
use Wegmeister\DatabaseStorage\Domain\Repository\DatabaseStorageRepository;
use Wegmeister\DatabaseStorage\Service\DatabaseStorageService;

/**
* A simple finisher that stores data into database
Expand All @@ -35,6 +36,11 @@ class DatabaseStorageFinisher extends AbstractFinisher
*/
protected $databaseStorageRepository;

/**
* @var DatabaseStorageService
*/
protected $databaseStorageService;

/**
* Executes this finisher
*
Expand All @@ -48,17 +54,22 @@ protected function executeInternal()
$formRuntime = $this->finisherContext->getFormRuntime();
$formValues = $formRuntime->getFormState()->getFormValues();

foreach ($formValues as &$formValue) {
if ($formValue instanceof AssetInterface) {
$formValue = $formValue->getResource();
}
}

$identifier = $this->parseOption('identifier');
if (!$identifier) {
if ($identifier) {
$this->databaseStorageService = new DatabaseStorageService($identifier);
} else {
$identifier = '__undefined__';
}

foreach ($formValues as $formElementIdentifier => $formValue) {
if ($formValue instanceof AssetInterface) {
$formValues[$formElementIdentifier] = $formValue->getResource();
}
if ($identifier && $this->databaseStorageService->formElementIdentifierMustBeIgnoredInFinisher($formElementIdentifier)) {
unset($formValues[$formElementIdentifier]);
}
}

$dbStorage = new DatabaseStorage();
$dbStorage
->setStorageidentifier($identifier)
Expand Down
16 changes: 16 additions & 0 deletions Classes/Service/DatabaseStorageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ class DatabaseStorageService
*/
protected $formStorageIdentifier;

/**
* @var array
* @Flow\InjectConfiguration(path="nodeTypesIgnoredInFinisher", package="Wegmeister.DatabaseStorage")
*/
protected $nodeTypesIgnoredInFinisher;

/**
* @var array
* @Flow\InjectConfiguration(path="nodeTypesIgnoredInExport", package="Wegmeister.DatabaseStorage")
Expand Down Expand Up @@ -68,6 +74,16 @@ public function __construct(string $formStorageIdentifier = '')
$this->formStorageIdentifier = $formStorageIdentifier;
}

public function formElementIdentifierMustBeIgnoredInFinisher(string $formValueIdentifier): bool
{
$formElementData = $this->getFormElementDataByIdentifier($formValueIdentifier);
if (!$formElementData) {
// No Form definition found, so we cannot know if it should be ignored --> do nothing
return false;
}
return in_array($formElementData['nodeTypeName'], $this->nodeTypesIgnoredInFinisher);
}

public function nodeTypeMustBeIgnoredInExport($nodeTypeName): bool
{
return in_array($nodeTypeName, $this->nodeTypesIgnoredInExport);
Expand Down
3 changes: 3 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Wegmeister:
title: 'Database Export'
subject: 'Database Export'
datetimeFormat: 'Y-m-d H:i:s'
nodeTypesIgnoredInFinisher:
- 'Neos.Form.Builder:Section'
- 'Neos.Form.Builder:StaticText'
nodeTypesIgnoredInExport:
- 'Neos.Form.Builder:Section'
- 'Neos.Form.Builder:StaticText'
Expand Down
6 changes: 5 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ Wegmeister:
subject: 'Database Export'
# DateTime format if the datetime is included in the export
datetimeFormat: 'Y-m-d H:i:s'
# Form element types that should not be part of the export
# Form element types that should not be stored by the finisher (for Node-based forms)
nodeTypesIgnoredInFinisher:
- 'Neos.Form.Builder:Section'
- 'Neos.Form.Builder:StaticText'
# Form element types that should not be part of the export (for Node-based forms)
nodeTypesIgnoredInExport:
- 'Neos.Form.Builder:Section'
- 'Neos.Form.Builder:StaticText'
Expand Down

0 comments on commit cfe008d

Please sign in to comment.