Add logic validation for database authentication backends

refs #4546
This commit is contained in:
Jannis Moßhammer 2013-08-26 17:23:31 +02:00
parent 17e6402aa9
commit 4e41ce5cdc
5 changed files with 40 additions and 7 deletions

View File

@ -33,6 +33,7 @@ use \Zend_Config;
use \Icinga\Application\Config as IcingaConfig;
use \Icinga\Application\Icinga;
use \Icinga\Application\Logger;
use \Icinga\Web\Form\Decorator\HelpText;
use \Icinga\Application\DbAdapterFactory;
use \Icinga\Web\Form;

View File

@ -29,7 +29,7 @@
namespace Icinga\Form\Config\Authentication;
use Icinga\Authentication\Backend\DbUserBackend;
use \Icinga\Authentication\Backend\DbUserBackend;
use \Zend_Config;
use \Icinga\Application\Config as IcingaConfig;
use \Icinga\Application\Icinga;
@ -122,6 +122,23 @@ class DbBackendForm extends BaseBackendForm
public function validateAuthenticationBackend()
{
try {
$name = $this->getBackendName();
$db = DbAdapterFactory::getDbAdapter(
$this->getValue('backend_' . $this->filterName($name) . '_' . 'resource')
);
$dbBackend = new DbUserBackend($db);
if ($dbBackend->getUserCount() < 1) {
$this->addErrorMessage("No users found under the specified database backend");
return false;
}
} catch (\Exception $e) {
$this->addErrorMessage("Using the specified backend failed: " . $e->getMessage());
return false;
} catch (\Zend_Db_Statement_Exception $e) {
$this->addErrorMessage("Using the specified backend failed: " . $e->getMessage());
return false;
}
return true;
}
}

View File

@ -34,7 +34,6 @@ use \Zend_Config;
use \Icinga\Application\Config as IcingaConfig;
use \Icinga\Application\Icinga;
use \Icinga\Application\Logger;
use \Icinga\Web\Form\Decorator\HelpText;
use \Icinga\Application\DbAdapterFactory;
use \Icinga\Web\Form;

View File

@ -29,7 +29,7 @@
namespace Icinga\Authentication\Backend;
use Zend_Db;
use \Zend_Db;
use \Icinga\User;
use \Icinga\Authentication\UserBackend;
use \Icinga\Authentication\Credentials;
@ -81,9 +81,7 @@ class DbUserBackend implements UserBackend
{
$this->db = $database;
/*
* Test if the connection is available
*/
// Test if the connection is available
$this->db->getConnection();
}
@ -207,4 +205,12 @@ class DbUserBackend implements UserBackend
);
return $usr;
}
public function getUserCount()
{
$this->db->getConnection();
$query = $this->db->select()->from($this->userTable, 'COUNT(*) as count')->query();
return $query->fetch()->count;
}
}

View File

@ -31,13 +31,15 @@ namespace Icinga\Authentication;
interface UserBackend
{
/**
* Creates a new object
* Create a userbackend from the given configuration or resource
*
* @param $config
*/
public function __construct($config);
/**
* Test if the username exists
*
* @param Credentials $credentials
* @return boolean
*/
@ -45,8 +47,16 @@ interface UserBackend
/**
* Authenticate
*
* @param Credentials $credentials
* @return User
*/
public function authenticate(Credentials $credentials);
/**
* Get the number of users available through this backend
*
* @return int
*/
public function getUserCount();
}