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 ""
|
msgid ""
|
||||||
msgstr ""
|
msgstr ""
|
||||||
"Project-Id-Version: \n"
|
"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"
|
"PO-Revision-Date: \n"
|
||||||
"Last-Translator: Benjamin Renard <brenard@easter-eggs.com>\n"
|
"Last-Translator: Benjamin Renard <brenard@easter-eggs.com>\n"
|
||||||
"Language-Team: \n"
|
"Language-Team: \n"
|
||||||
|
@ -34,6 +34,33 @@ msgstr "Inconnu"
|
||||||
msgid "Unable to connect to the database."
|
msgid "Unable to connect to the database."
|
||||||
msgstr "Impossible de se connecter à la base de données."
|
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
|
#: Auth/Form.php:51
|
||||||
msgid "Invalid username or password."
|
msgid "Invalid username or password."
|
||||||
msgstr "Nom d'utilisateur ou not de passe invalid."
|
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."
|
msgid "You must login to access this page."
|
||||||
msgstr "Vous devez vous connecter pour accéder à cette 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."
|
msgid "Configuration error in CAS auth backend."
|
||||||
msgstr "Erreur de configuration dans le backend d'authentification CAS."
|
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 "
|
"Une erreur inconnue est survenue. Si le problème persiste, merci de prendre "
|
||||||
"contact avec le support."
|
"contact avec le support."
|
||||||
|
|
||||||
#: Url.php:263
|
#: Url.php:271
|
||||||
msgid ""
|
msgid ""
|
||||||
"Unable to determine the requested page. If the problem persists, please "
|
"Unable to determine the requested page. If the problem persists, please "
|
||||||
"contact support."
|
"contact support."
|
||||||
|
@ -104,7 +131,7 @@ msgstr ""
|
||||||
"Impossible de déterminer la page demandée. Si le problème persiste, merci de "
|
"Impossible de déterminer la page demandée. Si le problème persiste, merci de "
|
||||||
"prendre contact avec le support."
|
"prendre contact avec le support."
|
||||||
|
|
||||||
#: Url.php:411
|
#: Url.php:419
|
||||||
msgid ""
|
msgid ""
|
||||||
"Unable to determine the requested page (loop detected). If the problem "
|
"Unable to determine the requested page (loop detected). If the problem "
|
||||||
"persists, please contact support."
|
"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 "
|
"Impossible de déterminer la page demandée (boucle détectée). Si le problème "
|
||||||
"persiste, merci de prendre contact avec le support."
|
"persiste, merci de prendre contact avec le support."
|
||||||
|
|
||||||
#: Url.php:440
|
#: Url.php:448
|
||||||
msgid "This request cannot be processed."
|
msgid "This request cannot be processed."
|
||||||
msgstr "Cette requête ne peut être traitée."
|
msgstr "Cette requête ne peut être traitée."
|
||||||
|
|
||||||
#: Url.php:450
|
#: Url.php:458
|
||||||
msgid "Authentication required but fail to authenticate you."
|
msgid "Authentication required but fail to authenticate you."
|
||||||
msgstr "Authentication requise mais impossible pour vous authentifier."
|
msgstr "Authentication requise mais impossible pour vous authentifier."
|
||||||
|
|
||||||
#: Url.php:469
|
#: Url.php:479
|
||||||
msgid "This request could not be processed correctly."
|
msgid "This request could not be processed correctly."
|
||||||
msgstr "Cette requête n'a put être traitée correctement."
|
msgstr "Cette requête n'a put être traitée correctement."
|
||||||
|
|
||||||
|
@ -626,10 +653,6 @@ msgstr ""
|
||||||
"\n"
|
"\n"
|
||||||
"%s: %s"
|
"%s: %s"
|
||||||
|
|
||||||
#: templates/login.tpl:19 templates/login.tpl:20
|
|
||||||
msgid "Username"
|
|
||||||
msgstr "Nom d'utilisateur"
|
|
||||||
|
|
||||||
#: templates/login.tpl:24 templates/login.tpl:25
|
#: templates/login.tpl:24 templates/login.tpl:25
|
||||||
msgid "Password"
|
msgid "Password"
|
||||||
msgstr "Not de passe"
|
msgstr "Not de passe"
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
msgid ""
|
msgid ""
|
||||||
msgstr ""
|
msgstr ""
|
||||||
"POT-Creation-Date: 2024-02-21 09:12+0000\n"
|
"POT-Creation-Date: 2024-09-17 07:17+0000\n"
|
||||||
"PO-Revision-Date: 2024-02-21 09:12+0000\n"
|
"PO-Revision-Date: 2024-09-17 07:17+0000\n"
|
||||||
"MIME-Version: 1.0\n"
|
"MIME-Version: 1.0\n"
|
||||||
"Content-Type: text/plain; charset=UTF-8\n"
|
"Content-Type: text/plain; charset=UTF-8\n"
|
||||||
"Content-Transfer-Encoding: 8bit\n"
|
"Content-Transfer-Encoding: 8bit\n"
|
||||||
|
@ -26,6 +26,32 @@ msgstr ""
|
||||||
msgid "Unable to connect to the database."
|
msgid "Unable to connect to the database."
|
||||||
msgstr ""
|
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
|
#: Auth/Form.php:51
|
||||||
msgid "Invalid username or password."
|
msgid "Invalid username or password."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
@ -46,7 +72,7 @@ msgstr ""
|
||||||
msgid "You must login to access this page."
|
msgid "You must login to access this page."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: Auth/Casuser.php:88 Auth/Casuser.php:112
|
#: Auth/Casuser.php:88 Auth/Casuser.php:121
|
||||||
msgid "Configuration error in CAS auth backend."
|
msgid "Configuration error in CAS auth backend."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
@ -84,27 +110,27 @@ msgstr ""
|
||||||
msgid "An unknown error occurred. If problem persist, please contact support."
|
msgid "An unknown error occurred. If problem persist, please contact support."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: Url.php:263
|
#: Url.php:271
|
||||||
msgid ""
|
msgid ""
|
||||||
"Unable to determine the requested page. If the problem persists, please "
|
"Unable to determine the requested page. If the problem persists, please "
|
||||||
"contact support."
|
"contact support."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: Url.php:411
|
#: Url.php:419
|
||||||
msgid ""
|
msgid ""
|
||||||
"Unable to determine the requested page (loop detected). If the problem "
|
"Unable to determine the requested page (loop detected). If the problem "
|
||||||
"persists, please contact support."
|
"persists, please contact support."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: Url.php:440
|
#: Url.php:448
|
||||||
msgid "This request cannot be processed."
|
msgid "This request cannot be processed."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: Url.php:450
|
#: Url.php:458
|
||||||
msgid "Authentication required but fail to authenticate you."
|
msgid "Authentication required but fail to authenticate you."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: Url.php:469
|
#: Url.php:479
|
||||||
msgid "This request could not be processed correctly."
|
msgid "This request could not be processed correctly."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
@ -543,10 +569,6 @@ msgid ""
|
||||||
"%s: %s"
|
"%s: %s"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
#: templates/login.tpl:19 templates/login.tpl:20
|
|
||||||
msgid "Username"
|
|
||||||
msgstr ""
|
|
||||||
|
|
||||||
#: templates/login.tpl:24 templates/login.tpl:25
|
#: templates/login.tpl:24 templates/login.tpl:25
|
||||||
msgid "Password"
|
msgid "Password"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
202
src/Auth/Db.php
202
src/Auth/Db.php
|
@ -4,8 +4,11 @@ namespace EesyPHP\Auth;
|
||||||
|
|
||||||
use EesyPHP\App;
|
use EesyPHP\App;
|
||||||
use EesyPHP\Cli;
|
use EesyPHP\Cli;
|
||||||
|
use EesyPHP\Hook;
|
||||||
|
use EesyPHP\I18n;
|
||||||
use EesyPHP\Log;
|
use EesyPHP\Log;
|
||||||
|
|
||||||
|
use Console_Table;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
use function EesyPHP\vardump;
|
use function EesyPHP\vardump;
|
||||||
|
@ -74,15 +77,16 @@ class Db extends Backend {
|
||||||
|
|
||||||
if (App :: get('cli.enabled')) {
|
if (App :: get('cli.enabled')) {
|
||||||
Cli :: add_command(
|
Cli :: add_command(
|
||||||
'add_user',
|
'user',
|
||||||
['\\EesyPHP\\Auth\\Db', 'cli_add_user'],
|
['\\EesyPHP\\Auth\\Db', 'cli_user'],
|
||||||
'Add user'
|
'Manage users',
|
||||||
);
|
"[list|add|edit|delete]",
|
||||||
Cli :: add_command(
|
[
|
||||||
'edit_user',
|
" list List existing users",
|
||||||
['\\EesyPHP\\Auth\\Db', 'cli_edit_user'],
|
" add Add a user",
|
||||||
'Edit user',
|
" edit [username] Edit a user",
|
||||||
'[username]'
|
" delete [username] Delete a user",
|
||||||
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -178,8 +182,10 @@ class Db extends Backend {
|
||||||
if (isset($info[$field]) && $info[$field])
|
if (isset($info[$field]) && $info[$field])
|
||||||
$values[$field] = $info[$field];
|
$values[$field] = $info[$field];
|
||||||
|
|
||||||
|
Hook :: trigger("user_adding_in_db", $info);
|
||||||
if (self :: $class :: insert(self :: $users_table, $values)) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
Log :: error('add_user(%s): error adding user', $values['username']);
|
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));
|
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 (
|
if (
|
||||||
self :: $class :: update(
|
self :: $class :: update(
|
||||||
self :: $users_table,
|
self :: $users_table,
|
||||||
|
@ -257,6 +267,10 @@ class Db extends Backend {
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
Log :: info('update_user(%s): user updated', $user->username);
|
Log :: info('update_user(%s): user updated', $user->username);
|
||||||
|
Hook :: trigger(
|
||||||
|
"user_updated_in_db",
|
||||||
|
["user" => $user, "changes" => $changes, "raw_changes" => $values]
|
||||||
|
);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Log :: error('update_user(%s): error adding user', $user->username);
|
Log :: error('update_user(%s): error adding user', $user->username);
|
||||||
|
@ -264,67 +278,135 @@ class Db extends Backend {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CLI command to add user
|
* Delete a user in database
|
||||||
* @param array $command_args Command arguments
|
* @param \EesyPHP\Auth\User $user The user object
|
||||||
* @return bool
|
* @return boolean True if user was deleted, false otherwise
|
||||||
*/
|
*/
|
||||||
public static function cli_add_user($command_args) {
|
public static function delete_user($user) {
|
||||||
$info = ['username' => null, 'password' => null];
|
if (!$user->username) {
|
||||||
foreach($info as $field => $value) {
|
Log::error("delete_user(): Invalid user provided (no username)");
|
||||||
while(!$value) {
|
return false;
|
||||||
$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) {
|
Hook :: trigger("user_deleting_in_db", ["user" => $user]);
|
||||||
$value = readline("Please enter user $field: ");
|
if (
|
||||||
if (empty($value))
|
self :: $class :: delete(
|
||||||
continue;
|
self :: $users_table,
|
||||||
$info[$field] = $value;
|
[self :: $username_field => $user->username]
|
||||||
}
|
)
|
||||||
if (self :: add_user($info)) {
|
) {
|
||||||
printf("User %s added\n", $info['username']);
|
Log :: info('delete_user(%s): user updated', $user->username);
|
||||||
|
Hook :: trigger("user_deleted_in_db", ["user" => $user]);
|
||||||
return true;
|
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
|
* @param array $command_args Command arguments
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function cli_edit_user($command_args) {
|
public static function cli_user($command_args) {
|
||||||
if (count($command_args) != 1)
|
if (!$command_args) Cli::usage();
|
||||||
Cli :: usage();
|
switch ($command_args[0]) {
|
||||||
$username = $command_args[0];
|
case "list":
|
||||||
$user = self :: get_user($username);
|
$users = self :: $class :: get_many(
|
||||||
if (!$user) Cli :: usage("Invalid user '$username'");
|
self :: $users_table,
|
||||||
$changes = [
|
null,
|
||||||
"username" => trim(
|
array_merge([self :: $username_field], self :: $exposed_fields),
|
||||||
Cli::ask_user("Please enter user new username [$username]: ")
|
self :: $username_field
|
||||||
),
|
);
|
||||||
"password" => Cli::ask_user("Please enter user new password [empty = unchange]: ", true),
|
if ($users === false)
|
||||||
];
|
Log :: fatal(I18n::_("Failed to list users from database."));
|
||||||
if (!$changes["username"]) unset($changes["username"]);
|
|
||||||
if (!$changes["password"]) unset($changes["password"]);
|
|
||||||
|
|
||||||
foreach(self :: $exposed_fields as $field) {
|
if (!$users) {
|
||||||
$value = Cli::ask_user("Please enter user $field [{$user[$field]}]: ");
|
print(I18n::_("No user found."));
|
||||||
if (empty($value))
|
return true;
|
||||||
continue;
|
}
|
||||||
$changes[$field] = $value;
|
|
||||||
|
$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