parent
b013966464
commit
fd4cbf1c5b
|
@ -9,4 +9,24 @@ create table icinga_users (
|
||||||
password varchar(255) NOT NULL,
|
password varchar(255) NOT NULL,
|
||||||
active BOOL,
|
active BOOL,
|
||||||
PRIMARY KEY (user_name)
|
PRIMARY KEY (user_name)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user: icingaadmin
|
||||||
|
* password: icinga
|
||||||
|
*/
|
||||||
|
INSERT INTO icinga_users (
|
||||||
|
user_name,
|
||||||
|
first_name,
|
||||||
|
last_name,
|
||||||
|
salt,
|
||||||
|
password,
|
||||||
|
active)
|
||||||
|
VALUES (
|
||||||
|
'icingaadmin',
|
||||||
|
'john',
|
||||||
|
'doe',
|
||||||
|
'IepKgTTShC',
|
||||||
|
'52deddb5cc7a5769484fcb0fbc5981a7c62cd9f3ddbb8ff3ddb1b89ea324ad16',
|
||||||
|
true
|
||||||
|
);
|
|
@ -9,4 +9,24 @@ create table icinga_users (
|
||||||
password varchar(255) NOT NULL,
|
password varchar(255) NOT NULL,
|
||||||
active BOOL,
|
active BOOL,
|
||||||
PRIMARY KEY (user_name)
|
PRIMARY KEY (user_name)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* user: icingaadmin
|
||||||
|
* password: icinga
|
||||||
|
*/
|
||||||
|
INSERT INTO icinga_users (
|
||||||
|
user_name,
|
||||||
|
first_name,
|
||||||
|
last_name,
|
||||||
|
salt,
|
||||||
|
password,
|
||||||
|
active)
|
||||||
|
VALUES (
|
||||||
|
'icingaadmin',
|
||||||
|
'john',
|
||||||
|
'doe',
|
||||||
|
'IepKgTTShC',
|
||||||
|
'52deddb5cc7a5769484fcb0fbc5981a7c62cd9f3ddbb8ff3ddb1b89ea324ad16',
|
||||||
|
true
|
||||||
|
);
|
|
@ -64,10 +64,12 @@ class DbUserBackend implements UserBackend {
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a DbUserBackend with the given configuration.
|
* Creates a DbUserBackend with the given configuration
|
||||||
|
*
|
||||||
* @param $config The configuration-object containing the members host,user,password,db
|
* @param $config The configuration-object containing the members host,user,password,db
|
||||||
*/
|
*/
|
||||||
public function __construct($config){
|
public function __construct($config)
|
||||||
|
{
|
||||||
$this->dbtype = $config->dbtype;
|
$this->dbtype = $config->dbtype;
|
||||||
$this->userTable = $config->table;
|
$this->userTable = $config->table;
|
||||||
|
|
||||||
|
@ -88,7 +90,8 @@ class DbUserBackend implements UserBackend {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the user in the given Credentials-object is available.
|
* Checks if the user in the given Credentials-object is available
|
||||||
|
*
|
||||||
* @param Credentials $credentials The login credentials of the user.
|
* @param Credentials $credentials The login credentials of the user.
|
||||||
* @return boolean True when the username is known and currently active.
|
* @return boolean True when the username is known and currently active.
|
||||||
*/
|
*/
|
||||||
|
@ -99,11 +102,13 @@ class DbUserBackend implements UserBackend {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Authenticate a user with the given credentials.
|
* Authenticate a user with the given credentials
|
||||||
|
*
|
||||||
* @param Credentials $credentials
|
* @param Credentials $credentials
|
||||||
* @return User|null The authenticated user or Null.
|
* @return User|null The authenticated user or Null.
|
||||||
*/
|
*/
|
||||||
public function authenticate(Credentials $credential){
|
public function authenticate(Credentials $credential)
|
||||||
|
{
|
||||||
$this->db->getConnection();
|
$this->db->getConnection();
|
||||||
$res = $this->db
|
$res = $this->db
|
||||||
->select()->from($this->userTable)
|
->select()->from($this->userTable)
|
||||||
|
@ -114,7 +119,7 @@ class DbUserBackend implements UserBackend {
|
||||||
$credential->getPassword())
|
$credential->getPassword())
|
||||||
)
|
)
|
||||||
->query()->fetch();
|
->query()->fetch();
|
||||||
if(!empty($res)){
|
if (!empty($res)) {
|
||||||
$this->updateLastLogin($credential->getUsername());
|
$this->updateLastLogin($credential->getUsername());
|
||||||
return $this->createUserFromResult($res);
|
return $this->createUserFromResult($res);
|
||||||
}
|
}
|
||||||
|
@ -122,10 +127,12 @@ class DbUserBackend implements UserBackend {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the timestamp containing the time of the last login for
|
* Updates the timestamp containing the time of the last login for
|
||||||
* the user with the given username.
|
* the user with the given username
|
||||||
|
*
|
||||||
* @param $username The login-name of the user.
|
* @param $username The login-name of the user.
|
||||||
*/
|
*/
|
||||||
private function updateLastLogin($username){
|
private function updateLastLogin($username)
|
||||||
|
{
|
||||||
$this->db->getConnection();
|
$this->db->getConnection();
|
||||||
$this->db->update(
|
$this->db->update(
|
||||||
$this->userTable,
|
$this->userTable,
|
||||||
|
@ -136,11 +143,13 @@ class DbUserBackend implements UserBackend {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches the user's salt from the database.
|
* Fetches the user's 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){
|
private function getUserSalt($username)
|
||||||
|
{
|
||||||
$this->db->getConnection();
|
$this->db->getConnection();
|
||||||
$res = $this->db->select()
|
$res = $this->db->select()
|
||||||
->from($this->userTable,$this->SALT_COLUMN)
|
->from($this->userTable,$this->SALT_COLUMN)
|
||||||
|
@ -150,29 +159,33 @@ class DbUserBackend implements UserBackend {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches the user information from the database.
|
* Fetches 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){
|
private function getUserByName($username)
|
||||||
|
{
|
||||||
$this->db->getConnection();
|
$this->db->getConnection();
|
||||||
$res = $this->db->
|
$res = $this->db->
|
||||||
select()->from($this->userTable)
|
select()->from($this->userTable)
|
||||||
->where($this->USER_NAME_COLUMN.' = ?',$username)
|
->where($this->USER_NAME_COLUMN.' = ?',$username)
|
||||||
->where($this->ACTIVE_COLUMN.' = ?',true)
|
->where($this->ACTIVE_COLUMN.' = ?',true)
|
||||||
->query()->fetch();
|
->query()->fetch();
|
||||||
if(empty($res)){
|
if (empty($res)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return $this->createUserFromResult($res);
|
return $this->createUserFromResult($res);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance of User from the given result-array.
|
* Creates a new instance of User from the given result-array
|
||||||
|
*
|
||||||
* @param array $result The query result-array containing the column
|
* @param array $result The query result-array containing the column
|
||||||
* @return User The created instance of User.
|
* @return User The created instance of User.
|
||||||
*/
|
*/
|
||||||
private function createUserFromResult(Array $result){
|
private function createUserFromResult(Array $result)
|
||||||
|
{
|
||||||
$usr = new User(
|
$usr = new User(
|
||||||
$result[$this->USER_NAME_COLUMN],
|
$result[$this->USER_NAME_COLUMN],
|
||||||
$result[$this->FIRST_NAME_COLUMN],
|
$result[$this->FIRST_NAME_COLUMN],
|
||||||
|
|
|
@ -60,6 +60,7 @@ 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.
|
* with the icinga_unittest account.
|
||||||
* @return \stdClass
|
* @return \stdClass
|
||||||
*/
|
*/
|
||||||
|
@ -75,19 +76,20 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a backend with the given database type.
|
* Create a backend with the given database type
|
||||||
|
*
|
||||||
* @param $dbType The database type as a string, like "mysql" or "pgsql".
|
* @param $dbType The database type as a string, like "mysql" or "pgsql".
|
||||||
* @return DbUserBackend|null
|
* @return DbUserBackend|null
|
||||||
*/
|
*/
|
||||||
private function createBackend($dbType){
|
private function createBackend($dbType)
|
||||||
try{
|
{
|
||||||
|
try {
|
||||||
$config = $this->getBackendConfig();
|
$config = $this->getBackendConfig();
|
||||||
$config->dbtype = $dbType;
|
$config->dbtype = $dbType;
|
||||||
$db = $this->createDb($dbType,$config);
|
$db = $this->createDb($dbType,$config);
|
||||||
$this->setUpDb($db);
|
$this->setUpDb($db);
|
||||||
return new DbUserBackend($config);
|
return new DbUserBackend($config);
|
||||||
}
|
} catch(\Exception $e) {
|
||||||
catch(\Exception $e){
|
|
||||||
echo "CREATE_BACKEND_ERROR:".$e->getMessage();
|
echo "CREATE_BACKEND_ERROR:".$e->getMessage();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -125,7 +127,8 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
||||||
/**
|
/**
|
||||||
* Test the PostgreSQL backend.
|
* Test the PostgreSQL backend.
|
||||||
*/
|
*/
|
||||||
public function testPgsql(){
|
public function testPgsql()
|
||||||
|
{
|
||||||
if(!empty($this->pgsql)){
|
if(!empty($this->pgsql)){
|
||||||
$this->runBackendAuthentication($this->pgsql);
|
$this->runBackendAuthentication($this->pgsql);
|
||||||
$this->runBackendUsername($this->pgsql);
|
$this->runBackendUsername($this->pgsql);
|
||||||
|
@ -139,7 +142,8 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
||||||
/**
|
/**
|
||||||
* Test the MySQL-Backend.
|
* Test the MySQL-Backend.
|
||||||
*/
|
*/
|
||||||
public function testMySQL(){
|
public function testMySQL()
|
||||||
|
{
|
||||||
if(!empty($this->mysql)){
|
if(!empty($this->mysql)){
|
||||||
$this->runBackendAuthentication($this->mysql);
|
$this->runBackendAuthentication($this->mysql);
|
||||||
$this->runBackendUsername($this->mysql);
|
$this->runBackendUsername($this->mysql);
|
||||||
|
@ -151,12 +155,14 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a database with the given config and type.
|
* Create a database with the given config and type
|
||||||
|
*
|
||||||
* @param $dbtype The database type as a string, like "mysql" or "pgsql".
|
* @param $dbtype The database type as a string, like "mysql" or "pgsql".
|
||||||
* @param $config The configuration-object.
|
* @param $config The configuration-object.
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
private function createDb($dbtype,$config){
|
private function createDb($dbtype,$config)
|
||||||
|
{
|
||||||
return \Zend_Db::factory($this->dbTypeMap[$dbtype],
|
return \Zend_Db::factory($this->dbTypeMap[$dbtype],
|
||||||
array(
|
array(
|
||||||
'host' => $config->host,
|
'host' => $config->host,
|
||||||
|
@ -167,35 +173,37 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Try to drop all databases that may eventually be present.
|
* Try to drop all databases that may eventually be present
|
||||||
*/
|
*/
|
||||||
public function tearDown()
|
public function tearDown()
|
||||||
{
|
{
|
||||||
try{
|
try{
|
||||||
$db = $this->createDb("mysql",$this->getBackendConfig());
|
$db = $this->createDb("mysql",$this->getBackendConfig());
|
||||||
$this->tearDownDb($db);
|
$this->tearDownDb($db);
|
||||||
}
|
} catch(\Exception $e) { }
|
||||||
catch(\Exception $e){}
|
try {
|
||||||
try{
|
|
||||||
$db = $this->createDb("pgsql",$this->getBackendConfig());
|
$db = $this->createDb("pgsql",$this->getBackendConfig());
|
||||||
$this->tearDownDb($db);
|
$this->tearDownDb($db);
|
||||||
}
|
} catch(\Exception $e) { }
|
||||||
catch(\Exception $e){}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Drop the test database in the given db.
|
* Drop the test database in the given db
|
||||||
|
*
|
||||||
* @param $db
|
* @param $db
|
||||||
*/
|
*/
|
||||||
private function tearDownDb($db){
|
private function tearDownDb($db)
|
||||||
|
{
|
||||||
$db->exec('DROP TABLE '.$this->testTable);
|
$db->exec('DROP TABLE '.$this->testTable);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fill the given database with the sample-data provided in users.
|
* Fill the given database with the sample-data provided in users
|
||||||
|
*
|
||||||
* @param $db
|
* @param $db
|
||||||
*/
|
*/
|
||||||
private function setUpDb($db){
|
private function setUpDb($db)
|
||||||
|
{
|
||||||
$db->exec('CREATE TABLE '.$this->testTable.' (
|
$db->exec('CREATE TABLE '.$this->testTable.' (
|
||||||
'.$this->USER_NAME_COLUMN.' varchar(255) NOT NULL,
|
'.$this->USER_NAME_COLUMN.' varchar(255) NOT NULL,
|
||||||
'.$this->FIRST_NAME_COLUMN.' varchar(255),
|
'.$this->FIRST_NAME_COLUMN.' varchar(255),
|
||||||
|
@ -208,7 +216,7 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
||||||
'.$this->ACTIVE_COLUMN.' BOOL,
|
'.$this->ACTIVE_COLUMN.' BOOL,
|
||||||
PRIMARY KEY ('.$this->USER_NAME_COLUMN.')
|
PRIMARY KEY ('.$this->USER_NAME_COLUMN.')
|
||||||
)');
|
)');
|
||||||
for($i = 0; $i < count($this->users); $i++){
|
for ($i = 0; $i < count($this->users); $i++) {
|
||||||
$usr = $this->users[$i];
|
$usr = $this->users[$i];
|
||||||
$data = Array(
|
$data = Array(
|
||||||
$this->USER_NAME_COLUMN => $usr[$this->USER_NAME_COLUMN],
|
$this->USER_NAME_COLUMN => $usr[$this->USER_NAME_COLUMN],
|
||||||
|
@ -225,10 +233,12 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run the hasUsername test against an instance of DbUserBackend.
|
* 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){
|
private function runBackendUsername($backend)
|
||||||
|
{
|
||||||
// Known user
|
// Known user
|
||||||
$this->assertTrue($backend->hasUsername(
|
$this->assertTrue($backend->hasUsername(
|
||||||
new Credentials(
|
new Credentials(
|
||||||
|
@ -252,10 +262,12 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run the authentication test against an instance of DbUserBackend.
|
* 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){
|
private function runBackendAuthentication($backend)
|
||||||
|
{
|
||||||
// Known user
|
// Known user
|
||||||
$this->assertNotNull($backend->authenticate(
|
$this->assertNotNull($backend->authenticate(
|
||||||
new Credentials(
|
new Credentials(
|
||||||
|
|
Loading…
Reference in New Issue