diff --git a/src/Auth/Db.php b/src/Auth/Db.php index fb6a52a..b2e17e3 100644 --- a/src/Auth/Db.php +++ b/src/Auth/Db.php @@ -72,8 +72,19 @@ class Db extends Backend { 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'); + if (App :: get('cli.enabled')) { + Cli :: add_command( + 'add_user', + ['\\EesyPHP\\Auth\\Db', 'cli_add_user'], + 'Add user' + ); + Cli :: add_command( + 'edit_user', + ['\\EesyPHP\\Auth\\Db', 'cli_edit_user'], + 'Edit user', + '[username]' + ); + } return true; } @@ -279,4 +290,41 @@ class Db extends Backend { } Log :: fatal("Error occurred adding user %s", $info['username']); } + + /** + * CLI command to edit an existing user + * @param array $command_args Command arguments + * @return bool + */ + public static function cli_edit_user($command_args) { + if (count($command_args) != 1) + Cli :: usage(); + $username = $command_args[0]; + $user = self :: get_user($username); + if (!$user) Cli :: usage("Invalid user '$username'"); + $changes = [ + "username" => trim( + Cli::ask_user("Please enter user new username [$username]: ") + ), + "password" => Cli::ask_user("Please enter user new password [empty = unchange]: ", true), + ]; + if (!$changes["username"]) unset($changes["username"]); + if (!$changes["password"]) unset($changes["password"]); + + foreach(self :: $exposed_fields as $field) { + $value = Cli::ask_user("Please enter user $field [{$user[$field]}]: "); + if (empty($value)) + continue; + $changes[$field] = $value; + } + if (!$changes) { + print("No change.\n"); + return true; + } + if (self :: update_user($user, $changes)) { + printf("User %s updated\n", $username); + return true; + } + Log :: fatal("Error occurred updating user %s", $username); + } }