- Timestamp:
- Jan 26, 2023 4:37:24 PM (16 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bin/user.cli.php
r771 r779 89 89 case 'list': 90 90 $users = User_CLI::getList(); 91 $positions = "%-3s %-15s %- 11s %-15s\n";91 $positions = "%-3s %-15s %-30s %-11s %-15s\n"; 92 92 printf($positions, 93 93 'ID', 94 94 'USERNAME', 95 'EMAIL', 95 96 'LAST_ACCESS', 96 97 'LAST_IP' … … 100 101 $u[$auth->getParam('db_primary_key')], 101 102 $u['username'], 103 $u['email'], 102 104 date($app->getParam('date_format'), strtotime($u['last_access_datetime'])), 103 105 $u['last_login_ip'] … … 108 110 case 'create': 109 111 $username = User_CLI::getArg(2, 'username'); 110 $password = User_CLI::getArg(3, 'password'); 112 $password = User_CLI::getArg(3, 'password', $auth->generatePassword()); 113 $email = User_CLI::getArg(4, 'email', ''); 111 114 $user_id = $auth->getUserID($username); 112 115 if (false !== $auth->getUserID($username)) { 113 printf("User '%s' already exists. Use 'update'instead.\n", $username);116 printf("User `%s` already exists. Use `update` instead.\n", $username); 114 117 exit(1); 115 118 } 116 $user_id = User_CLI::create($username, $password );117 printf("Created user '%s' (user_id %s).\n", $username, $user_id);119 $user_id = User_CLI::create($username, $password, $email); 120 printf("Created user `%s` with password `%s` (user_id %s).\n", $username, $password, $user_id); 118 121 break; 119 122 120 123 case 'update': 121 124 $username = User_CLI::getArg(2, 'username'); 122 $password = User_CLI::getArg(3, 'password' );125 $password = User_CLI::getArg(3, 'password', $auth->generatePassword()); 123 126 if (!$user_id = $auth->getUserID($username)) { 124 printf("User '%s' not found. Use 'create'first.\n", $username);127 printf("User `%s` not found. Use `create` first.\n", $username); 125 128 exit(1); 126 129 } 127 130 $auth->setPassword($user_id, $password); 128 printf("Updated user '%s' password (user_id %s).\n", $username, $user_id);131 printf("Updated user `%s` with password `%s` (user_id %s).\n", $username, $password, $user_id); 129 132 break; 130 133 … … 132 135 $username = User_CLI::getArg(2, 'username'); 133 136 if (!$user_id = $auth->getUserID($username)) { 134 printf("User '%s'not found.\n", $username);137 printf("User `%s` not found.\n", $username); 135 138 exit(1); 136 139 } 137 140 User_CLI::remove($username); 138 printf("Removed user '%s'(user_id %s).\n", $username, $user_id);141 printf("Removed user `%s` (user_id %s).\n", $username, $user_id); 139 142 break; 140 143 … … 157 160 class User_CLI 158 161 { 159 static public function getArg($pos, $name) 160 { 161 if (!isset($_SERVER['argv'][$pos]) || $_SERVER['argv'][$pos] == '') { 162 // Required arguments missing. 162 public static function getArg($pos, $name, $default=null) 163 { 164 if (isset($_SERVER['argv'][$pos]) && $_SERVER['argv'][$pos] != '') { 165 return $_SERVER['argv'][$pos]; 166 } 167 168 if (null === $default) { 163 169 printf("Required argument %s is missing. Lost? Try `%s help`.\n", strtoupper($name), basename($_SERVER['argv'][0])); 164 170 exit(1); 165 171 } 166 172 167 return $ _SERVER['argv'][$pos];168 } 169 170 static public function getList()173 return $default; 174 } 175 176 public static function getList() 171 177 { 172 178 global $auth, $db; … … 185 191 } 186 192 187 static public function create($username, $password)193 public static function create($username, $password, $email) 188 194 { 189 195 global $auth, $db; … … 199 205 $addtl_cols[] = ", account_id"; 200 206 $addtl_vals[] = ", '1'"; 207 } 208 if (in_array('email', $cols) && '' != $email) { 209 $addtl_cols[] = ", email"; 210 $addtl_vals[] = sprintf(", '%s'", $db->escapeString($email)); 201 211 } 202 212 $db->query(" … … 215 225 } 216 226 217 static public function remove($username)227 public static function remove($username) 218 228 { 219 229 global $auth, $db; … … 225 235 } 226 236 227 static public function usage()237 public static function usage() 228 238 { 229 239 ?> … … 234 244 COMMANDS 235 245 236 help Display this help 237 list List all users. 238 create USERNAME PASSWORD Create a user USERNAME authenticated by PASSWORD. 239 update USERNAME PASSWORD Update the password for user USERNAME to PASSWORD. 246 help Display this help 247 list List all users. 248 create USERNAME [PASSWORD] [EMAIL] Create a user USERNAME authenticated by PASSWORD. 249 update USERNAME [PASSWORD] Update the password for user USERNAME to PASSWORD. 250 251 If PASSWORD is not given, a random password will be generated and printed to the screen. 240 252 241 253 This script must be run in a common site directory configured with a DB auth file,
Note: See TracChangeset
for help on using the changeset viewer.