Compare commits
2 commits
dbf5b0a54c
...
6c74b2a719
Author | SHA1 | Date | |
---|---|---|---|
|
6c74b2a719 | ||
|
20461e3e47 |
2 changed files with 100 additions and 15 deletions
|
@ -3,6 +3,7 @@
|
||||||
namespace EesyPHP\Auth;
|
namespace EesyPHP\Auth;
|
||||||
|
|
||||||
use EesyPHP\App;
|
use EesyPHP\App;
|
||||||
|
use EesyPHP\Cli;
|
||||||
use EesyPHP\Log;
|
use EesyPHP\Log;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
@ -52,6 +53,7 @@ class Db extends Backend {
|
||||||
'users_table' => 'users',
|
'users_table' => 'users',
|
||||||
'username_field' => 'username',
|
'username_field' => 'username',
|
||||||
'password_field' => 'password',
|
'password_field' => 'password',
|
||||||
|
'password_hash_algo' => 'default',
|
||||||
'exposed_fields' => array('name', 'mail'),
|
'exposed_fields' => array('name', 'mail'),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
@ -67,6 +69,9 @@ class Db extends Backend {
|
||||||
self :: $username_field = App::get('auth.db.username_field', 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 :: $password_field = App::get('auth.db.password_field', null, 'string');
|
||||||
self :: $exposed_fields = App::get('auth.db.exposed_fields', null, 'array');
|
self :: $exposed_fields = App::get('auth.db.exposed_fields', null, 'array');
|
||||||
|
|
||||||
|
if (App :: get('cli.enabled'))
|
||||||
|
Cli :: add_command('add_user', ['\\EesyPHP\\Auth\\Db', 'cli_add_user'], 'Add user');
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,13 +91,11 @@ class Db extends Backend {
|
||||||
public static function get_user($username) {
|
public static function get_user($username) {
|
||||||
self :: connect();
|
self :: connect();
|
||||||
try {
|
try {
|
||||||
$query = self :: $class :: $fpdo -> from(self :: $users_table)
|
$info = self :: $class :: get_one(
|
||||||
-> select(null)
|
self :: $users_table,
|
||||||
-> select(self :: $exposed_fields)
|
[self :: $username_field => $username],
|
||||||
-> where(self :: $username_field, $username);
|
self :: $exposed_fields
|
||||||
|
);
|
||||||
$result = $query -> execute();
|
|
||||||
$info = $result -> fetch();
|
|
||||||
if ($info === false)
|
if ($info === false)
|
||||||
return null;
|
return null;
|
||||||
return new User($username, '\\EesyPHP\\Auth\\Db', $info);
|
return new User($username, '\\EesyPHP\\Auth\\Db', $info);
|
||||||
|
@ -112,20 +115,78 @@ class Db extends Backend {
|
||||||
public static function check_password($user, $password) {
|
public static function check_password($user, $password) {
|
||||||
self :: connect();
|
self :: connect();
|
||||||
try {
|
try {
|
||||||
$query = self :: $class :: $fpdo -> from(self :: $users_table)
|
$info = self :: $class :: get_one(
|
||||||
-> select(null)
|
self :: $users_table,
|
||||||
-> select(self :: $password_field)
|
[self :: $username_field => $user->username],
|
||||||
-> where(self :: $username_field, $user->username);
|
[self :: $password_field]
|
||||||
|
);
|
||||||
$result = $query -> execute();
|
|
||||||
$info = $result -> fetch();
|
|
||||||
if ($info === false)
|
if ($info === false)
|
||||||
return false;
|
return false;
|
||||||
return password_verify($password, $info['password']);
|
return password_verify($password, $info[self :: $password_field]);
|
||||||
}
|
}
|
||||||
catch (Exception $e) {
|
catch (Exception $e) {
|
||||||
Log :: error("Error retrieving user %s password from database: %s", $user, $e->getMessage());
|
Log :: error("Error retrieving user %s password from database: %s", $user, $e->getMessage());
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add user in database
|
||||||
|
* @param array $info User info with at least username, password (clear) and all required exposed
|
||||||
|
* fields
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function add_user($info) {
|
||||||
|
$values = [
|
||||||
|
App::get('auth.db.username_field') => $info['username'],
|
||||||
|
App::get('auth.db.password_field') => password_hash(
|
||||||
|
$info['password'],
|
||||||
|
constant('PASSWORD_'.strtoupper(App::get('auth.db.password_hash_algo')))
|
||||||
|
),
|
||||||
|
];
|
||||||
|
foreach($info as $field => $value) {
|
||||||
|
if (!$value) {
|
||||||
|
Log :: error("add_user: field %s is missing", $field);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach(App :: get('auth.db.exposed_fields') as $field)
|
||||||
|
if (isset($info[$field]) && $info[$field])
|
||||||
|
$values[$field] = $info[$field];
|
||||||
|
|
||||||
|
if (self :: $class :: insert(self :: $users_table, $values)) {
|
||||||
|
Log :: info('add_user(%s): user added', $values['username']);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Log :: error('add_user(%s): error adding user', $values['username']);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CLI command to add user
|
||||||
|
* @param array $command_args Command arguments
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function cli_add_user($command_args) {
|
||||||
|
$info = ['username' => null, 'password' => null];
|
||||||
|
foreach($info as $field => $value) {
|
||||||
|
while(!$value) {
|
||||||
|
$value = Cli::ask_user("Please enter user $field: ", $field == 'password');
|
||||||
|
if (empty($value))
|
||||||
|
print("Invalid value\n");
|
||||||
|
}
|
||||||
|
$info[$field] = $value;
|
||||||
|
}
|
||||||
|
foreach(self :: $exposed_fields as $field) {
|
||||||
|
$value = readline("Please enter user $field: ");
|
||||||
|
if (empty($value))
|
||||||
|
continue;
|
||||||
|
$info[$field] = $value;
|
||||||
|
}
|
||||||
|
if (self :: add_user($info)) {
|
||||||
|
printf("User %s added\n", $info['username']);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Log :: fatal("Error occurred adding user %s", $info['username']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
24
src/Cli.php
24
src/Cli.php
|
@ -444,4 +444,28 @@ Additional parameters:
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method to ask user to enter value from STDIN
|
||||||
|
* @param string $prompt Prompt message
|
||||||
|
* @param boolean $password Set to true if you ask for a password (value not displayed)
|
||||||
|
* @param array $extra_args Extra arguments used to compute prompt message
|
||||||
|
* @return string User input
|
||||||
|
*/
|
||||||
|
public static function ask_user($prompt, $password=false, ...$extra_args) {
|
||||||
|
if ($extra_args)
|
||||||
|
$prompt = call_user_func_array(
|
||||||
|
'sprintf',
|
||||||
|
array_merge(array($prompt), $extra_args)
|
||||||
|
);
|
||||||
|
if ($password) {
|
||||||
|
print($prompt);
|
||||||
|
system('stty -echo');
|
||||||
|
$password = trim(fgets(STDIN));
|
||||||
|
system('stty echo');
|
||||||
|
print("\n");
|
||||||
|
return $password;
|
||||||
|
}
|
||||||
|
return readline($prompt);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue