diff --git a/application/forms/Config/Authentication/BaseBackendForm.php b/application/forms/Config/Authentication/BaseBackendForm.php index 2b913d9d9..da04011fd 100644 --- a/application/forms/Config/Authentication/BaseBackendForm.php +++ b/application/forms/Config/Authentication/BaseBackendForm.php @@ -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; diff --git a/application/forms/Config/Authentication/DbBackendForm.php b/application/forms/Config/Authentication/DbBackendForm.php index 6bb79eecb..aa7d555a8 100644 --- a/application/forms/Config/Authentication/DbBackendForm.php +++ b/application/forms/Config/Authentication/DbBackendForm.php @@ -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; } } diff --git a/application/forms/Config/Authentication/LdapBackendForm.php b/application/forms/Config/Authentication/LdapBackendForm.php index 58acb995c..52705ca7a 100644 --- a/application/forms/Config/Authentication/LdapBackendForm.php +++ b/application/forms/Config/Authentication/LdapBackendForm.php @@ -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; diff --git a/library/Icinga/Authentication/Backend/DbUserBackend.php b/library/Icinga/Authentication/Backend/DbUserBackend.php index f75e694b1..0a9a6dbe6 100644 --- a/library/Icinga/Authentication/Backend/DbUserBackend.php +++ b/library/Icinga/Authentication/Backend/DbUserBackend.php @@ -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; + } } diff --git a/library/Icinga/Authentication/UserBackend.php b/library/Icinga/Authentication/UserBackend.php index ddebb622c..145f8ac36 100644 --- a/library/Icinga/Authentication/UserBackend.php +++ b/library/Icinga/Authentication/UserBackend.php @@ -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(); }