From e357960d1ed74bb29227377c2804a6024f3d78bf Mon Sep 17 00:00:00 2001 From: Matthias Jentsch Date: Thu, 16 Jul 2015 16:16:55 +0200 Subject: [PATCH] Add Inspection API to DB backend refs #9641 --- .../Config/UserBackend/DbBackendForm.php | 17 +++++------- .../Authentication/User/DbUserBackend.php | 26 ++++++++++++++++++- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/application/forms/Config/UserBackend/DbBackendForm.php b/application/forms/Config/UserBackend/DbBackendForm.php index f096f3e35..c09ec166c 100644 --- a/application/forms/Config/UserBackend/DbBackendForm.php +++ b/application/forms/Config/UserBackend/DbBackendForm.php @@ -105,18 +105,15 @@ class DbBackendForm extends Form */ public static function isValidUserBackend(Form $form) { - try { - $dbUserBackend = new DbUserBackend(ResourceFactory::createResource($form->getResourceConfig())); - if ($dbUserBackend->select()->where('is_active', true)->count() < 1) { - $form->addError($form->translate('No active users found under the specified database backend')); - return false; - } - } catch (Exception $e) { - $form->addError(sprintf($form->translate('Using the specified backend failed: %s'), $e->getMessage())); - return false; + $backend = new DbUserBackend(ResourceFactory::createResource($form->getResourceConfig())); + $result = $backend->inspect(); + if ($result->hasError()) { + $form->addError(sprintf($form->translate('Using the specified backend failed: %s'), $result->getError())); } - return true; + // TODO: display diagnostics in $result->toArray() to the user + + return ! $result->hasError(); } /** diff --git a/library/Icinga/Authentication/User/DbUserBackend.php b/library/Icinga/Authentication/User/DbUserBackend.php index 0e5dad5fa..3c28a2fdd 100644 --- a/library/Icinga/Authentication/User/DbUserBackend.php +++ b/library/Icinga/Authentication/User/DbUserBackend.php @@ -4,13 +4,15 @@ namespace Icinga\Authentication\User; use Exception; +use Icinga\Data\Inspectable; +use Icinga\Data\Inspection; use PDO; use Icinga\Data\Filter\Filter; use Icinga\Exception\AuthenticationException; use Icinga\Repository\DbRepository; use Icinga\User; -class DbUserBackend extends DbRepository implements UserBackendInterface +class DbUserBackend extends DbRepository implements UserBackendInterface, Inspectable { /** * The algorithm to use when hashing passwords @@ -246,4 +248,26 @@ class DbUserBackend extends DbRepository implements UserBackendInterface { return crypt($password, self::HASH_ALGORITHM . ($salt !== null ? $salt : $this->generateSalt())); } + + /** + * Inspect this object to gain extended information about its health + * + * @return Inspection The inspection result + */ + public function inspect() + { + $insp = new Inspection('Db User Backend'); + $insp->write($this->ds->inspect()); + try { + $users = $this->select()->where('is_active', true)->count(); + if ($users > 1) { + $insp->write(sprintf('%s active users', $users)); + } else { + return $insp->error('0 active users', $users); + } + } catch (Exception $e) { + $insp->error(sprintf('Query failed: %s', $e->getMessage())); + } + return $insp; + } }