Merge pull request #4023 from Icinga/fix/role-form-does-not-use-config-form-events

Fix that the role form doesn't use config form events
This commit is contained in:
Johannes Meyer 2019-12-09 09:35:56 +01:00 committed by GitHub
commit af92d4fc89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 13 deletions

View File

@ -3,12 +3,14 @@
namespace Icinga\Forms\Security; namespace Icinga\Forms\Security;
use Icinga\Application\Hook\ConfigFormEventsHook;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Application\Modules\Manager; use Icinga\Application\Modules\Manager;
use Icinga\Data\Filter\Filter; use Icinga\Data\Filter\Filter;
use Icinga\Forms\ConfigForm; use Icinga\Forms\ConfigForm;
use Icinga\Forms\RepositoryForm; use Icinga\Forms\RepositoryForm;
use Icinga\Util\StringHelper; use Icinga\Util\StringHelper;
use Icinga\Web\Notification;
use Zend_Form_Element; use Zend_Form_Element;
/** /**
@ -384,4 +386,33 @@ class RoleForm extends RepositoryForm
return strnatcmp($a, $b); return strnatcmp($a, $b);
}); });
} }
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'
));
}
}
} }

View File

@ -6,7 +6,7 @@ namespace Icinga\Application\Hook;
use Icinga\Application\Hook; use Icinga\Application\Hook;
use Icinga\Application\Logger; use Icinga\Application\Logger;
use Icinga\Exception\IcingaException; use Icinga\Exception\IcingaException;
use Icinga\Forms\ConfigForm; use Icinga\Web\Form;
/** /**
* Base class for config form event hooks * Base class for config form event hooks
@ -19,11 +19,11 @@ abstract class ConfigFormEventsHook
/** /**
* Get whether the hook applies to the given config form * Get whether the hook applies to the given config form
* *
* @param ConfigForm $form * @param Form $form
* *
* @return bool * @return bool
*/ */
public function appliesTo(ConfigForm $form) public function appliesTo(Form $form)
{ {
return false; return false;
} }
@ -36,11 +36,11 @@ abstract class ConfigFormEventsHook
* The exception's message will be automatically added as form error message so that it will be * The exception's message will be automatically added as form error message so that it will be
* displayed in the frontend. * displayed in the frontend.
* *
* @param ConfigForm $form * @param Form $form
* *
* @throws \Exception If either the form is not valid or to interrupt the form handling * @throws \Exception If either the form is not valid or to interrupt the form handling
*/ */
public function isValid(ConfigForm $form) public function isValid(Form $form)
{ {
} }
@ -50,9 +50,9 @@ abstract class ConfigFormEventsHook
* Implement this method in order to run code after the configuration form has been stored successfully. * Implement this method in order to run code after the configuration form has been stored successfully.
* You can't interrupt the form handling here. Any exception will be caught, logged and notified. * You can't interrupt the form handling here. Any exception will be caught, logged and notified.
* *
* @param ConfigForm $form * @param Form $form
*/ */
public function onSuccess(ConfigForm $form) public function onSuccess(Form $form)
{ {
} }
@ -69,11 +69,11 @@ abstract class ConfigFormEventsHook
/** /**
* Run all isValid hooks * Run all isValid hooks
* *
* @param ConfigForm $form * @param Form $form
* *
* @return bool Returns false if any hook threw an exception * @return bool Returns false if any hook threw an exception
*/ */
final public static function runIsValid(ConfigForm $form) final public static function runIsValid(Form $form)
{ {
return self::runEventMethod('isValid', $form); return self::runEventMethod('isValid', $form);
} }
@ -81,16 +81,16 @@ abstract class ConfigFormEventsHook
/** /**
* Run all onSuccess hooks * Run all onSuccess hooks
* *
* @param ConfigForm $form * @param Form $form
* *
* @return bool Returns false if any hook threw an exception * @return bool Returns false if any hook threw an exception
*/ */
final public static function runOnSuccess(ConfigForm $form) final public static function runOnSuccess(Form $form)
{ {
return self::runEventMethod('onSuccess', $form); return self::runEventMethod('onSuccess', $form);
} }
private static function runEventMethod($eventMethod, ConfigForm $form) private static function runEventMethod($eventMethod, Form $form)
{ {
static::$lastErrors = []; static::$lastErrors = [];
@ -120,7 +120,7 @@ abstract class ConfigFormEventsHook
return $success; return $success;
} }
private function runAppliesTo(ConfigForm $form) private function runAppliesTo(Form $form)
{ {
try { try {
$appliesTo = $this->appliesTo($form); $appliesTo = $this->appliesTo($form);