From 0520ed348dab738867a40317ad6a8d91bf20a9c8 Mon Sep 17 00:00:00 2001 From: Valentina Da Rold Date: Wed, 22 Apr 2020 16:36:34 +0200 Subject: [PATCH] Add ConfigFormEventsHook to UserForm.php Add ConfigFormEventsHook to UserGroupForm.php Add check in getValues for UserForm.php --- application/forms/Config/User/UserForm.php | 36 ++++++++++++++++++- .../forms/Config/UserGroup/UserGroupForm.php | 31 ++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/application/forms/Config/User/UserForm.php b/application/forms/Config/User/UserForm.php index 6605a60a2..fb2ef4dc3 100644 --- a/application/forms/Config/User/UserForm.php +++ b/application/forms/Config/User/UserForm.php @@ -3,8 +3,10 @@ namespace Icinga\Forms\Config\User; +use Icinga\Application\Hook\ConfigFormEventsHook; use Icinga\Data\Filter\Filter; use Icinga\Forms\RepositoryForm; +use Icinga\Web\Notification; class UserForm extends RepositoryForm { @@ -97,7 +99,10 @@ class UserForm extends RepositoryForm public function getValues($suppressArrayNotation = false) { $values = parent::getValues($suppressArrayNotation); - if (! $values['password']) { + // before checking if password values is empty + // we have to check that the password field is set + // otherwise an error is thrown + if (isset($values['password']) && ! $values['password']) { unset($values['password']); } @@ -173,4 +178,33 @@ class UserForm extends RepositoryForm return sprintf($this->translate('Failed to remove user "%s"'), $this->getIdentifier()); } } + + public function isValid($formData) + { + $valid = parent::isValid($formData); + + if ($valid && ConfigFormEventsHook::runIsValid($this) === false) { + foreach (ConfigFormEventsHook::getLastErrors() as $msg) { + $this->error($msg); + } + + $valid = false; + } + + return $valid; + } + + public function onSuccess() + { + if (parent::onSuccess() === false) { + return false; + } + + if (ConfigFormEventsHook::runOnSuccess($this) === false) { + Notification::error($this->translate( + 'Configuration successfully stored. Though, one or more module hooks failed to run.' + . ' See logs for details' + )); + } + } } diff --git a/application/forms/Config/UserGroup/UserGroupForm.php b/application/forms/Config/UserGroup/UserGroupForm.php index a590524de..b944e9728 100644 --- a/application/forms/Config/UserGroup/UserGroupForm.php +++ b/application/forms/Config/UserGroup/UserGroupForm.php @@ -3,8 +3,10 @@ namespace Icinga\Forms\Config\UserGroup; +use Icinga\Application\Hook\ConfigFormEventsHook; use Icinga\Data\Filter\Filter; use Icinga\Forms\RepositoryForm; +use Icinga\Web\Notification; class UserGroupForm extends RepositoryForm { @@ -124,4 +126,33 @@ class UserGroupForm extends RepositoryForm return sprintf($this->translate('Failed to remove group "%s"'), $this->getIdentifier()); } } + + public function isValid($formData) + { + $valid = parent::isValid($formData); + + if ($valid && ConfigFormEventsHook::runIsValid($this) === false) { + foreach (ConfigFormEventsHook::getLastErrors() as $msg) { + $this->error($msg); + } + + $valid = false; + } + + return $valid; + } + + public function onSuccess() + { + if (parent::onSuccess() === false) { + return false; + } + + if (ConfigFormEventsHook::runOnSuccess($this) === false) { + Notification::error($this->translate( + 'Configuration successfully stored. Though, one or more module hooks failed to run.' + . ' See logs for details' + )); + } + } }