128 lines
3.1 KiB
PHP
128 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace EesyPHP\Auth;
|
|
|
|
use EesyPHP\App;
|
|
use EesyPHP\Log;
|
|
|
|
use Exception;
|
|
|
|
class Db extends Backend {
|
|
|
|
/**
|
|
* Database connection object
|
|
* @var class-string|null
|
|
*/
|
|
private static $class = null;
|
|
|
|
/**
|
|
* Users table name
|
|
* @var string
|
|
*/
|
|
private static $users_table;
|
|
|
|
/**
|
|
* Username field name
|
|
* @var string
|
|
*/
|
|
private static $username_field;
|
|
|
|
/**
|
|
* Password field name
|
|
* @var string
|
|
*/
|
|
private static $password_field;
|
|
|
|
/**
|
|
* List of Db fields exposed in User object
|
|
* @var array
|
|
*/
|
|
private static $exposed_fields;
|
|
|
|
/**
|
|
* Initialize
|
|
* @return boolean
|
|
*/
|
|
public static function init() {
|
|
// Set config default values
|
|
App :: set_default(
|
|
'auth.db',
|
|
array(
|
|
'class' => '\\EesyPHP\\Db',
|
|
'users_table' => 'users',
|
|
'username_field' => 'username',
|
|
'password_field' => 'password',
|
|
'exposed_fields' => array('name', 'mail'),
|
|
)
|
|
);
|
|
self :: $class = App::get('auth.db.class', null, 'string');
|
|
if (!self :: $class || !class_exists(self :: $class)) {
|
|
Log :: warning(
|
|
'Database class %s configured as authentication backend does not exists, can not '.
|
|
'initialize this authentication backend.', self :: $class
|
|
);
|
|
return false;
|
|
}
|
|
self :: $users_table = App::get('auth.db.users_table', null, 'string');
|
|
self :: $username_field = App::get('auth.db.username_field', null, 'string');
|
|
self :: $password_field = App::get('auth.db.password_field', null, 'string');
|
|
self :: $exposed_fields = App::get('auth.db.exposed_fields', null, 'array');
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Connect to database
|
|
* @return void
|
|
*/
|
|
private static function connect() {
|
|
self :: $class :: connect();
|
|
}
|
|
|
|
/**
|
|
* Retrieve a user by its username
|
|
* @param string $username
|
|
* @return \EesyPHP\Auth\User|null|false The user object if found, null it not, false in case of error
|
|
*/
|
|
public static function get_user($username) {
|
|
self :: connect();
|
|
try {
|
|
$info = self :: $class :: get_one(
|
|
self :: $users_table,
|
|
[self :: $username_field => $username],
|
|
self :: $exposed_fields
|
|
);
|
|
if ($info === false)
|
|
return null;
|
|
return new User($username, '\\EesyPHP\\Auth\\Db', $info);
|
|
}
|
|
catch (Exception $e) {
|
|
Log :: error("Error retrieving user %s info from database: %s", $username, $e->getMessage());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Check a user password
|
|
* @param \EesyPHP\Auth\User $user The user object
|
|
* @param string $password The password to check
|
|
* @return boolean
|
|
*/
|
|
public static function check_password($user, $password) {
|
|
self :: connect();
|
|
try {
|
|
$info = self :: $class :: get_one(
|
|
self :: $users_table,
|
|
[self :: $username_field => $user->username],
|
|
[self :: $password_field]
|
|
);
|
|
if ($info === false)
|
|
return false;
|
|
return password_verify($password, $info[self :: $password_field]);
|
|
}
|
|
catch (Exception $e) {
|
|
Log :: error("Error retrieving user %s password from database: %s", $user, $e->getMessage());
|
|
}
|
|
return false;
|
|
}
|
|
}
|