parent
b013966464
commit
fd4cbf1c5b
|
@ -9,4 +9,24 @@ create table icinga_users (
|
|||
password varchar(255) NOT NULL,
|
||||
active BOOL,
|
||||
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,
|
||||
active BOOL,
|
||||
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
|
||||
*/
|
||||
public function __construct($config){
|
||||
public function __construct($config)
|
||||
{
|
||||
$this->dbtype = $config->dbtype;
|
||||
$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.
|
||||
* @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
|
||||
* @return User|null The authenticated user or Null.
|
||||
*/
|
||||
public function authenticate(Credentials $credential){
|
||||
public function authenticate(Credentials $credential)
|
||||
{
|
||||
$this->db->getConnection();
|
||||
$res = $this->db
|
||||
->select()->from($this->userTable)
|
||||
|
@ -114,7 +119,7 @@ class DbUserBackend implements UserBackend {
|
|||
$credential->getPassword())
|
||||
)
|
||||
->query()->fetch();
|
||||
if(!empty($res)){
|
||||
if (!empty($res)) {
|
||||
$this->updateLastLogin($credential->getUsername());
|
||||
return $this->createUserFromResult($res);
|
||||
}
|
||||
|
@ -122,10 +127,12 @@ class DbUserBackend implements UserBackend {
|
|||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
private function updateLastLogin($username){
|
||||
private function updateLastLogin($username)
|
||||
{
|
||||
$this->db->getConnection();
|
||||
$this->db->update(
|
||||
$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.
|
||||
* @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();
|
||||
$res = $this->db->select()
|
||||
->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.
|
||||
* @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();
|
||||
$res = $this->db->
|
||||
select()->from($this->userTable)
|
||||
->where($this->USER_NAME_COLUMN.' = ?',$username)
|
||||
->where($this->ACTIVE_COLUMN.' = ?',true)
|
||||
->query()->fetch();
|
||||
if(empty($res)){
|
||||
if (empty($res)) {
|
||||
return null;
|
||||
}
|
||||
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
|
||||
* @return User The created instance of User.
|
||||
*/
|
||||
private function createUserFromResult(Array $result){
|
||||
private function createUserFromResult(Array $result)
|
||||
{
|
||||
$usr = new User(
|
||||
$result[$this->USER_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
|
||||
*
|
||||
* with the icinga_unittest account.
|
||||
* @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".
|
||||
* @return DbUserBackend|null
|
||||
*/
|
||||
private function createBackend($dbType){
|
||||
try{
|
||||
private function createBackend($dbType)
|
||||
{
|
||||
try {
|
||||
$config = $this->getBackendConfig();
|
||||
$config->dbtype = $dbType;
|
||||
$db = $this->createDb($dbType,$config);
|
||||
$this->setUpDb($db);
|
||||
return new DbUserBackend($config);
|
||||
}
|
||||
catch(\Exception $e){
|
||||
} catch(\Exception $e) {
|
||||
echo "CREATE_BACKEND_ERROR:".$e->getMessage();
|
||||
return null;
|
||||
}
|
||||
|
@ -125,7 +127,8 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
|||
/**
|
||||
* Test the PostgreSQL backend.
|
||||
*/
|
||||
public function testPgsql(){
|
||||
public function testPgsql()
|
||||
{
|
||||
if(!empty($this->pgsql)){
|
||||
$this->runBackendAuthentication($this->pgsql);
|
||||
$this->runBackendUsername($this->pgsql);
|
||||
|
@ -139,7 +142,8 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
|||
/**
|
||||
* Test the MySQL-Backend.
|
||||
*/
|
||||
public function testMySQL(){
|
||||
public function testMySQL()
|
||||
{
|
||||
if(!empty($this->mysql)){
|
||||
$this->runBackendAuthentication($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 $config The configuration-object.
|
||||
* @return mixed
|
||||
*/
|
||||
private function createDb($dbtype,$config){
|
||||
private function createDb($dbtype,$config)
|
||||
{
|
||||
return \Zend_Db::factory($this->dbTypeMap[$dbtype],
|
||||
array(
|
||||
'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()
|
||||
{
|
||||
try{
|
||||
$db = $this->createDb("mysql",$this->getBackendConfig());
|
||||
$this->tearDownDb($db);
|
||||
}
|
||||
catch(\Exception $e){}
|
||||
try{
|
||||
} catch(\Exception $e) { }
|
||||
try {
|
||||
$db = $this->createDb("pgsql",$this->getBackendConfig());
|
||||
$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
|
||||
*/
|
||||
private function tearDownDb($db){
|
||||
private function tearDownDb($db)
|
||||
{
|
||||
$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
|
||||
*/
|
||||
private function setUpDb($db){
|
||||
private function setUpDb($db)
|
||||
{
|
||||
$db->exec('CREATE TABLE '.$this->testTable.' (
|
||||
'.$this->USER_NAME_COLUMN.' varchar(255) NOT NULL,
|
||||
'.$this->FIRST_NAME_COLUMN.' varchar(255),
|
||||
|
@ -208,7 +216,7 @@ class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
|||
'.$this->ACTIVE_COLUMN.' BOOL,
|
||||
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];
|
||||
$data = Array(
|
||||
$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.
|
||||
*/
|
||||
private function runBackendUsername($backend){
|
||||
private function runBackendUsername($backend)
|
||||
{
|
||||
// Known user
|
||||
$this->assertTrue($backend->hasUsername(
|
||||
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.
|
||||
*/
|
||||
private function runBackendAuthentication($backend){
|
||||
private function runBackendAuthentication($backend)
|
||||
{
|
||||
// Known user
|
||||
$this->assertNotNull($backend->authenticate(
|
||||
new Credentials(
|
||||
|
|
Loading…
Reference in New Issue