Changeset 779


Ignore:
Timestamp:
Jan 26, 2023 4:37:24 PM (15 months ago)
Author:
anonymous
Message:

Allow passing an email address to user.cli.php

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bin/user.cli.php

    r771 r779  
    8989case 'list':
    9090    $users = User_CLI::getList();
    91     $positions = "%-3s  %-15s  %-11s  %-15s\n";
     91    $positions = "%-3s  %-15s  %-30s  %-11s  %-15s\n";
    9292    printf($positions,
    9393        'ID',
    9494        'USERNAME',
     95        'EMAIL',
    9596        'LAST_ACCESS',
    9697        'LAST_IP'
     
    100101            $u[$auth->getParam('db_primary_key')],
    101102            $u['username'],
     103            $u['email'],
    102104            date($app->getParam('date_format'), strtotime($u['last_access_datetime'])),
    103105            $u['last_login_ip']
     
    108110case 'create':
    109111    $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', '');
    111114    $user_id = $auth->getUserID($username);
    112115    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);
    114117        exit(1);
    115118    }
    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);
    118121    break;
    119122
    120123case 'update':
    121124    $username = User_CLI::getArg(2, 'username');
    122     $password = User_CLI::getArg(3, 'password');
     125    $password = User_CLI::getArg(3, 'password', $auth->generatePassword());
    123126    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);
    125128        exit(1);
    126129    }
    127130    $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);
    129132    break;
    130133
     
    132135    $username = User_CLI::getArg(2, 'username');
    133136    if (!$user_id = $auth->getUserID($username)) {
    134         printf("User '%s' not found.\n", $username);
     137        printf("User `%s` not found.\n", $username);
    135138        exit(1);
    136139    }
    137140    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);
    139142    break;
    140143
     
    157160class User_CLI
    158161{
    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) {
    163169            printf("Required argument %s is missing. Lost? Try `%s help`.\n", strtoupper($name), basename($_SERVER['argv'][0]));
    164170            exit(1);
    165171        }
    166172
    167         return $_SERVER['argv'][$pos];
    168     }
    169 
    170     static public function getList()
     173        return $default;
     174    }
     175
     176    public static function getList()
    171177    {
    172178        global $auth, $db;
     
    185191    }
    186192
    187     static public function create($username, $password)
     193    public static function create($username, $password, $email)
    188194    {
    189195        global $auth, $db;
     
    199205            $addtl_cols[] = ", account_id";
    200206            $addtl_vals[] = ", '1'";
     207        }
     208        if (in_array('email', $cols) && '' != $email) {
     209            $addtl_cols[] = ", email";
     210            $addtl_vals[] = sprintf(", '%s'", $db->escapeString($email));
    201211        }
    202212        $db->query("
     
    215225    }
    216226
    217     static public function remove($username)
     227    public static function remove($username)
    218228    {
    219229        global $auth, $db;
     
    225235    }
    226236
    227     static public function usage()
     237    public static function usage()
    228238    {
    229239        ?>
     
    234244COMMANDS
    235245
    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
     251If PASSWORD is not given, a random password will be generated and printed to the screen.
    240252
    241253This 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.