Auth\Db: replace add_user/edit_user CLI commands by user CLI command with subcommands
This commit is contained in:
parent
74dc4bb608
commit
e6f3fc835e
5 changed files with 211 additions and 84 deletions
File diff suppressed because one or more lines are too long
Binary file not shown.
|
@ -1,7 +1,7 @@
|
|||
msgid ""
|
||||
msgstr ""
|
||||
"Project-Id-Version: \n"
|
||||
"POT-Creation-Date: 2024-02-21 09:12+0000\n"
|
||||
"POT-Creation-Date: 2024-09-17 07:17+0000\n"
|
||||
"PO-Revision-Date: \n"
|
||||
"Last-Translator: Benjamin Renard <brenard@easter-eggs.com>\n"
|
||||
"Language-Team: \n"
|
||||
|
@ -34,6 +34,33 @@ msgstr "Inconnu"
|
|||
msgid "Unable to connect to the database."
|
||||
msgstr "Impossible de se connecter à la base de données."
|
||||
|
||||
#: Auth/Db.php:315
|
||||
msgid "Failed to list users from database."
|
||||
msgstr ""
|
||||
"Une erreur est survenue en listant les utilisateurs dans la base de données."
|
||||
|
||||
#: Auth/Db.php:318
|
||||
msgid "No user found."
|
||||
msgstr "Aucun utilisateur."
|
||||
|
||||
#: Auth/Db.php:323
|
||||
msgid "Username"
|
||||
msgstr "Nom d'utilisateur"
|
||||
|
||||
#: Auth/Db.php:334
|
||||
#, php-format
|
||||
msgid "%d users(s)"
|
||||
msgstr "%d utilisateur(s)"
|
||||
|
||||
#: Auth/Db.php:361 Auth/Db.php:392
|
||||
msgid "Username is missing"
|
||||
msgstr "Nom d'utilisateur manquant"
|
||||
|
||||
#: Auth/Db.php:403
|
||||
#, php-format
|
||||
msgid "Unknown subcommand %s"
|
||||
msgstr "Sous-commande %s inconnue"
|
||||
|
||||
#: Auth/Form.php:51
|
||||
msgid "Invalid username or password."
|
||||
msgstr "Nom d'utilisateur ou not de passe invalid."
|
||||
|
@ -54,7 +81,7 @@ msgstr "Accès interdit"
|
|||
msgid "You must login to access this page."
|
||||
msgstr "Vous devez vous connecter pour accéder à cette page."
|
||||
|
||||
#: Auth/Casuser.php:88 Auth/Casuser.php:112
|
||||
#: Auth/Casuser.php:88 Auth/Casuser.php:121
|
||||
msgid "Configuration error in CAS auth backend."
|
||||
msgstr "Erreur de configuration dans le backend d'authentification CAS."
|
||||
|
||||
|
@ -96,7 +123,7 @@ msgstr ""
|
|||
"Une erreur inconnue est survenue. Si le problème persiste, merci de prendre "
|
||||
"contact avec le support."
|
||||
|
||||
#: Url.php:263
|
||||
#: Url.php:271
|
||||
msgid ""
|
||||
"Unable to determine the requested page. If the problem persists, please "
|
||||
"contact support."
|
||||
|
@ -104,7 +131,7 @@ msgstr ""
|
|||
"Impossible de déterminer la page demandée. Si le problème persiste, merci de "
|
||||
"prendre contact avec le support."
|
||||
|
||||
#: Url.php:411
|
||||
#: Url.php:419
|
||||
msgid ""
|
||||
"Unable to determine the requested page (loop detected). If the problem "
|
||||
"persists, please contact support."
|
||||
|
@ -112,15 +139,15 @@ msgstr ""
|
|||
"Impossible de déterminer la page demandée (boucle détectée). Si le problème "
|
||||
"persiste, merci de prendre contact avec le support."
|
||||
|
||||
#: Url.php:440
|
||||
#: Url.php:448
|
||||
msgid "This request cannot be processed."
|
||||
msgstr "Cette requête ne peut être traitée."
|
||||
|
||||
#: Url.php:450
|
||||
#: Url.php:458
|
||||
msgid "Authentication required but fail to authenticate you."
|
||||
msgstr "Authentication requise mais impossible pour vous authentifier."
|
||||
|
||||
#: Url.php:469
|
||||
#: Url.php:479
|
||||
msgid "This request could not be processed correctly."
|
||||
msgstr "Cette requête n'a put être traitée correctement."
|
||||
|
||||
|
@ -626,10 +653,6 @@ msgstr ""
|
|||
"\n"
|
||||
"%s: %s"
|
||||
|
||||
#: templates/login.tpl:19 templates/login.tpl:20
|
||||
msgid "Username"
|
||||
msgstr "Nom d'utilisateur"
|
||||
|
||||
#: templates/login.tpl:24 templates/login.tpl:25
|
||||
msgid "Password"
|
||||
msgstr "Not de passe"
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
msgid ""
|
||||
msgstr ""
|
||||
"POT-Creation-Date: 2024-02-21 09:12+0000\n"
|
||||
"PO-Revision-Date: 2024-02-21 09:12+0000\n"
|
||||
"POT-Creation-Date: 2024-09-17 07:17+0000\n"
|
||||
"PO-Revision-Date: 2024-09-17 07:17+0000\n"
|
||||
"MIME-Version: 1.0\n"
|
||||
"Content-Type: text/plain; charset=UTF-8\n"
|
||||
"Content-Transfer-Encoding: 8bit\n"
|
||||
|
@ -26,6 +26,32 @@ msgstr ""
|
|||
msgid "Unable to connect to the database."
|
||||
msgstr ""
|
||||
|
||||
#: Auth/Db.php:315
|
||||
msgid "Failed to list users from database."
|
||||
msgstr ""
|
||||
|
||||
#: Auth/Db.php:318
|
||||
msgid "No user found."
|
||||
msgstr ""
|
||||
|
||||
#: Auth/Db.php:323
|
||||
msgid "Username"
|
||||
msgstr ""
|
||||
|
||||
#: Auth/Db.php:334
|
||||
#, php-format
|
||||
msgid "%d users(s)"
|
||||
msgstr ""
|
||||
|
||||
#: Auth/Db.php:361 Auth/Db.php:392
|
||||
msgid "Username is missing"
|
||||
msgstr ""
|
||||
|
||||
#: Auth/Db.php:403
|
||||
#, php-format
|
||||
msgid "Unknown subcommand %s"
|
||||
msgstr ""
|
||||
|
||||
#: Auth/Form.php:51
|
||||
msgid "Invalid username or password."
|
||||
msgstr ""
|
||||
|
@ -46,7 +72,7 @@ msgstr ""
|
|||
msgid "You must login to access this page."
|
||||
msgstr ""
|
||||
|
||||
#: Auth/Casuser.php:88 Auth/Casuser.php:112
|
||||
#: Auth/Casuser.php:88 Auth/Casuser.php:121
|
||||
msgid "Configuration error in CAS auth backend."
|
||||
msgstr ""
|
||||
|
||||
|
@ -84,27 +110,27 @@ msgstr ""
|
|||
msgid "An unknown error occurred. If problem persist, please contact support."
|
||||
msgstr ""
|
||||
|
||||
#: Url.php:263
|
||||
#: Url.php:271
|
||||
msgid ""
|
||||
"Unable to determine the requested page. If the problem persists, please "
|
||||
"contact support."
|
||||
msgstr ""
|
||||
|
||||
#: Url.php:411
|
||||
#: Url.php:419
|
||||
msgid ""
|
||||
"Unable to determine the requested page (loop detected). If the problem "
|
||||
"persists, please contact support."
|
||||
msgstr ""
|
||||
|
||||
#: Url.php:440
|
||||
#: Url.php:448
|
||||
msgid "This request cannot be processed."
|
||||
msgstr ""
|
||||
|
||||
#: Url.php:450
|
||||
#: Url.php:458
|
||||
msgid "Authentication required but fail to authenticate you."
|
||||
msgstr ""
|
||||
|
||||
#: Url.php:469
|
||||
#: Url.php:479
|
||||
msgid "This request could not be processed correctly."
|
||||
msgstr ""
|
||||
|
||||
|
@ -543,10 +569,6 @@ msgid ""
|
|||
"%s: %s"
|
||||
msgstr ""
|
||||
|
||||
#: templates/login.tpl:19 templates/login.tpl:20
|
||||
msgid "Username"
|
||||
msgstr ""
|
||||
|
||||
#: templates/login.tpl:24 templates/login.tpl:25
|
||||
msgid "Password"
|
||||
msgstr ""
|
||||
|
|
202
src/Auth/Db.php
202
src/Auth/Db.php
|
@ -4,8 +4,11 @@ namespace EesyPHP\Auth;
|
|||
|
||||
use EesyPHP\App;
|
||||
use EesyPHP\Cli;
|
||||
use EesyPHP\Hook;
|
||||
use EesyPHP\I18n;
|
||||
use EesyPHP\Log;
|
||||
|
||||
use Console_Table;
|
||||
use Exception;
|
||||
|
||||
use function EesyPHP\vardump;
|
||||
|
@ -74,15 +77,16 @@ class Db extends Backend {
|
|||
|
||||
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]'
|
||||
'user',
|
||||
['\\EesyPHP\\Auth\\Db', 'cli_user'],
|
||||
'Manage users',
|
||||
"[list|add|edit|delete]",
|
||||
[
|
||||
" list List existing users",
|
||||
" add Add a user",
|
||||
" edit [username] Edit a user",
|
||||
" delete [username] Delete a user",
|
||||
]
|
||||
);
|
||||
}
|
||||
return true;
|
||||
|
@ -178,8 +182,10 @@ class Db extends Backend {
|
|||
if (isset($info[$field]) && $info[$field])
|
||||
$values[$field] = $info[$field];
|
||||
|
||||
Hook :: trigger("user_adding_in_db", $info);
|
||||
if (self :: $class :: insert(self :: $users_table, $values)) {
|
||||
Log :: info('add_user(%s): user added', $values['username']);
|
||||
Log :: info('add_user(%s): user added', $info['username']);
|
||||
Hook :: trigger("user_added_in_db", $info);
|
||||
return true;
|
||||
}
|
||||
Log :: error('add_user(%s): error adding user', $values['username']);
|
||||
|
@ -249,6 +255,10 @@ class Db extends Backend {
|
|||
}
|
||||
|
||||
Log::debug("update_user(%s): changes=%s", $user->username, vardump($values));
|
||||
Hook :: trigger(
|
||||
"user_updating_in_db",
|
||||
["user" => $user, "changes" => $changes, "raw_changes" => $values]
|
||||
);
|
||||
if (
|
||||
self :: $class :: update(
|
||||
self :: $users_table,
|
||||
|
@ -257,6 +267,10 @@ class Db extends Backend {
|
|||
)
|
||||
) {
|
||||
Log :: info('update_user(%s): user updated', $user->username);
|
||||
Hook :: trigger(
|
||||
"user_updated_in_db",
|
||||
["user" => $user, "changes" => $changes, "raw_changes" => $values]
|
||||
);
|
||||
return true;
|
||||
}
|
||||
Log :: error('update_user(%s): error adding user', $user->username);
|
||||
|
@ -264,67 +278,135 @@ class Db extends Backend {
|
|||
}
|
||||
|
||||
/**
|
||||
* CLI command to add user
|
||||
* @param array $command_args Command arguments
|
||||
* @return bool
|
||||
* Delete a user in database
|
||||
* @param \EesyPHP\Auth\User $user The user object
|
||||
* @return boolean True if user was deleted, false otherwise
|
||||
*/
|
||||
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;
|
||||
public static function delete_user($user) {
|
||||
if (!$user->username) {
|
||||
Log::error("delete_user(): Invalid user provided (no username)");
|
||||
return false;
|
||||
}
|
||||
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']);
|
||||
Hook :: trigger("user_deleting_in_db", ["user" => $user]);
|
||||
if (
|
||||
self :: $class :: delete(
|
||||
self :: $users_table,
|
||||
[self :: $username_field => $user->username]
|
||||
)
|
||||
) {
|
||||
Log :: info('delete_user(%s): user updated', $user->username);
|
||||
Hook :: trigger("user_deleted_in_db", ["user" => $user]);
|
||||
return true;
|
||||
}
|
||||
Log :: fatal("Error occurred adding user %s", $info['username']);
|
||||
Log :: error('delete_user(%s): error adding user', $user->username);
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* CLI command to edit an existing user
|
||||
* CLI command to manage users
|
||||
* @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"]);
|
||||
public static function cli_user($command_args) {
|
||||
if (!$command_args) Cli::usage();
|
||||
switch ($command_args[0]) {
|
||||
case "list":
|
||||
$users = self :: $class :: get_many(
|
||||
self :: $users_table,
|
||||
null,
|
||||
array_merge([self :: $username_field], self :: $exposed_fields),
|
||||
self :: $username_field
|
||||
);
|
||||
if ($users === false)
|
||||
Log :: fatal(I18n::_("Failed to list users from database."));
|
||||
|
||||
foreach(self :: $exposed_fields as $field) {
|
||||
$value = Cli::ask_user("Please enter user $field [{$user[$field]}]: ");
|
||||
if (empty($value))
|
||||
continue;
|
||||
$changes[$field] = $value;
|
||||
if (!$users) {
|
||||
print(I18n::_("No user found."));
|
||||
return true;
|
||||
}
|
||||
|
||||
$tbl = new Console_Table();
|
||||
$headers = [I18n::_("Username")];
|
||||
foreach(self :: $exposed_fields as $field)
|
||||
$headers[] = mb_convert_case(str_replace("_", " ", $field), MB_CASE_TITLE);
|
||||
$tbl->setHeaders($headers);
|
||||
foreach($users as $user) {
|
||||
$row = [$user[self::$username_field]];
|
||||
foreach(self :: $exposed_fields as $field)
|
||||
$row[] = $user[$field]?strval($user[$field]):"";
|
||||
$tbl->addRow($row);
|
||||
}
|
||||
echo $tbl->getTable();
|
||||
echo "\n".sprintf(_("%d users(s)"), count($users))."\n";
|
||||
return true;
|
||||
|
||||
case "add":
|
||||
$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']);
|
||||
|
||||
case "edit":
|
||||
if (count($command_args) < 2)
|
||||
Cli :: usage(I18n::_("Username is missing"));
|
||||
$username = $command_args[1];
|
||||
$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, true)) {
|
||||
printf("User %s updated\n", $username);
|
||||
return true;
|
||||
}
|
||||
Log :: fatal("Error occurred updating user %s", $username);
|
||||
|
||||
case "delete":
|
||||
if (count($command_args) < 2)
|
||||
Cli :: usage(I18n::_("Username is missing"));
|
||||
$username = $command_args[1];
|
||||
$user = self :: get_user($username);
|
||||
if (!$user) Cli :: usage("Invalid user '$username'");
|
||||
if (self :: delete_user($user)) {
|
||||
printf("User %s deleted\n", $username);
|
||||
return true;
|
||||
}
|
||||
Log :: fatal("Error occurred deleting user %s", $username);
|
||||
|
||||
default:
|
||||
Cli::usage(I18n::_("Unknown subcommand %s"), $command_args[0]);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue