Fix authentication chain

refs #4593
This commit is contained in:
Marius Hein 2013-08-30 12:17:12 +02:00
parent 845f181001
commit e2e744a424
2 changed files with 32 additions and 31 deletions

View File

@ -33,7 +33,6 @@ use \stdClass;
use \Zend_Config; use \Zend_Config;
use \Zend_Db; use \Zend_Db;
use \Zend_Db_Adapter_Abstract; use \Zend_Db_Adapter_Abstract;
use \Zend_Db_Statement_Exception;
use \Icinga\Application\DbAdapterFactory; use \Icinga\Application\DbAdapterFactory;
use \Icinga\Exception\ProgrammingError; use \Icinga\Exception\ProgrammingError;
use \Icinga\User; use \Icinga\User;
@ -86,7 +85,7 @@ class DbUserBackend implements UserBackend
* *
* @var Zend_Db * @var Zend_Db
*/ */
private $db = null; private $db;
/** /**
* The name of the user table * The name of the user table
@ -142,10 +141,6 @@ class DbUserBackend implements UserBackend
*/ */
public function hasUsername(Credential $credential) public function hasUsername(Credential $credential)
{ {
if ($this->db === null) {
Logger::warn('Ignoring hasUsername in database as no connection is available');
return false;
}
$user = $this->getUserByName($credential->getUsername()); $user = $this->getUserByName($credential->getUsername());
return isset($user); return isset($user);
} }
@ -159,11 +154,8 @@ class DbUserBackend implements UserBackend
*/ */
public function authenticate(Credential $credential) public function authenticate(Credential $credential)
{ {
if ($this->db === null) { $this->assertDbConnection();
Logger::warn('Ignoring database authentication as no connection is available');
return null;
}
$this->db->getConnection();
try { try {
$salt = $this->getUserSalt($credential->getUsername()); $salt = $this->getUserSalt($credential->getUsername());
} catch (Exception $e) { } catch (Exception $e) {
@ -204,7 +196,8 @@ class DbUserBackend implements UserBackend
*/ */
private function getUserSalt($username) private function getUserSalt($username)
{ {
$this->db->getConnection(); $this->assertDbConnection();
$res = $this->db->select() $res = $this->db->select()
->from($this->userTable, self::SALT_COLUMN) ->from($this->userTable, self::SALT_COLUMN)
->where(self::USER_NAME_COLUMN.' = ?', $username) ->where(self::USER_NAME_COLUMN.' = ?', $username)
@ -225,11 +218,9 @@ class DbUserBackend implements UserBackend
*/ */
private function getUserByName($username) private function getUserByName($username)
{ {
if ($this->db === null) {
Logger::warn('Ignoring getUserByName as no database connection is available'); $this->assertDbConnection();
return null;
}
try {
$this->db->getConnection(); $this->db->getConnection();
$res = $this->db-> $res = $this->db->
select()->from($this->userTable) select()->from($this->userTable)
@ -240,10 +231,7 @@ class DbUserBackend implements UserBackend
return $this->createUserFromResult($res); return $this->createUserFromResult($res);
} }
return null; return null;
} catch (Zend_Db_Statement_Exception $exc) {
Logger::error('Could not fetch users from db : %s ', $exc->getMessage());
return null;
}
} }
/** /**
@ -261,6 +249,19 @@ class DbUserBackend implements UserBackend
return $usr; return $usr;
} }
/**
* Assert a valid database connection
*
* @throws ConfigurationError
*/
private function assertDbConnection()
{
if ($this->db === null) {
$msg = 'DbUserBackend ' . $this->getName() . ' has no valid database connection.';
Logger::fatal($msg);
throw new ConfigurationError($msg);
}
}
/** /**
* Return the number of users in this database connection * Return the number of users in this database connection

View File

@ -311,7 +311,7 @@ class Manager
} }
} }
if (count($this->userBackends) === $authErrors) { if ($authErrors >= count($this->userBackends)) {
Logger::fatal('AuthManager: No working backend found, unable to authenticate any user'); Logger::fatal('AuthManager: No working backend found, unable to authenticate any user');
throw new ConfigurationError( throw new ConfigurationError(
'No working backend found. Unable to authenticate any user.' 'No working backend found. Unable to authenticate any user.'