From e8af4295b14bb1d026c71629c47a9066e1b1318b Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 29 Jul 2015 10:52:32 +0200 Subject: [PATCH] Allow to manually validate the configuration in the wizard as well It's a mess... --- .../forms/Setup/IdoResourcePage.php | 47 ++++++++++++++++++ .../library/Monitoring/MonitoringWizard.php | 13 +++++ .../application/forms/AuthBackendPage.php | 49 +++++++++++++++++++ .../application/forms/DbResourcePage.php | 30 ++++++++++++ .../application/forms/LdapResourcePage.php | 47 ++++++++++++++++++ modules/setup/library/Setup/WebWizard.php | 21 ++++++++ 6 files changed, 207 insertions(+) diff --git a/modules/monitoring/application/forms/Setup/IdoResourcePage.php b/modules/monitoring/application/forms/Setup/IdoResourcePage.php index ea244d057..e0b74c3ac 100644 --- a/modules/monitoring/application/forms/Setup/IdoResourcePage.php +++ b/modules/monitoring/application/forms/Setup/IdoResourcePage.php @@ -21,6 +21,7 @@ class IdoResourcePage extends Form $this->addDescription($this->translate( 'Please fill out the connection details below to access the IDO database of your monitoring environment.' )); + $this->setValidatePartial(true); } /** @@ -96,6 +97,52 @@ class IdoResourcePage extends Form return true; } + /** + * Run the configured backend's inspection checks and show the result, if necessary + * + * This will only run any validation if the user pushed the 'backend_validation' button. + * + * @param array $formData + * + * @return bool + */ + public function isValidPartial(array $formData) + { + if (isset($formData['backend_validation']) && parent::isValid($formData)) { + $inspection = ResourceConfigForm::inspectResource($this); + if ($inspection !== null) { + $join = function ($e) use (& $join) { + return is_string($e) ? $e : join("\n", array_map($join, $e)); + }; + $this->addElement( + 'note', + 'inspection_output', + array( + 'order' => 0, + 'value' => '' . $this->translate('Validation Log') . "\n\n" + . join("\n", array_map($join, $inspection->toArray())), + 'decorators' => array( + 'ViewHelper', + array('HtmlTag', array('tag' => 'pre', 'class' => 'log-output')), + ) + ) + ); + + if ($inspection->hasError()) { + $this->warning(sprintf( + $this->translate('Failed to successfully validate the configuration: %s'), + $inspection->getError() + )); + return false; + } + } + + $this->info($this->translate('The configuration has been successfully validated.')); + } + + return true; + } + /** * Add a checkbox to the form by which the user can skip the resource validation * diff --git a/modules/monitoring/library/Monitoring/MonitoringWizard.php b/modules/monitoring/library/Monitoring/MonitoringWizard.php index b91129d10..0a224610c 100644 --- a/modules/monitoring/library/Monitoring/MonitoringWizard.php +++ b/modules/monitoring/library/Monitoring/MonitoringWizard.php @@ -114,6 +114,19 @@ class MonitoringWizard extends Wizard implements SetupWizard mt('monitoring', 'Setup the monitoring module for Icinga Web 2', 'setup.summary.btn.finish') ); } + + if ($page->getName() === 'setup_monitoring_ido') { + $page->addElement( + 'submit', + 'backend_validation', + array( + 'ignore' => true, + 'label' => t('Validate Configuration'), + 'decorators' => array('ViewHelper') + ) + ); + $page->getDisplayGroup('buttons')->addElement($page->getElement('backend_validation')); + } } /** diff --git a/modules/setup/application/forms/AuthBackendPage.php b/modules/setup/application/forms/AuthBackendPage.php index 4eb4ca504..4c37a2967 100644 --- a/modules/setup/application/forms/AuthBackendPage.php +++ b/modules/setup/application/forms/AuthBackendPage.php @@ -30,6 +30,7 @@ class AuthBackendPage extends Form { $this->setName('setup_authentication_backend'); $this->setTitle($this->translate('Authentication Backend', 'setup.page.title')); + $this->setValidatePartial(true); } /** @@ -150,6 +151,54 @@ class AuthBackendPage extends Form return true; } + /** + * Run the configured backend's inspection checks and show the result, if necessary + * + * This will only run any validation if the user pushed the 'backend_validation' button. + * + * @param array $formData + * + * @return bool + */ + public function isValidPartial(array $formData) + { + if (isset($formData['backend_validation']) && parent::isValid($formData)) { + $self = clone $this; + $self->getSubForm('backend_form')->getElement('resource')->setIgnore(false); + $inspection = UserBackendConfigForm::inspectUserBackend($self); + if ($inspection !== null) { + $join = function ($e) use (& $join) { + return is_string($e) ? $e : join("\n", array_map($join, $e)); + }; + $this->addElement( + 'note', + 'inspection_output', + array( + 'order' => 0, + 'value' => '' . $this->translate('Validation Log') . "\n\n" + . join("\n", array_map($join, $inspection->toArray())), + 'decorators' => array( + 'ViewHelper', + array('HtmlTag', array('tag' => 'pre', 'class' => 'log-output')), + ) + ) + ); + + if ($inspection->hasError()) { + $this->warning(sprintf( + $this->translate('Failed to successfully validate the configuration: %s'), + $inspection->getError() + )); + return false; + } + } + + $this->info($this->translate('The configuration has been successfully validated.')); + } + + return true; + } + /** * Add a checkbox to this form by which the user can skip the authentication validation */ diff --git a/modules/setup/application/forms/DbResourcePage.php b/modules/setup/application/forms/DbResourcePage.php index e9967f350..a0faf932d 100644 --- a/modules/setup/application/forms/DbResourcePage.php +++ b/modules/setup/application/forms/DbResourcePage.php @@ -23,6 +23,7 @@ class DbResourcePage extends Form 'Now please configure your database resource. Note that the database itself does not need to' . ' exist at this time as it is going to be created once the wizard is about to be finished.' )); + $this->setValidatePartial(true); } /** @@ -84,6 +85,35 @@ class DbResourcePage extends Form return true; } + /** + * Check whether it's possible to connect to the database server + * + * This will only run the check if the user pushed the 'backend_validation' button. + * + * @param array $formData + * + * @return bool + */ + public function isValidPartial(array $formData) + { + if (isset($formData['backend_validation']) && parent::isValid($formData)) { + try { + $db = new DbTool($this->getValues()); + $db->checkConnectivity(); + } catch (PDOException $e) { + $this->warning(sprintf( + $this->translate('Failed to successfully validate the configuration: %s'), + $e->getMessage() + )); + return false; + } + + $this->info($this->translate('The configuration has been successfully validated.')); + } + + return true; + } + /** * Add a checkbox to the form by which the user can skip the connection validation */ diff --git a/modules/setup/application/forms/LdapResourcePage.php b/modules/setup/application/forms/LdapResourcePage.php index 316b66f31..2c558dfbb 100644 --- a/modules/setup/application/forms/LdapResourcePage.php +++ b/modules/setup/application/forms/LdapResourcePage.php @@ -23,6 +23,7 @@ class LdapResourcePage extends Form 'Now please configure your AD/LDAP resource. This will later ' . 'be used to authenticate users logging in to Icinga Web 2.' )); + $this->setValidatePartial(true); } /** @@ -82,6 +83,52 @@ class LdapResourcePage extends Form return true; } + /** + * Run the configured backend's inspection checks and show the result, if necessary + * + * This will only run any validation if the user pushed the 'backend_validation' button. + * + * @param array $formData + * + * @return bool + */ + public function isValidPartial(array $formData) + { + if (isset($formData['backend_validation']) && parent::isValid($formData)) { + $inspection = ResourceConfigForm::inspectResource($this); + if ($inspection !== null) { + $join = function ($e) use (& $join) { + return is_string($e) ? $e : join("\n", array_map($join, $e)); + }; + $this->addElement( + 'note', + 'inspection_output', + array( + 'order' => 0, + 'value' => '' . $this->translate('Validation Log') . "\n\n" + . join("\n", array_map($join, $inspection->toArray())), + 'decorators' => array( + 'ViewHelper', + array('HtmlTag', array('tag' => 'pre', 'class' => 'log-output')), + ) + ) + ); + + if ($inspection->hasError()) { + $this->warning(sprintf( + $this->translate('Failed to successfully validate the configuration: %s'), + $inspection->getError() + )); + return false; + } + } + + $this->info($this->translate('The configuration has been successfully validated.')); + } + + return true; + } + /** * Add a checkbox to the form by which the user can skip the connection validation */ diff --git a/modules/setup/library/Setup/WebWizard.php b/modules/setup/library/Setup/WebWizard.php index 1addf6283..cd1df67f4 100644 --- a/modules/setup/library/Setup/WebWizard.php +++ b/modules/setup/library/Setup/WebWizard.php @@ -296,6 +296,27 @@ class WebWizard extends Wizard implements SetupWizard } elseif ($index === count($pages) - 1) { $page->getElement(static::BTN_NEXT)->setLabel(mt('setup', 'Setup Icinga Web 2', 'setup.summary.btn.finish')); } + + $authData = $this->getPageData('setup_authentication_type'); + $veto = $page->getName() === 'setup_authentication_backend' && $authData['type'] === 'db'; + if (! $veto && in_array($page->getName(), array( + 'setup_authentication_backend', + 'setup_auth_db_resource', + 'setup_config_db_resource', + 'setup_ldap_resource', + 'setup_monitoring_ido' + ))) { + $page->addElement( + 'submit', + 'backend_validation', + array( + 'ignore' => true, + 'label' => t('Validate Configuration'), + 'decorators' => array('ViewHelper') + ) + ); + $page->getDisplayGroup('buttons')->addElement($page->getElement('backend_validation')); + } } /**