diff options
author | Andrew Dolgov <[email protected]> | 2022-06-10 13:39:00 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2022-06-10 13:39:00 +0300 |
commit | cf1eaeedf3026948cf2210af1c747bdc3522d6ff (patch) | |
tree | 31780f12fb075b6b37be7a8f2fb813ba9db4980a /update.php | |
parent | 2975c7297b680e486f326939b9fba82d8cf18035 (diff) |
* add UserHelper methods to manipulate user database (add, modify, delete)
* expose said methods via CLI (update.php)
* fix several invocations of deprecated functions
* set stricter type hints on several method arguments
Diffstat (limited to 'update.php')
-rwxr-xr-x | update.php | 133 |
1 files changed, 126 insertions, 7 deletions
diff --git a/update.php b/update.php index f33458c2e..10f7f230a 100755 --- a/update.php +++ b/update.php @@ -99,8 +99,12 @@ "opml-export:" => ["USER:FILE", "export OPML of USER to FILE"], "opml-import:" => ["USER:FILE", "import OPML for USER from FILE"], "user-list" => "list all users", -# "user-add:" => ["USER[:PASSWORD]", "add USER, optionally without prompting for PASSWORD"], -# "user-remove:" => ["USERNAME", "remove specified user"], + "user-add:" => ["USER[:PASSWORD[:ACCESS_LEVEL=0]]", "add USER, prompts for password if unset"], + "user-remove:" => ["USERNAME", "remove USER"], + "user-set-password:" => ["USER:PASSWORD", "sets PASSWORD of specified USER"], + "user-set-access-level:" => ["USER:LEVEL", "sets access LEVEL of specified USER"], + "user-exists:" => ["USER", "returns 0 if specified USER exists in the database"], + "force-yes" => "assume 'yes' to all queries", "help" => "", ]; @@ -150,7 +154,7 @@ Debug::set_enabled(true); if (isset($options["log-level"])) { - Debug::set_loglevel((int)$options["log-level"]); + Debug::set_loglevel(Debug::map_loglevel((int)$options["log-level"])); } if (isset($options["log"])) { @@ -159,7 +163,7 @@ Debug::log("Logging to " . $options["log"]); } else { if (isset($options['quiet'])) { - Debug::set_loglevel(Debug::$LOG_DISABLED); + Debug::set_loglevel(Debug::LOG_DISABLED); } } @@ -265,7 +269,7 @@ if (isset($options["update-schema"])) { if (Config::is_migration_needed()) { - if ($options["update-schema"] != "force-yes") { + if (!isset($options['force-yes']) || $options["update-schema"] != "force-yes") { Debug::log("Type 'yes' to continue."); if (read_stdin() != 'yes') @@ -275,7 +279,7 @@ } if (!isset($options["log-level"])) { - Debug::set_loglevel(Debug::$LOG_VERBOSE); + Debug::set_loglevel(Debug::LOG_VERBOSE); } $migrations = Config::get_migrations(); @@ -352,7 +356,7 @@ if (isset($options["force-refetch"])) $_REQUEST["force_refetch"] = true; if (isset($options["force-rehash"])) $_REQUEST["force_rehash"] = true; - Debug::set_loglevel(Debug::$LOG_EXTENDED); + Debug::set_loglevel(Debug::LOG_EXTENDED); $rc = RSSUtils::update_rss_feed($feed) != false ? 0 : 1; @@ -407,6 +411,121 @@ } + if (isset($options["user-add"])) { + list ($login, $password, $access_level) = explode(":", $options["user-add"], 3); + + $uid = UserHelper::find_user_by_login($login); + + if ($uid) { + Debug::log("Error: User already exists: $login"); + exit(1); + } + + if (!$access_level) + $access_level = UserHelper::ACCESS_LEVEL_USER; + + if (!in_array($access_level, UserHelper::ACCESS_LEVELS)) { + Debug::log("Error: Invalid access level value: $access_level"); + exit(1); + } + + if (!$password) { + Debug::log("Please enter password for user $login: "); + $password = read_stdin(); + + if (!$password) { + Debug::log("Error: password may not be blank."); + exit(1); + } + } + + Debug::log("Adding user $login with access level $access_level..."); + + if (UserHelper::user_add($login, $password, $access_level)) { + Debug::log("Success."); + } else { + Debug::log("Operation failed, check the logs for more information."); + } + } + + if (isset($options["user-set-password"])) { + list ($login, $password) = explode(":", $options["user-set-password"], 2); + + $uid = UserHelper::find_user_by_login($login); + + if (!$uid) { + Debug::log("Error: User not found: $login"); + exit(1); + } + + Debug::log("Changing password of user $login..."); + + if (UserHelper::user_modify($uid, $password)) { + Debug::log("Success."); + } else { + Debug::log("Operation failed, check the logs for more information."); + } + } + + if (isset($options["user-set-access-level"])) { + list ($login, $access_level) = explode(":", $options["user-set-access-level"], 2); + + $uid = UserHelper::find_user_by_login($login); + + if (!$uid) { + Debug::log("Error: User not found: $login"); + exit(1); + } + + if (!in_array($access_level, UserHelper::ACCESS_LEVELS)) { + Debug::log("Error: Invalid access level value: $access_level"); + exit(1); + } + + Debug::log("Changing access level of user $login..."); + + if (UserHelper::user_modify($uid, '', UserHelper::map_access_level((int)$access_level))) { + Debug::log("Success."); + } else { + Debug::log("Operation failed, check the logs for more information."); + } + } + + if (isset($options["user-remove"])) { + $login = $options["user-remove"]; + + $uid = UserHelper::find_user_by_login($login); + + if (!$uid) { + Debug::log("Error: User not found: $login"); + exit(1); + } + + if (!isset($options['force-yes'])) { + Debug::log("About to remove user $login. Type 'yes' to continue."); + + if (read_stdin() != 'yes') + exit(1); + } + + Debug::log("Removing user $login..."); + + if (UserHelper::user_delete($uid)) { + Debug::log("Success."); + } else { + Debug::log("Operation failed, check the logs for more information."); + } + } + + if (isset($options["user-exists"])) { + $login = $options["user-exists"]; + + if (UserHelper::find_user_by_login($login)) + exit(0); + else + exit(1); + } + PluginHost::getInstance()->run_commands($options); if (file_exists(Config::get(Config::LOCK_DIRECTORY) . "/$lock_filename")) |