Auth\Db: add edit_user CLI command

This commit is contained in:
Benjamin Renard 2024-09-13 18:52:05 +02:00
parent bfac58c43a
commit 7b2ce7ab73
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -72,8 +72,19 @@ class Db extends Backend {
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')) if (App :: get('cli.enabled')) {
Cli :: add_command('add_user', ['\\EesyPHP\\Auth\\Db', 'cli_add_user'], 'Add user'); 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; return true;
} }
@ -279,4 +290,41 @@ class Db extends Backend {
} }
Log :: fatal("Error occurred adding user %s", $info['username']); 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);
}
} }