-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Make formState initialization extendable
This adds a mechanism to hook into the form state initialization and define custom initializers. Resolves: #152
- Loading branch information
1 parent
b40e709
commit 124d43c
Showing
5 changed files
with
128 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Neos\Form\FormState; | ||
|
||
/* | ||
* This file is part of the Neos.Form package. | ||
* | ||
* (c) Contributors of the Neos Project - www.neos.io | ||
* | ||
* This package is Open Source Software. For the full copyright and license | ||
* information, please view the LICENSE file which was distributed with this | ||
* source code. | ||
*/ | ||
|
||
use Neos\Flow\Annotations as Flow; | ||
use Neos\Flow\Mvc\ActionRequest; | ||
use Neos\Form\Core\Model\FormDefinition; | ||
use Neos\Fusion\Form\Runtime\Domain\FormState; | ||
use Neos\Utility\PositionalArraySorter; | ||
|
||
final class FormStateInitializerChain | ||
{ | ||
|
||
/** | ||
* @Flow\InjectConfiguration(path="formStateInitializerChain") | ||
* @var string[][] | ||
*/ | ||
protected $formStateInitializerChain; | ||
|
||
public function initializeFormState(FormDefinition $formDefinition, ActionRequest $actionRequest): FormState | ||
{ | ||
$formState = new FormState(); | ||
$sortedChain = (new PositionalArraySorter($this->formStateInitializerChain))->toArray(); | ||
|
||
foreach ($sortedChain as $initializer) { | ||
$class = $initializer['class'] ?? ''; | ||
if (!is_a($class, FormStateInitializerInterface::class)) { | ||
throw new \RuntimeException(sprintf('The given class "%s" does not implement the interface %s', $class, FormStateInitializerInterface::class), 1648204540); | ||
} | ||
|
||
$formState = (new $class())->initializeState($formDefinition, $actionRequest, $formState); | ||
} | ||
|
||
return $formState; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Neos\Form\FormState; | ||
|
||
/* | ||
* This file is part of the Neos.Form package. | ||
* | ||
* (c) Contributors of the Neos Project - www.neos.io | ||
* | ||
* This package is Open Source Software. For the full copyright and license | ||
* information, please view the LICENSE file which was distributed with this | ||
* source code. | ||
*/ | ||
|
||
use Neos\Flow\Mvc\ActionRequest; | ||
use Neos\Form\Core\Model\FormDefinition; | ||
use Neos\Form\Core\Runtime\FormState; | ||
|
||
interface FormStateInitializerInterface | ||
{ | ||
public function initializeState(FormDefinition $formDefinition, ActionRequest $actionRequest, FormState $previousFormState): FormState; | ||
} |
43 changes: 43 additions & 0 deletions
43
Classes/FormState/SerializedFormStateArgumentInitializer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Neos\Form\FormState; | ||
|
||
/* | ||
* This file is part of the Neos.Form package. | ||
* | ||
* (c) Contributors of the Neos Project - www.neos.io | ||
* | ||
* This package is Open Source Software. For the full copyright and license | ||
* information, please view the LICENSE file which was distributed with this | ||
* source code. | ||
*/ | ||
|
||
use Neos\Flow\Annotations as Flow; | ||
use Neos\Flow\Mvc\ActionRequest; | ||
use Neos\Flow\Security\Cryptography\HashService; | ||
use Neos\Form\Core\Model\FormDefinition; | ||
use Neos\Form\Core\Runtime\FormState; | ||
|
||
class SerializedFormStateArgumentInitializer implements FormStateInitializerInterface | ||
{ | ||
|
||
/** | ||
* @Flow\Inject | ||
* @var HashService | ||
* @internal | ||
*/ | ||
protected $hashService; | ||
|
||
public function initializeState(FormDefinition $formDefinition, ActionRequest $actionRequest, FormState $previousFormState): FormState | ||
{ | ||
$serializedFormStateWithHmac = $actionRequest->getInternalArgument('__state'); | ||
if ($serializedFormStateWithHmac !== null) { | ||
$serializedFormState = $this->hashService->validateAndStripHmac($serializedFormStateWithHmac); | ||
/** @noinspection UnserializeExploitsInspection The unserialize call is safe because of the HMAC check above */ | ||
return unserialize(base64_decode($serializedFormState)); | ||
} | ||
|
||
return $previousFormState; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters