From b7a389601d666dbcc7b86fffe06e6bbf28534339 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 19 Aug 2015 14:42:18 +0200 Subject: [PATCH] IdoResourcePage: Validate the version of a PostgreSQL server refs #9460 --- .../forms/Setup/IdoResourcePage.php | 101 ++++++++++-------- .../application/forms/DbResourcePage.php | 6 +- 2 files changed, 61 insertions(+), 46 deletions(-) diff --git a/modules/monitoring/application/forms/Setup/IdoResourcePage.php b/modules/monitoring/application/forms/Setup/IdoResourcePage.php index 2bb4de3a9..3e23d5125 100644 --- a/modules/monitoring/application/forms/Setup/IdoResourcePage.php +++ b/modules/monitoring/application/forms/Setup/IdoResourcePage.php @@ -8,6 +8,7 @@ use Icinga\Forms\Config\ResourceConfigForm; use Icinga\Forms\Config\Resource\DbResourceForm; use Icinga\Web\Form; use Icinga\Module\Monitoring\Forms\Config\BackendConfigForm; +use Icinga\Module\Setup\Utils\DbTool; class IdoResourcePage extends Form { @@ -73,23 +74,8 @@ class IdoResourcePage extends Form } if (! isset($formData['skip_validation']) || !$formData['skip_validation']) { - $inspection = ResourceConfigForm::inspectResource($this); - if ($inspection !== null && $inspection->hasError()) { - $this->error($inspection->getError()); - $this->addSkipValidationCheckbox($this->translate( - 'Check this to not to validate connectivity with the given database server.' - )); - return false; - } - - $configObject = new ConfigObject($this->getValues()); - if ( - ! BackendConfigForm::isValidIdoSchema($this, $configObject) - || !BackendConfigForm::isValidIdoInstance($this, $configObject) - ) { - $this->addSkipValidationCheckbox($this->translate( - 'Check this to not to validate the IDO schema in the given database.' - )); + if (! $this->validateConfiguration()) { + $this->addSkipValidationCheckbox(); return false; } } @@ -109,8 +95,31 @@ class IdoResourcePage extends Form public function isValidPartial(array $formData) { if (isset($formData['backend_validation']) && parent::isValid($formData)) { - $inspection = ResourceConfigForm::inspectResource($this); - if ($inspection !== null) { + if (! $this->validateConfiguration(true)) { + return false; + } + + $this->info($this->translate('The configuration has been successfully validated.')); + } elseif (! isset($formData['backend_validation'])) { + // This is usually done by isValid(Partial), but as we're not calling any of these... + $this->populate($formData); + } + + return true; + } + + /** + * Return whether the configuration is valid + * + * @param bool $showLog Whether to show the validation log + * + * @return bool + */ + protected function validateConfiguration($showLog = false) + { + $inspection = ResourceConfigForm::inspectResource($this); + if ($inspection !== null) { + if ($showLog) { $join = function ($e) use (& $join) { return is_string($e) ? $e : join("\n", array_map($join, $e)); }; @@ -127,45 +136,53 @@ class IdoResourcePage extends Form ) ) ); - - 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.')); - } elseif (! isset($formData['backend_validation'])) { - // This is usually done by isValid(Partial), but as we're not calling any of these... - $this->populate($formData); + if ($inspection->hasError()) { + $this->error(sprintf( + $this->translate('Failed to successfully validate the configuration: %s'), + $inspection->getError() + )); + return false; + } + } + + $configObject = new ConfigObject($this->getValues()); + if ( + ! BackendConfigForm::isValidIdoSchema($this, $configObject) + || !BackendConfigForm::isValidIdoInstance($this, $configObject) + ) { + return false; + } + + if ($this->getValue('db') === 'pgsql') { + $db = new DbTool($this->getValues()); + $version = $db->connectToDb()->getServerVersion(); + if (version_compare($version, '9.1', '<')) { + $this->error($this->translate(sprintf( + 'The server\'s version %s is too old. The minimum required version is %s.', + $version, + '9.1' + ))); + return false; + } } return true; } /** - * Add a checkbox to the form by which the user can skip the resource validation - * - * @param string $description + * Add a checkbox to the form by which the user can skip the configuration validation */ - protected function addSkipValidationCheckbox($description = null) + protected function addSkipValidationCheckbox() { - if (empty($description)) { - $description = $this->translate( - 'Proceed without any further (custom) validation.' - ); - } - $this->addElement( 'checkbox', 'skip_validation', array( 'required' => true, 'label' => $this->translate('Skip Validation'), - 'description' => $description + 'description' => $this->translate('Check this to not to validate the configuration') ) ); } diff --git a/modules/setup/application/forms/DbResourcePage.php b/modules/setup/application/forms/DbResourcePage.php index a9b8602c6..22b0576d6 100644 --- a/modules/setup/application/forms/DbResourcePage.php +++ b/modules/setup/application/forms/DbResourcePage.php @@ -151,7 +151,7 @@ class DbResourcePage extends Form } /** - * Add a checkbox to the form by which the user can skip the connection validation + * Add a checkbox to the form by which the user can skip the configuration validation */ protected function addSkipValidationCheckbox() { @@ -161,9 +161,7 @@ class DbResourcePage extends Form array( 'required' => true, 'label' => $this->translate('Skip Validation'), - 'description' => $this->translate( - 'Check this to not to validate connectivity with the given database server' - ) + 'description' => $this->translate('Check this to not to validate the configuration') ) ); }