commit d83688db5b6457541bc42238270a4ab28af2701e Author: Paul Greidanus Date: Thu Jun 30 11:27:52 2016 -0600 Adding CLI driven support for password resets diff --git a/admin/cli/reset_password.php b/admin/cli/reset_password.php index 27f4ca9..1c05f75 100644 --- a/admin/cli/reset_password.php +++ b/admin/cli/reset_password.php @@ -26,13 +26,14 @@ define('CLI_SCRIPT', true); -require(__DIR__.'/../../config.php'); +require(dirname(dirname(dirname(__FILE__))).'/config.php'); require_once($CFG->libdir.'/clilib.php'); // cli only functions // now get cli options -list($options, $unrecognized) = cli_get_params(array('help'=>false), - array('h'=>'help')); +list($options, $unrecognized) = cli_get_params(array('help'=>false, 'username'=>'', + 'password'=>'', 'badpassword' => false), + array('h'=>'help','u'=>'username', 'p'=>'password')); if ($unrecognized) { $unrecognized = implode("\n ", $unrecognized); @@ -47,7 +48,10 @@ There are no security checks here because anybody who is able to execute this file may execute any PHP too. Options: --h, --help Print out this help +-h, --help Print out this help +-u, --username=username Specify username to change +-p, --password=newpass Specify new password +--badpassword Allow the user of a bad password - ignoring password policies - not recommended Example: \$sudo -u www-data /usr/bin/php admin/cli/reset_password.php @@ -56,20 +60,32 @@ Example: echo $help; die; } -cli_heading('Password reset'); // TODO: localize -$prompt = "enter username (manual authentication only)"; // TODO: localize -$username = cli_input($prompt); + +if ($options['username'] == '') { + cli_heading('Password reset'); // TODO: localize + $prompt = "enter username (manual authentication only)"; // TODO: localize + $username = cli_input($prompt); +} else { + $username = $options['username']; +} if (!$user = $DB->get_record('user', array('auth'=>'manual', 'username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) { cli_error("Can not find user '$username'"); } -$prompt = "Enter new password"; // TODO: localize -$password = cli_input($prompt); +if ($options['password'] == '' ) { + $prompt = "Enter new password"; // TODO: localize + $password = cli_input($prompt); +} else { + $password = $options['password']; +} $errmsg = '';//prevent eclipse warning -if (!check_password_policy($password, $errmsg)) { - cli_error($errmsg); + +if (!$options['badpassword'] ) { + if (!check_password_policy($password, $errmsg)) { + cli_error($errmsg); + } } $hashedpassword = hash_internal_user_password($password); @@ -78,4 +94,4 @@ $DB->set_field('user', 'password', $hashedpassword, array('id'=>$user->id)); echo "Password changed\n"; -exit(0); // 0 means success \ No newline at end of file +exit(0); // 0 means success