From 6c74b2a7192e0025d05a121332f2313a4d374088 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Sun, 4 Feb 2024 11:05:03 +0100 Subject: [PATCH] Auth Db: add add_user method ad CLI command --- src/Auth/Db.php | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ src/Cli.php | 24 ++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/src/Auth/Db.php b/src/Auth/Db.php index edd280b..b3b288e 100644 --- a/src/Auth/Db.php +++ b/src/Auth/Db.php @@ -3,6 +3,7 @@ namespace EesyPHP\Auth; use EesyPHP\App; +use EesyPHP\Cli; use EesyPHP\Log; use Exception; @@ -52,6 +53,7 @@ class Db extends Backend { 'users_table' => 'users', 'username_field' => 'username', 'password_field' => 'password', + 'password_hash_algo' => 'default', '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 :: $password_field = App::get('auth.db.password_field', null, 'string'); 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; } @@ -124,4 +129,64 @@ class Db extends Backend { } 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']); + } } diff --git a/src/Cli.php b/src/Cli.php index 7799ffc..02495d4 100644 --- a/src/Cli.php +++ b/src/Cli.php @@ -444,4 +444,28 @@ Additional parameters: 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); + } + }