3 Commits
Author SHA1 Message Date
mcndave f1ba948a3a - Removed CSRF form validation from FormHandler / added to FormContext formExecute method.
- CRSF tokens are now consumed if a form return execute value is (bool) true, preventing double form send
2026-10-27 18:25:42 +00:00
mcndave 6d0a68c6db - WIP on package manager addition 2026-10-27 18:25:42 +00:00
Dave Mc Nicoll e1ba05a66d - Fixed a bug within FormContext while unpacking arguments for an object when no array were given 2026-10-22 15:17:57 +00:00
3 changed files with 32 additions and 8 deletions

No files matched your search

+3 -3
View File
@@ -10,12 +10,12 @@
}
],
"require": {
"mcnd/picea": "dev-master"
"mcnd/picea": "^1.4.0"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/mcNdave/picea.git"
"type": "composer",
"url": "https://git.mcnd.ca/api/packages/mcndave/composer"
}
],
"autoload": {
+26 -3
View File
@@ -76,6 +76,17 @@ class FormContext implements FormContextInterface
return true;
}
public function formExecute($returnValue) : void
{
$this->formExecutionStatus = $returnValue;
$this->formExecuted = true;
if ($returnValue) {
$this->consumeCsrfToken();
}
}
public function executed() : bool
{
return $this->formExecutionStatus === false ? false : $this->formExecuted;
@@ -109,6 +120,17 @@ class FormContext implements FormContextInterface
return $this->formExecutionStatus;
}
public function consumeCsrfToken() {
if (! $this->formSkipCsrf) {
$token = $this->get('picea-ui-form')[$this->formName] ?? false;
if ( $token ) {
# token is consumed
$_SESSION["picea-ui.form:{$this->formName}"] = array_diff($_SESSION["picea-ui.form:{$this->formName}"], [ $token ]);
}
}
}
public function getRequest() : ServerRequestInterface
{
return $this->formContextRequest;
@@ -226,7 +248,7 @@ class FormContext implements FormContextInterface
is_null($value) => null,
enum_exists($e) => ( $value instanceof \BackedEnum ? $value : $e::tryFrom($value) ) ?? throw new \InvalidArgumentException(sprintf("Field '\$$property' awaiting values : '%s'.", implode("', '", array_map(fn($e) => $e->value, $e::cases())))),
function_exists($e) => $e($value),
class_exists($e) => new $e(...$value),
class_exists($e) => is_array($value) ? new $e(...$value) : new $e($value),
interface_exists($e) => throw new \InvalidArgumentException("There is no way provided to match an interface object yet."),
default => $value
};
@@ -238,11 +260,12 @@ class FormContext implements FormContextInterface
}
elseif ($types instanceof \ReflectionIntersectionType || $types instanceof \ReflectionUnionType) {
foreach($types->getTypes() as $type) {
return $cast($type->getName());
$value = $cast($type->getName());
break;
}
}
elseif ($types->isBuiltin()) {
return $cast($types->getName());
$value = $cast($types->getName());
}
return $value;
+3 -2
View File
@@ -13,6 +13,7 @@ class FormHandler {
public bool $validateCsrfToken = true;
# in PHP 8.5, value should be taken directly from a property get within $context->formExecutionStatus
public /* mixed */ $executionStatus = null;
public ? bool $validationStatus = null;
@@ -49,8 +50,8 @@ class FormHandler {
if ( $this->sent && $this->context->formSent() ) {
if ( $this->validationStatus = $this->form->validate($this->context) ) {
$this->context->formExecutionStatus = $this->executionStatus = $this->form->execute($this->context);
$this->context->formExecuted = true;
$this->executionStatus = $this->form->execute($this->context);
$this->context->formExecute($this->executionStatus);
}
}
}