Commit a95294f6 authored by Donal McMullan's avatar Donal McMullan
Browse files

Roll password_validate and password_validate_user into a single password_validate function

parent 94fec13d
......@@ -1165,70 +1165,7 @@ function auth_generate_login_form() {
* @param array $values The values passed through
* @param string $authplugin The authentication plugin that the user uses
*/
function password_validate(Pieform $form, $values, $username, $institution) {
$authinstances = auth_get_auth_instances_for_institution($institution);
// If there's only one auth instance, and it's 'internal' then we use it to
// validate the password. This was a requirement of the original spec, so
// even though this code is... strange... it's still here.
if (1 == count($authinstances) && $authinstances[0]->authname == 'internal') {
$authobj = AuthFactory::create($authinstances[0]->id);
safe_require('auth', 'internal');
$authobj = new AuthInternal($authinstances[0]->id);
if (!$form->get_error('password1') && !$authobj->is_password_valid($values['password1'])) {
$form->set_error('password1', get_string('passwordinvalidform', "auth.$authobj->type"));
}
}
$suckypasswords = array(
'mahara', 'password', $username, 'abc123'
);
if (!$form->get_error('password1') && in_array($values['password1'], $suckypasswords)) {
$form->set_error('password1', get_string('passwordtooeasy'));
}
if (!$form->get_error('password1') && $values['password1'] != $values['password2']) {
$form->set_error('password2', get_string('passwordsdonotmatch'));
}
// No Mike, that's a _BAD_ Mike! :)
if (substr($values['password1'], 0, 6) == 'mike01') {
if (!$form->get_property('jsform')) {
die_info('<img src="'
. theme_get_url('images/sidebox1_corner_botright.gif')
. '" alt="(C) 2007 MSS Enterprises"></p>');
}
}
}
/**
* Given a form, an array of values with 'password1' and 'password2'
* indices and a user, validate that the user can change their password to
* the one in $values.
*
* This provides one place where validation of passwords can be done. This is
* used by:
* - registration
* - user forgot password
* - user changing password on their account page
* - user forced to change their password by the <kbd>passwordchange</kbd>
* flag on the <kbd>usr</kbd> table.
*
* The password is checked for:
* - Being in valid form according to the rules of the authentication method
* for the user
* - Not being an easy password (a blacklist of strings, NOT a length check or
* similar), including being the user's username
* - Both values being equal
*
* @param Pieform $form The form to validate
* @param array $values The values passed through
* @param string $authplugin The authentication plugin that the user uses
*/
function password_validate_user(Pieform $form, $values, User $user) {
function password_validate(Pieform $form, $values, User $user) {
$authobj = AuthFactory::create($user->authinstance);
......
......@@ -159,10 +159,9 @@ function forgotpass_submit(Pieform $form, $values) {
}
function forgotpasschange_validate(Pieform $form, $values) {
if (!$user = get_record('usr', 'id', $values['user'])) {
throw new Exception('Request to change the password for a user who does not exist');
}
password_validate($form, $values, $user->username, $user->institution);
$user = new User();
$user->find_by_id($values['user']);
password_validate_user($form, $values, $user);
}
......
......@@ -399,7 +399,9 @@ function register_validate(Pieform $form, $values) {
$form->set_error('username', get_string('usernamealreadytaken', 'auth.internal'));
}
password_validate($form, $values, $values['username'], $values['institution']);
$user =(object) $values;
$user->authinstance = 1; // Internal
password_validate($form, $values, $user);
// First name and last name must contain at least one non whitespace
// character, so that there's something to read
......@@ -444,6 +446,9 @@ function register_submit(Pieform $form, $values) {
try {
insert_record('usr_registration', $values);
$f = fopen('/tmp/donal.txt','w');
fwrite($f, get_string('registeredemailmessagetext', 'auth.internal', $values['firstname'], get_config('sitename'), $values['key'], get_config('sitename')));
$user =(object) $values;
$user->admin = 0;
$user->staff = 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment