Add Inspection API to DB backend

refs #9641
This commit is contained in:
Matthias Jentsch 2015-07-16 16:16:55 +02:00
parent 31618ce2cf
commit e357960d1e
2 changed files with 32 additions and 11 deletions

View File

@ -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();
}
/**

View File

@ -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;
}
}