Merge branch 'bugfix/db-user-backend-not-working-4596'
fixes #4577 fixes #4578 fixes #4596 fixes #4599 fixes #4627
This commit is contained in:
commit
1b63f24b7b
|
@ -482,13 +482,13 @@ exec { 'populate-icingweba-pgsql-db-accounts':
|
|||
}
|
||||
|
||||
exec { 'populate-icingaweb-mysql-db-preferences':
|
||||
unless => 'mysql -uicingaweb -picinga icingaweb -e "SELECT * FROM preferences;" &> /dev/null',
|
||||
unless => 'mysql -uicingaweb -picinga icingaweb -e "SELECT * FROM preference;" &> /dev/null',
|
||||
command => 'mysql -uicingaweb -picinga icingaweb < /vagrant/etc/schema/preferences.mysql.sql',
|
||||
require => [ Exec['create-mysql-icingaweb-db'] ]
|
||||
}
|
||||
|
||||
exec { 'populate-icingweba-pgsql-db-preferences':
|
||||
unless => 'psql -U icingaweb -d icingaweb -c "SELECT * FROM preferences;" &> /dev/null',
|
||||
unless => 'psql -U icingaweb -d icingaweb -c "SELECT * FROM preference;" &> /dev/null',
|
||||
command => 'sudo -u postgres psql -U icingaweb -d icingaweb -f /vagrant/etc/schema/preferences.pgsql.sql',
|
||||
require => [ Exec['create-pgsql-icingaweb-db'] ]
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
create table account (
|
||||
`username` varchar(255) COLLATE latin1_general_ci NOT NULL,
|
||||
`salt` varchar(255) NOT NULL,
|
||||
`password` varchar(255) NOT NULL,
|
||||
`active` tinyint(1) DEFAULT NULL,
|
||||
PRIMARY KEY (`username`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
|
||||
|
||||
/*
|
||||
* user: icingaadmin
|
||||
* password: icinga
|
||||
*/
|
||||
INSERT INTO account (
|
||||
`username`,
|
||||
`salt`,
|
||||
`password`,
|
||||
`active`
|
||||
)
|
||||
VALUES (
|
||||
'icingaadmin',
|
||||
'IepKgTTShC',
|
||||
'52deddb5cc7a5769484fcb0fbc5981a7c62cd9f3ddbb8ff3ddb1b89ea324ad16',
|
||||
1
|
||||
);
|
|
@ -0,0 +1,28 @@
|
|||
create table "account" (
|
||||
"username" character varying(255) NOT NULL,
|
||||
"salt" character varying(255),
|
||||
"password" character varying(255) NOT NULL,
|
||||
"active" boolean
|
||||
);
|
||||
|
||||
ALTER TABLE ONLY "account"
|
||||
ADD CONSTRAINT account_pkey PRIMARY KEY ("username");
|
||||
|
||||
CREATE UNIQUE INDEX username_lower_unique_idx ON "account" USING btree (lower((username)::text));
|
||||
|
||||
/*
|
||||
* user: icingaadmin
|
||||
* password: icinga
|
||||
*/
|
||||
INSERT INTO "account" (
|
||||
"username",
|
||||
"salt",
|
||||
"password",
|
||||
"active"
|
||||
)
|
||||
VALUES (
|
||||
'icingaadmin',
|
||||
'IepKgTTShC',
|
||||
'52deddb5cc7a5769484fcb0fbc5981a7c62cd9f3ddbb8ff3ddb1b89ea324ad16',
|
||||
true
|
||||
);
|
|
@ -1,7 +1,6 @@
|
|||
create table `preferences`(
|
||||
`username` VARCHAR(255) NOT NULL,
|
||||
`preference` VARCHAR(100) NOT NULL,
|
||||
create table `preference`(
|
||||
`username` VARCHAR(255) COLLATE latin1_general_ci NOT NULL,
|
||||
`key` VARCHAR(100) COLLATE latin1_general_ci NOT NULL,
|
||||
`value` VARCHAR(255) NOT NULL,
|
||||
|
||||
PRIMARY KEY(username, preference)
|
||||
);
|
||||
PRIMARY KEY (`username`, `key`)
|
||||
) ENGINE=InnoDB;
|
|
@ -1,7 +1,10 @@
|
|||
create table "preferences"(
|
||||
create table "preference"(
|
||||
"username" VARCHAR(255) NOT NULL,
|
||||
"preference" VARCHAR(100) NOT NULL,
|
||||
"value" VARCHAR(255) NOT NULL,
|
||||
"key" VARCHAR(100) NOT NULL,
|
||||
"value" VARCHAR(255) NOT NULL
|
||||
);
|
||||
|
||||
PRIMARY KEY(username, preference)
|
||||
);
|
||||
ALTER TABLE ONLY "preference"
|
||||
ADD CONSTRAINT preference_pkey PRIMARY KEY ("username", "key");
|
||||
|
||||
CREATE UNIQUE INDEX username_and_key_lower_unique_idx ON "preference" USING btree (lower((username)::text), lower((key)::text));
|
|
@ -1,28 +0,0 @@
|
|||
create table account (
|
||||
user_name varchar(255) NOT NULL,
|
||||
first_name varchar(255),
|
||||
last_name varchar(255),
|
||||
email varchar(255),
|
||||
domain varchar(255),
|
||||
last_login timestamp,
|
||||
salt varchar(255),
|
||||
password varchar(255) NOT NULL,
|
||||
active BOOL,
|
||||
PRIMARY KEY (user_name)
|
||||
);
|
||||
|
||||
/*
|
||||
* user: icingaadmin
|
||||
* password: icinga
|
||||
*/
|
||||
INSERT INTO account (
|
||||
user_name,
|
||||
salt,
|
||||
password,
|
||||
active)
|
||||
VALUES (
|
||||
'icingaadmin',
|
||||
'IepKgTTShC',
|
||||
'52deddb5cc7a5769484fcb0fbc5981a7c62cd9f3ddbb8ff3ddb1b89ea324ad16',
|
||||
true
|
||||
);
|
|
@ -1,28 +0,0 @@
|
|||
create table "account" (
|
||||
user_name varchar(255) NOT NULL,
|
||||
first_name varchar(255),
|
||||
last_name varchar(255),
|
||||
email varchar(255),
|
||||
domain varchar(255),
|
||||
last_login timestamp,
|
||||
salt varchar(255),
|
||||
password varchar(255) NOT NULL,
|
||||
active BOOL,
|
||||
PRIMARY KEY (user_name)
|
||||
);
|
||||
|
||||
/*
|
||||
* user: icingaadmin
|
||||
* password: icinga
|
||||
*/
|
||||
INSERT INTO "account" (
|
||||
user_name,
|
||||
salt,
|
||||
password,
|
||||
active)
|
||||
VALUES (
|
||||
'icingaadmin',
|
||||
'IepKgTTShC',
|
||||
'52deddb5cc7a5769484fcb0fbc5981a7c62cd9f3ddbb8ff3ddb1b89ea324ad16',
|
||||
true
|
||||
);
|
|
@ -54,7 +54,7 @@ class DbAdapterFactory implements ConfigAwareFactory
|
|||
*
|
||||
* @var String
|
||||
*/
|
||||
private static $factoryClass;
|
||||
private static $factoryClass = 'Zend_Db';
|
||||
|
||||
/**
|
||||
* Resource cache to allow multiple use
|
||||
|
@ -115,7 +115,7 @@ class DbAdapterFactory implements ConfigAwareFactory
|
|||
public static function resetConfig()
|
||||
{
|
||||
self::$resources = null;
|
||||
self::$factoryClass = null;
|
||||
self::$factoryClass = 'Zend_Db';
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -150,11 +150,11 @@ class DbAdapterFactory implements ConfigAwareFactory
|
|||
/**
|
||||
* Get the resource with the given $identifier
|
||||
*
|
||||
* @throws ConfigurationError
|
||||
* @throws ProgrammingError
|
||||
* @param string $identifier The name of the resource
|
||||
*
|
||||
* @return Zend_Db_Adapter_Abstract
|
||||
* @throws ConfigurationError
|
||||
* @throws ProgrammingError
|
||||
*/
|
||||
public static function getDbAdapter($identifier)
|
||||
{
|
||||
|
@ -180,6 +180,19 @@ class DbAdapterFactory implements ConfigAwareFactory
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a db adapter directly from a configuration instead of a resource identifier
|
||||
*
|
||||
* @param Zend_Config $config The resource configuration that will be used.
|
||||
*
|
||||
* @return Zend_Db_Adapter_Abstract The created DbAdapter
|
||||
* @throws ProgrammingError
|
||||
*/
|
||||
public static function createDbAdapterFromConfig(Zend_Config $config)
|
||||
{
|
||||
return self::createDbAdapter($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the Db_Adapter for the given configuration section
|
||||
*
|
||||
|
@ -198,12 +211,12 @@ class DbAdapterFactory implements ConfigAwareFactory
|
|||
throw new ConfigurationError($msg);
|
||||
}
|
||||
$options = array(
|
||||
'dbname' => $config->dbname,
|
||||
'host' => $config->host,
|
||||
'username' => $config->username,
|
||||
'password' => $config->password,
|
||||
'options' => self::$defaultZendDbAdapterOptions,
|
||||
'driver_options' => self::$defaultPdoDriverOptions
|
||||
'dbname' => $config->dbname,
|
||||
'host' => $config->host,
|
||||
'username' => $config->username,
|
||||
'password' => $config->password,
|
||||
'options' => self::$defaultZendDbAdapterOptions,
|
||||
'driver_options' => self::$defaultPdoDriverOptions
|
||||
);
|
||||
switch ($config->db) {
|
||||
case 'mysql':
|
||||
|
@ -225,21 +238,18 @@ class DbAdapterFactory implements ConfigAwareFactory
|
|||
* Call the currently set factory class
|
||||
*
|
||||
* @param string $adapter The name of the used db adapter
|
||||
* @param array $options An array or Zend_Config object with adapter
|
||||
* @param mixed $options An array or Zend_Config object with adapter
|
||||
* parameters
|
||||
*
|
||||
* @return Zend_Db_Adapter_Abstract The created adapter
|
||||
*/
|
||||
private static function callFactory($adapter, array $options)
|
||||
private static function callFactory($adapter, $options)
|
||||
{
|
||||
$factory = self::$factoryClass;
|
||||
|
||||
$optionModifierCallback = __CLASS__. '::get'. ucfirst(str_replace('_', '', $adapter)). 'Options';
|
||||
|
||||
$optionModifierCallback = __CLASS__ . '::get' . ucfirst(str_replace('_', '', $adapter)) . 'Options';
|
||||
if (is_callable($optionModifierCallback)) {
|
||||
$options = call_user_func($optionModifierCallback, $options);
|
||||
}
|
||||
|
||||
return $factory::factory($adapter, $options);
|
||||
}
|
||||
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
|
||||
namespace Icinga\Authentication\Backend;
|
||||
|
||||
use Zend_Db;
|
||||
use \Icinga\User;
|
||||
use \Icinga\Authentication\UserBackend;
|
||||
use \Icinga\Authentication\Credentials;
|
||||
|
@ -49,13 +50,7 @@ class DbUserBackend implements UserBackend
|
|||
* Mapping of all table column names
|
||||
*/
|
||||
|
||||
const USER_NAME_COLUMN = 'user_name';
|
||||
|
||||
const FIRST_NAME_COLUMN = 'first_name';
|
||||
|
||||
const LAST_NAME_COLUMN = 'last_name';
|
||||
|
||||
const LAST_LOGIN_COLUMN = 'last_login';
|
||||
const USER_NAME_COLUMN = 'username';
|
||||
|
||||
const SALT_COLUMN = 'salt';
|
||||
|
||||
|
@ -63,10 +58,6 @@ class DbUserBackend implements UserBackend
|
|||
|
||||
const ACTIVE_COLUMN = 'active';
|
||||
|
||||
const DOMAIN_COLUMN = 'domain';
|
||||
|
||||
const EMAIL_COLUMN = 'email';
|
||||
|
||||
/**
|
||||
* The database connection that will be used for fetching users
|
||||
*
|
||||
|
@ -80,6 +71,7 @@ class DbUserBackend implements UserBackend
|
|||
* @var String
|
||||
*/
|
||||
private $userTable = "account";
|
||||
|
||||
/**
|
||||
* Create a DbUserBackend
|
||||
*
|
||||
|
@ -98,9 +90,9 @@ class DbUserBackend implements UserBackend
|
|||
/**
|
||||
* Check if the user identified by the given credentials is available
|
||||
*
|
||||
* @param Credentials $credentials The login credentials
|
||||
* @param Credentials $credentials The login credentials
|
||||
*
|
||||
* @return boolean True when the username is known and currently active.
|
||||
* @return boolean True when the username is known and currently active.
|
||||
*/
|
||||
public function hasUsername(Credentials $credential)
|
||||
{
|
||||
|
@ -109,15 +101,15 @@ class DbUserBackend implements UserBackend
|
|||
return false;
|
||||
}
|
||||
$user = $this->getUserByName($credential->getUsername());
|
||||
return !empty($user);
|
||||
return isset($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticate a user with the given credentials
|
||||
*
|
||||
* @param Credentials $credentials The login credentials
|
||||
* @param Credentials $credentials The login credentials
|
||||
*
|
||||
* @return User|null The authenticated user or Null.
|
||||
* @return User|null The authenticated user or Null.
|
||||
*/
|
||||
public function authenticate(Credentials $credential)
|
||||
{
|
||||
|
@ -126,6 +118,12 @@ class DbUserBackend implements UserBackend
|
|||
return null;
|
||||
}
|
||||
$this->db->getConnection();
|
||||
try {
|
||||
$salt = $this->getUserSalt($credential->getUsername());
|
||||
} catch (\Exception $e) {
|
||||
Logger::error($e->getMessage());
|
||||
return null;
|
||||
}
|
||||
$res = $this->db
|
||||
->select()->from($this->userTable)
|
||||
->where(self::USER_NAME_COLUMN.' = ?', $credential->getUsername())
|
||||
|
@ -134,41 +132,22 @@ class DbUserBackend implements UserBackend
|
|||
self::PASSWORD_COLUMN. ' = ?',
|
||||
hash_hmac(
|
||||
'sha256',
|
||||
$this->getUserSalt($credential->getUsername()),
|
||||
$salt,
|
||||
$credential->getPassword()
|
||||
)
|
||||
)
|
||||
->query()->fetch();
|
||||
if (!empty($res)) {
|
||||
$this->updateLastLogin($credential->getUsername());
|
||||
if ($res !== false) {
|
||||
return $this->createUserFromResult($res);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the timestamp containing the time of the last login for
|
||||
* the user with the given username
|
||||
*
|
||||
* @param $username The login-name of the user.
|
||||
*/
|
||||
private function updateLastLogin($username)
|
||||
{
|
||||
$this->db->getConnection();
|
||||
$this->db->update(
|
||||
$this->userTable,
|
||||
array(
|
||||
self::LAST_LOGIN_COLUMN => new \Zend_Db_Expr('NOW()')
|
||||
),
|
||||
self::USER_NAME_COLUMN.' = '.$this->db->quoteInto('?', $username)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the users salt from the database
|
||||
*
|
||||
* @param $username The user whose salt should be fetched.
|
||||
* @param $username The user whose salt should be fetched.
|
||||
*
|
||||
* @return String|null Returns the salt-string or Null, when the user does not exist.
|
||||
* @return String|null Returns the salt-string or Null, when the user does not exist.
|
||||
*/
|
||||
private function getUserSalt($username)
|
||||
{
|
||||
|
@ -177,15 +156,19 @@ class DbUserBackend implements UserBackend
|
|||
->from($this->userTable, self::SALT_COLUMN)
|
||||
->where(self::USER_NAME_COLUMN.' = ?', $username)
|
||||
->query()->fetch();
|
||||
return $res[self::SALT_COLUMN];
|
||||
if ($res !== false) {
|
||||
return $res->{self::SALT_COLUMN};
|
||||
} else {
|
||||
throw new \Exception('No Salt found for user "' . $username . '"');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the user information from the database
|
||||
*
|
||||
* @param $username The name of the user.
|
||||
* @param $username The name of the user.
|
||||
*
|
||||
* @return User|null Returns the user object, or null when the user does not exist.
|
||||
* @return User|null Returns the user object, or null when the user does not exist.
|
||||
*/
|
||||
private function getUserByName($username)
|
||||
{
|
||||
|
@ -200,10 +183,10 @@ class DbUserBackend implements UserBackend
|
|||
->where(self::USER_NAME_COLUMN.' = ?', $username)
|
||||
->where(self::ACTIVE_COLUMN.' = ?', true)
|
||||
->query()->fetch();
|
||||
if (empty($res)) {
|
||||
return null;
|
||||
if ($res !== false) {
|
||||
return $this->createUserFromResult($res);
|
||||
}
|
||||
return $this->createUserFromResult($res);
|
||||
return null;
|
||||
} catch (\Zend_Db_Statement_Exception $exc) {
|
||||
Logger::error("Could not fetch users from db : %s ", $exc->getMessage());
|
||||
return null;
|
||||
|
@ -213,19 +196,15 @@ class DbUserBackend implements UserBackend
|
|||
/**
|
||||
* Create a new instance of User from a query result
|
||||
*
|
||||
* @param array $result The query result-array containing the column
|
||||
* @param $result The query result containing the user row
|
||||
*
|
||||
* @return User The created instance of User.
|
||||
* @return User The created instance of User.
|
||||
*/
|
||||
private function createUserFromResult(Array $result)
|
||||
private function createUserFromResult($resultRow)
|
||||
{
|
||||
$usr = new User(
|
||||
$result[self::USER_NAME_COLUMN],
|
||||
$result[self::FIRST_NAME_COLUMN],
|
||||
$result[self::LAST_NAME_COLUMN],
|
||||
$result[self::EMAIL_COLUMN]
|
||||
$resultRow->{self::USER_NAME_COLUMN}
|
||||
);
|
||||
$usr->setDomain($result[self::DOMAIN_COLUMN]);
|
||||
return $usr;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -159,20 +159,20 @@ class Manager
|
|||
*/
|
||||
private function initBestBackend($target, $backends)
|
||||
{
|
||||
foreach ($backends as $backend) {
|
||||
foreach ($backends as $key => $backend) {
|
||||
if (strtolower($target) === strtolower($backend->target)) {
|
||||
$db = $this->tryToInitBackend($target, $backend);
|
||||
if (isset($db)) {
|
||||
break;
|
||||
return $db;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isset($db)) {
|
||||
$msg = 'Failed to create any authentication backend, login will not be possible.';
|
||||
Logger::error($msg);
|
||||
return null;
|
||||
}
|
||||
return $db;
|
||||
Logger::error(
|
||||
'Failed to create any authentication backend '
|
||||
. 'for the target "' . $target . '". Entities belonging to this target'
|
||||
. ' will not be able to authenticate.'
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -188,6 +188,7 @@ class Manager
|
|||
{
|
||||
$type = ucwords(strtolower($backendConfig->backend));
|
||||
if (!$type) {
|
||||
Logger::warn('Backend has no type configured. (e.g. backend=ldap)');
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
|
@ -199,9 +200,7 @@ class Manager
|
|||
$class = '\\Icinga\\Authentication\\Backend\\' . $type . $target. 'Backend';
|
||||
return new $class($resource);
|
||||
} catch (\Exception $e) {
|
||||
$msg = 'Not able to create backend: ' .
|
||||
print_r($backendConfig->backend, true)
|
||||
. '. Exception: ' . $e->getMessage();
|
||||
$msg = 'Not able to create backend. Exception: ' . $e->getMessage();
|
||||
Logger::warn($msg);
|
||||
return null;
|
||||
}
|
||||
|
@ -210,12 +209,13 @@ class Manager
|
|||
/**
|
||||
* Try to authenticate the current user with the Credentials (@see Credentials).
|
||||
*
|
||||
* @param Credentials $credentials The credentials to use for authentication
|
||||
* @param Boolean $persist Whether to persist the authentication result
|
||||
* in the current session
|
||||
* @param Credentials $credentials The credentials to use for authentication
|
||||
* @param Boolean $persist Whether to persist the authentication result
|
||||
* in the current session
|
||||
*
|
||||
* @return Boolean true on success, otherwise false
|
||||
**/
|
||||
* @return Boolean true on success, otherwise false
|
||||
* @throws ConfigError
|
||||
*/
|
||||
public function authenticate(Credentials $credentials, $persist = true)
|
||||
{
|
||||
if (!$this->userBackend) {
|
||||
|
|
|
@ -47,7 +47,7 @@ class DbStore implements LoadInterface, FlushObserverInterface
|
|||
/**
|
||||
* Column name for preference
|
||||
*/
|
||||
const COLUMN_PREFERENCE = 'preference';
|
||||
const COLUMN_PREFERENCE = 'key';
|
||||
|
||||
/**
|
||||
* Column name for value
|
||||
|
@ -66,14 +66,14 @@ class DbStore implements LoadInterface, FlushObserverInterface
|
|||
*
|
||||
* @var Zend_Db_Adapter_Abstract
|
||||
*/
|
||||
private $dbAdapter;
|
||||
private $db;
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $table = 'preferences';
|
||||
private $table = 'preference';
|
||||
|
||||
/**
|
||||
* Setter for user
|
||||
|
@ -88,12 +88,11 @@ class DbStore implements LoadInterface, FlushObserverInterface
|
|||
/**
|
||||
* Setter for db adapter
|
||||
*
|
||||
* @param Zend_Db_Adapter_Abstract $dbAdapter
|
||||
* @param Zend_Db_Adapter_Abstract $db
|
||||
*/
|
||||
public function setDbAdapter(Zend_Db_Adapter_Abstract $dbAdapter)
|
||||
public function setDbAdapter(Zend_Db_Adapter_Abstract $db)
|
||||
{
|
||||
$this->dbAdapter = $dbAdapter;
|
||||
$this->dbAdapter->getProfiler()->setEnabled(true);
|
||||
$this->db = $db;
|
||||
|
||||
}
|
||||
|
||||
|
@ -114,14 +113,14 @@ class DbStore implements LoadInterface, FlushObserverInterface
|
|||
*/
|
||||
public function load()
|
||||
{
|
||||
$res = $this->dbAdapter->select()->from($this->table)
|
||||
$res = $this->db->select()->from($this->table)
|
||||
->where('username=?', $this->user->getUsername())
|
||||
->query();
|
||||
|
||||
$out = array();
|
||||
|
||||
foreach ($res->fetchAll() as $row) {
|
||||
$out[$row[self::COLUMN_PREFERENCE]] = $row[self::COLUMN_VALUE];
|
||||
$out[$row->{self::COLUMN_PREFERENCE}] = $row->{self::COLUMN_VALUE};
|
||||
}
|
||||
|
||||
return $out;
|
||||
|
@ -136,8 +135,8 @@ class DbStore implements LoadInterface, FlushObserverInterface
|
|||
private function createWhereCondition($preference)
|
||||
{
|
||||
return array(
|
||||
self::COLUMN_USERNAME. '=?' => $this->user->getUsername(),
|
||||
self::COLUMN_PREFERENCE. '=?' => $preference
|
||||
$this->db->quoteIdentifier(self::COLUMN_USERNAME) . '=?' => $this->user->getUsername(),
|
||||
$this->db->quoteIdentifier(self::COLUMN_PREFERENCE) . '=?' => $preference
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -150,12 +149,12 @@ class DbStore implements LoadInterface, FlushObserverInterface
|
|||
*/
|
||||
private function doCreate($preference, $value)
|
||||
{
|
||||
return $this->dbAdapter->insert(
|
||||
return $this->db->insert(
|
||||
$this->table,
|
||||
array(
|
||||
self::COLUMN_USERNAME => $this->user->getUsername(),
|
||||
self::COLUMN_PREFERENCE => $preference,
|
||||
self::COLUMN_VALUE => $value
|
||||
$this->db->quoteIdentifier(self::COLUMN_USERNAME) => $this->user->getUsername(),
|
||||
$this->db->quoteIdentifier(self::COLUMN_PREFERENCE) => $preference,
|
||||
$this->db->quoteIdentifier(self::COLUMN_VALUE) => $value
|
||||
)
|
||||
);
|
||||
}
|
||||
|
@ -169,7 +168,7 @@ class DbStore implements LoadInterface, FlushObserverInterface
|
|||
*/
|
||||
private function doUpdate($preference, $value)
|
||||
{
|
||||
return $this->dbAdapter->update(
|
||||
return $this->db->update(
|
||||
$this->table,
|
||||
array(
|
||||
self::COLUMN_VALUE => $value
|
||||
|
@ -186,7 +185,7 @@ class DbStore implements LoadInterface, FlushObserverInterface
|
|||
*/
|
||||
private function doDelete($preference)
|
||||
{
|
||||
return $this->dbAdapter->delete(
|
||||
return $this->db->delete(
|
||||
$this->table,
|
||||
$this->createWhereCondition($preference)
|
||||
);
|
||||
|
|
|
@ -29,29 +29,46 @@
|
|||
|
||||
namespace Tests\Icinga\Authentication;
|
||||
|
||||
require_once('Zend/Config.php');
|
||||
require_once('Zend/Config/Ini.php');
|
||||
require_once('Zend/Db/Adapter/Abstract.php');
|
||||
require_once('Zend/Db.php');
|
||||
require_once('Zend/Log.php');
|
||||
require_once('../../library/Icinga/Util/ConfigAwareFactory.php');
|
||||
require_once('../../library/Icinga/Authentication/UserBackend.php');
|
||||
require_once('../../library/Icinga/Protocol/Ldap/Exception.php');
|
||||
require_once('../../library/Icinga/Application/DbAdapterFactory.php');
|
||||
require_once('../../library/Icinga/Application/Config.php');
|
||||
require_once('../../library/Icinga/Authentication/Credentials.php');
|
||||
require_once('../../library/Icinga/Authentication/Backend/DbUserBackend.php');
|
||||
require_once('../../library/Icinga/User.php');
|
||||
require_once('../../library/Icinga/Application/Logger.php');
|
||||
|
||||
use Zend_Config;
|
||||
use Zend_Db_Adapter_Abstract;
|
||||
use Icinga\Authentication\Backend\DbUserBackend;
|
||||
use Icinga\Application\DbAdapterFactory;
|
||||
use Icinga\Util\Crypto;
|
||||
use Icinga\Authentication\Credentials;
|
||||
use Icinga\User;
|
||||
use \Icinga\Application\Config;
|
||||
|
||||
/**
|
||||
*
|
||||
* Test Class fpr DbUserBackend
|
||||
* Created Wed, 17 Jul 2013 11:52:34 +0000
|
||||
*
|
||||
*/
|
||||
class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
/*
|
||||
* Mapping of columns
|
||||
*/
|
||||
const USER_NAME_COLUMN = 'username';
|
||||
|
||||
const SALT_COLUMN = 'salt';
|
||||
|
||||
const PASSWORD_COLUMN = 'password';
|
||||
|
||||
const ACTIVE_COLUMN = 'active';
|
||||
|
||||
/**
|
||||
* The table that is used to store the authentication data
|
||||
*
|
||||
|
@ -66,21 +83,6 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
|||
*/
|
||||
private $testDatabase = 'icinga_unittest';
|
||||
|
||||
/**
|
||||
* Mapping of columns
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $USER_NAME_COLUMN = 'user_name',
|
||||
$FIRST_NAME_COLUMN = 'first_name',
|
||||
$LAST_NAME_COLUMN = 'last_name',
|
||||
$LAST_LOGIN_COLUMN = 'last_login',
|
||||
$SALT_COLUMN = 'salt',
|
||||
$PASSWORD_COLUMN = 'password',
|
||||
$ACTIVE_COLUMN = 'active',
|
||||
$DOMAIN_COLUMN = 'domain',
|
||||
$EMAIL_COLUMN = 'email';
|
||||
|
||||
/**
|
||||
* Example users
|
||||
*
|
||||
|
@ -114,36 +116,40 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
|||
);
|
||||
|
||||
/**
|
||||
* Create a preset-configuration that can be used to access the database
|
||||
* Create a preset configuration that can be used to access the database
|
||||
* with the icinga_unittest account.
|
||||
*
|
||||
* @return \stdClass
|
||||
* @param String $dbType The database type as a string, like 'mysql' or 'pgsql'.
|
||||
*
|
||||
* @return Zend_Config The created resource configuration
|
||||
*/
|
||||
private function getBackendConfig()
|
||||
private function getResourceConfig($dbType)
|
||||
{
|
||||
$config = new \stdClass();
|
||||
$config->host = '127.0.0.1';
|
||||
$config->user = 'icinga_unittest';
|
||||
$config->password= 'icinga_unittest';
|
||||
$config->table = $this->testTable;
|
||||
$config->db = $this->testDatabase;
|
||||
return $config;
|
||||
return new Zend_Config(
|
||||
array(
|
||||
'type' => 'db',
|
||||
'db' => $dbType,
|
||||
'host' => 'localhost',
|
||||
'username' => 'icinga_unittest',
|
||||
'password' => 'icinga_unittest',
|
||||
'dbname' => $this->testDatabase,
|
||||
'table' => $this->testTable
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a backend with the given database type
|
||||
*
|
||||
* @param $dbType The database type as a string, like 'mysql' or 'pgsql'.
|
||||
* @param String $dbType The database type as a string, like 'mysql' or 'pgsql'.
|
||||
*
|
||||
* @return DbUserBackend|null
|
||||
* @return DbUserBackend|null
|
||||
*/
|
||||
private function createBackend($dbType)
|
||||
{
|
||||
try {
|
||||
$config = $this->getBackendConfig();
|
||||
$config->dbtype = $dbType;
|
||||
$db = $this->createDb($dbType,$config);
|
||||
$this->setUpDb($db);
|
||||
$db = $this->createDb($this->getResourceConfig($dbType));
|
||||
$this->setUpDb($db,$dbType);
|
||||
return new DbUserBackend($db);
|
||||
} catch(\Exception $e) {
|
||||
echo 'CREATE_BACKEND_ERROR:'.$e->getMessage();
|
||||
|
@ -151,29 +157,42 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the db adapter
|
||||
*
|
||||
* @param $config The configuration to use
|
||||
*
|
||||
* @return Zend_Db_Adapter_Abstract The created adabter
|
||||
*/
|
||||
private function createDb($config)
|
||||
{
|
||||
return DbAdapterFactory::createDbAdapterFromConfig($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the backends and fill it with sample-data
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
DbAdapterFactory::resetConfig();
|
||||
$this->users = Array(
|
||||
0 => Array(
|
||||
$this->USER_NAME_COLUMN => 'user1',
|
||||
$this->PASSWORD_COLUMN => 'secret1',
|
||||
$this->SALT_COLUMN => '8a7487a539c5d1d6766639d04d1ed1e6',
|
||||
$this->ACTIVE_COLUMN => 1
|
||||
self::USER_NAME_COLUMN => 'user1',
|
||||
self::PASSWORD_COLUMN => 'secret1',
|
||||
self::SALT_COLUMN => '8a7487a539c5d1d6766639d04d1ed1e6',
|
||||
self::ACTIVE_COLUMN => 1
|
||||
),
|
||||
1 => Array(
|
||||
$this->USER_NAME_COLUMN => 'user2',
|
||||
$this->PASSWORD_COLUMN => 'secret2',
|
||||
$this->SALT_COLUMN => '04b5521ddd761b5a5b633be83faa494d',
|
||||
$this->ACTIVE_COLUMN => 1
|
||||
self::USER_NAME_COLUMN => 'user2',
|
||||
self::PASSWORD_COLUMN => 'secret2',
|
||||
self::SALT_COLUMN => '04b5521ddd761b5a5b633be83faa494d',
|
||||
self::ACTIVE_COLUMN => 1
|
||||
),
|
||||
2 => Array(
|
||||
$this->USER_NAME_COLUMN => 'user3',
|
||||
$this->PASSWORD_COLUMN => 'secret3',
|
||||
$this->SALT_COLUMN => '08bb94ba3120338ae56db80ef551d324',
|
||||
$this->ACTIVE_COLUMN => 0
|
||||
self::USER_NAME_COLUMN => 'user3',
|
||||
self::PASSWORD_COLUMN => 'secret3',
|
||||
self::SALT_COLUMN => '08bb94ba3120338ae56db80ef551d324',
|
||||
self::ACTIVE_COLUMN => 0
|
||||
)
|
||||
);
|
||||
$this->mysql = $this->createBackend('mysql');
|
||||
|
@ -185,12 +204,11 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
|||
*/
|
||||
public function testCorrectUserLoginForPgsql()
|
||||
{
|
||||
if(!empty($this->pgsql)){
|
||||
if (!empty($this->pgsql)) {
|
||||
$this->runBackendAuthentication($this->pgsql);
|
||||
$this->runBackendUsername($this->pgsql);
|
||||
}
|
||||
else{
|
||||
echo '\nSKIPPING PGSQL TEST...\n';
|
||||
else {
|
||||
$this->markTestSkipped();
|
||||
}
|
||||
}
|
||||
|
@ -205,40 +223,21 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
|||
$this->runBackendUsername($this->mysql);
|
||||
}
|
||||
else{
|
||||
echo '\nSKIPPING MYSQL TEST...\n';
|
||||
$this->markTestSkipped();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a database with the given config and type
|
||||
*
|
||||
* @param $dbtype The database type as a string, like 'mysql' or 'pgsql'.
|
||||
* @param $config The configuration-object.
|
||||
* @return mixed
|
||||
*/
|
||||
private function createDb($dbtype,$config)
|
||||
{
|
||||
return \Zend_Db::factory($this->dbTypeMap[$dbtype],
|
||||
array(
|
||||
'host' => $config->host,
|
||||
'username' => $config->user,
|
||||
'password' => $config->password,
|
||||
'dbname' => 'icinga_unittest'
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to drop all databases that may eventually be present
|
||||
*/
|
||||
public function tearDown()
|
||||
{
|
||||
try{
|
||||
$db = $this->createDb('mysql',$this->getBackendConfig());
|
||||
$db = $this->createDb($this->getResourceConfig('mysql'));
|
||||
$this->tearDownDb($db);
|
||||
} catch(\Exception $e) { }
|
||||
try {
|
||||
$db = $this->createDb('pgsql',$this->getBackendConfig());
|
||||
$db = $this->createDb($this->getResourceConfig('pgsql'));
|
||||
$this->tearDownDb($db);
|
||||
} catch(\Exception $e) { }
|
||||
}
|
||||
|
@ -256,37 +255,28 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
|||
/**
|
||||
* Fill the given database with the sample-data provided in users
|
||||
*
|
||||
* @param $db
|
||||
* @param $db Zend_Db_Adapter_Abstract The database to set up
|
||||
* @param $type String The database type as a string: 'mysql'|'pgsql'
|
||||
*/
|
||||
private function setUpDb($db)
|
||||
private function setUpDb($db,$type)
|
||||
{
|
||||
try {
|
||||
$this->tearDownDb($db);
|
||||
} catch (\Exception $e) {
|
||||
// if no database exists, an exception will be thrown
|
||||
}
|
||||
$db->exec('CREATE TABLE '.$this->testTable.' (
|
||||
'.$this->USER_NAME_COLUMN.' varchar(255) NOT NULL,
|
||||
'.$this->FIRST_NAME_COLUMN.' varchar(255),
|
||||
'.$this->LAST_NAME_COLUMN.' varchar(255),
|
||||
'.$this->LAST_LOGIN_COLUMN.' timestamp,
|
||||
'.$this->SALT_COLUMN.' varchar(255),
|
||||
'.$this->DOMAIN_COLUMN.' varchar(255),
|
||||
'.$this->EMAIL_COLUMN.' varchar(255),
|
||||
'.$this->PASSWORD_COLUMN.' varchar(255) NOT NULL,
|
||||
'.$this->ACTIVE_COLUMN.' BOOL,
|
||||
PRIMARY KEY ('.$this->USER_NAME_COLUMN.')
|
||||
)');
|
||||
} catch (\Exception $e) {}
|
||||
|
||||
$setupScript = file_get_contents('../../etc/schema/accounts.' . $type . '.sql');
|
||||
$db->exec($setupScript);
|
||||
|
||||
for ($i = 0; $i < count($this->users); $i++) {
|
||||
$usr = $this->users[$i];
|
||||
$data = Array(
|
||||
$this->USER_NAME_COLUMN => $usr[$this->USER_NAME_COLUMN],
|
||||
$this->PASSWORD_COLUMN => hash_hmac('sha256',
|
||||
$usr[$this->SALT_COLUMN],
|
||||
$usr[$this->PASSWORD_COLUMN]
|
||||
),
|
||||
$this->ACTIVE_COLUMN => $usr[$this->ACTIVE_COLUMN],
|
||||
$this->SALT_COLUMN => $usr[$this->SALT_COLUMN]
|
||||
self::USER_NAME_COLUMN => $usr[self::USER_NAME_COLUMN],
|
||||
self::PASSWORD_COLUMN => hash_hmac('sha256',
|
||||
$usr[self::SALT_COLUMN],
|
||||
$usr[self::PASSWORD_COLUMN]
|
||||
),
|
||||
self::ACTIVE_COLUMN => $usr[self::ACTIVE_COLUMN],
|
||||
self::SALT_COLUMN => $usr[self::SALT_COLUMN]
|
||||
);
|
||||
$db->insert($this->testTable,$data);
|
||||
}
|
||||
|
@ -296,53 +286,53 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
|||
/**
|
||||
* Run the hasUsername test against an instance of DbUserBackend
|
||||
*
|
||||
* @param $backend The backend that will be tested.
|
||||
* @param $backend The backend that will be tested.
|
||||
*/
|
||||
private function runBackendUsername($backend)
|
||||
{
|
||||
// Known user
|
||||
$this->assertTrue($backend->hasUsername(
|
||||
new Credentials(
|
||||
$this->users[0][$this->USER_NAME_COLUMN],
|
||||
$this->users[0][$this->PASSWORD_COLUMN])
|
||||
),'Assert that the user is known by the backend');
|
||||
$this->users[0][self::USER_NAME_COLUMN],
|
||||
$this->users[0][self::PASSWORD_COLUMN])
|
||||
), 'Assert that the user is known by the backend');
|
||||
|
||||
// Unknown user
|
||||
$this->assertFalse($backend->hasUsername(
|
||||
new Credentials(
|
||||
'unknown user',
|
||||
'secret')
|
||||
),'Assert that the user is not known by the backend');
|
||||
), 'Assert that the user is not known by the backend');
|
||||
|
||||
// Inactive user
|
||||
$this->assertFalse($backend->hasUsername(
|
||||
new Credentials(
|
||||
$this->users[2][$this->USER_NAME_COLUMN],
|
||||
$this->users[2][$this->PASSWORD_COLUMN])
|
||||
),'Assert that the user is inactive and therefore not known by the backend');
|
||||
$this->users[2][self::USER_NAME_COLUMN],
|
||||
$this->users[2][self::PASSWORD_COLUMN])
|
||||
), 'Assert that the user is inactive and therefore not known by the backend');
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the authentication test against an instance of DbUserBackend
|
||||
*
|
||||
* @param $backend The backend that will be tested.
|
||||
* @param $backend The backend that will be tested.
|
||||
*/
|
||||
private function runBackendAuthentication($backend)
|
||||
{
|
||||
// Known user
|
||||
$this->assertNotNull($backend->authenticate(
|
||||
new Credentials(
|
||||
$this->users[0][$this->USER_NAME_COLUMN],
|
||||
$this->users[0][$this->PASSWORD_COLUMN])
|
||||
),'Assert that an existing, active user with the right credentials can authenticate.');
|
||||
$this->users[0][self::USER_NAME_COLUMN],
|
||||
$this->users[0][self::PASSWORD_COLUMN])
|
||||
), 'Assert that an existing, active user with the right credentials can authenticate.');
|
||||
|
||||
// Wrong password
|
||||
$this->assertNull(
|
||||
$backend->authenticate(
|
||||
new Credentials(
|
||||
$this->users[1][$this->USER_NAME_COLUMN],
|
||||
$this->users[1][self::USER_NAME_COLUMN],
|
||||
'wrongpassword')
|
||||
),'Assert that an existing user with an invalid password cannot authenticate'
|
||||
), 'Assert that an existing user with an invalid password cannot authenticate'
|
||||
);
|
||||
|
||||
// Nonexisting user
|
||||
|
@ -350,16 +340,16 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
|||
$backend->authenticate(
|
||||
new Credentials(
|
||||
'nonexisting user',
|
||||
$this->users[1][$this->PASSWORD_COLUMN])
|
||||
),'Assert that a non-existing user cannot authenticate.'
|
||||
$this->users[1][self::PASSWORD_COLUMN])
|
||||
), 'Assert that a non-existing user cannot authenticate.'
|
||||
);
|
||||
|
||||
// Inactive user
|
||||
$this->assertNull(
|
||||
$backend->authenticate(
|
||||
new Credentials(
|
||||
$this->users[2][$this->USER_NAME_COLUMN],
|
||||
$this->users[2][$this->PASSWORD_COLUMN])
|
||||
),'Assert that an inactive user cannot authenticate.');
|
||||
$this->users[2][self::USER_NAME_COLUMN],
|
||||
$this->users[2][self::PASSWORD_COLUMN])
|
||||
), 'Assert that an inactive user cannot authenticate.');
|
||||
}
|
||||
}
|
|
@ -2,20 +2,26 @@
|
|||
|
||||
namespace Tests\Icinga\User\Preferences;
|
||||
|
||||
require_once __DIR__. '/../../../../../../library/Icinga/Exception/ConfigurationError.php';
|
||||
require_once __DIR__. '/../../../../../../library/Icinga/User.php';
|
||||
require_once __DIR__. '/../../../../../../library/Icinga/User/Preferences.php';
|
||||
require_once __DIR__. '/../../../../../../library/Icinga/User/Preferences/LoadInterface.php';
|
||||
require_once __DIR__. '/../../../../../../library/Icinga/User/Preferences/FlushObserverInterface.php';
|
||||
require_once __DIR__. '/../../../../../../library/Icinga/User/Preferences/DbStore.php';
|
||||
require_once __DIR__ . '/../../../../../../library/Icinga/Exception/ConfigurationError.php';
|
||||
require_once __DIR__ . '/../../../../../../library/Icinga/Util/ConfigAwareFactory.php';
|
||||
require_once __DIR__ . '/../../../../../../library/Icinga/Application/DbAdapterFactory.php';
|
||||
require_once __DIR__ . '/../../../../../../library/Icinga/User.php';
|
||||
require_once __DIR__ . '/../../../../../../library/Icinga/User/Preferences.php';
|
||||
require_once __DIR__ . '/../../../../../../library/Icinga/User/Preferences/ChangeSet.php';
|
||||
require_once __DIR__ . '/../../../../../../library/Icinga/User/Preferences/LoadInterface.php';
|
||||
require_once __DIR__ . '/../../../../../../library/Icinga/User/Preferences/FlushObserverInterface.php';
|
||||
require_once __DIR__ . '/../../../../../../library/Icinga/User/Preferences/DbStore.php';
|
||||
|
||||
require_once 'Zend/Db.php';
|
||||
require_once 'Zend/Config.php';
|
||||
require_once 'Zend/Db/Adapter/Abstract.php';
|
||||
|
||||
use Icinga\Application\DbAdapterFactory;
|
||||
use Icinga\User;
|
||||
use Icinga\User\Preferences\DbStore;
|
||||
use Icinga\User\Preferences;
|
||||
use \PHPUnit_Framework_TestCase;
|
||||
use \Zend_Config;
|
||||
use \Zend_Db;
|
||||
use \Zend_Db_Adapter_Abstract;
|
||||
use \PDOException;
|
||||
|
@ -27,11 +33,10 @@ class DbStoreTest extends PHPUnit_Framework_TestCase
|
|||
|
||||
const TYPE_PGSQL = 'pgsql';
|
||||
|
||||
private $database = 'icinga_unittest';
|
||||
|
||||
private $table = 'preferences';
|
||||
private $table = 'preference';
|
||||
|
||||
private $databaseConfig = array(
|
||||
'type' => 'db',
|
||||
'host' => '127.0.0.1',
|
||||
'username' => 'icinga_unittest',
|
||||
'password' => 'icinga_unittest',
|
||||
|
@ -50,26 +55,18 @@ class DbStoreTest extends PHPUnit_Framework_TestCase
|
|||
|
||||
private function createDb($type)
|
||||
{
|
||||
$zendType = 'PDO_'. strtoupper($type);
|
||||
|
||||
if ($type === self::TYPE_MYSQL) {
|
||||
$this->databaseConfig['port'] = 3306;
|
||||
} elseif ($type === self::TYPE_PGSQL) {
|
||||
$this->databaseConfig['port'] = 5432;
|
||||
}
|
||||
|
||||
$db = Zend_Db::factory(
|
||||
$zendType,
|
||||
$this->databaseConfig
|
||||
$this->databaseConfig['db'] = $type;
|
||||
$db = DbAdapterFactory::createDbAdapterFromConfig(
|
||||
new Zend_Config($this->databaseConfig)
|
||||
);
|
||||
|
||||
try {
|
||||
$db->getConnection();
|
||||
|
||||
$dumpFile = realpath(__DIR__. '/../../../../../../etc/schema/preferences.'. strtolower($type). '.sql');
|
||||
$dumpFile = realpath(__DIR__ . '/../../../../../../etc/schema/preferences.' . strtolower($type) . '.sql');
|
||||
|
||||
if (!$dumpFile) {
|
||||
throw new Exception('Dumpfile for db type not found: '. $type);
|
||||
throw new Exception('Dumpfile for db type not found: ' . $type);
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -96,11 +93,11 @@ class DbStoreTest extends PHPUnit_Framework_TestCase
|
|||
protected function tearDown()
|
||||
{
|
||||
if ($this->dbMysql) {
|
||||
$this->dbMysql->getConnection()->exec('DROP TABLE '. $this->table);
|
||||
$this->dbMysql->getConnection()->exec('DROP TABLE ' . $this->table);
|
||||
}
|
||||
|
||||
if ($this->dbPgsql) {
|
||||
$this->dbPgsql->getConnection()->exec('DROP TABLE '. $this->table);
|
||||
$this->dbPgsql->getConnection()->exec('DROP TABLE ' . $this->table);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -165,5 +162,4 @@ class DbStoreTest extends PHPUnit_Framework_TestCase
|
|||
$this->markTestSkipped('PgSQL test environment is not configured');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue