Commit bb807f87 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Allow internal auth users to change their usernames

parent c54a779a
......@@ -71,6 +71,18 @@ else {
$elements = array();
}
if ($authobj->authname == 'internal') {
$elements['changeusernameheading'] = array(
'value' => '<tr><td colspan="2"><p>' . get_string('changeusernameheading', 'account') . '</p></td></tr>'
);
$elements['username'] = array(
'type' => 'text',
'defaultvalue' => $USER->get('username'),
'title' => get_string('changeusername', 'account'),
'description' => get_string('changeusernamedesc', 'account', get_config('sitename')),
);
}
$elements['accountoptionsdesc'] = array(
'value' => '<tr><td colspan="2"><p>' . get_string('accountoptionsdesc', 'account') . '</p></td></tr>'
);
......@@ -160,6 +172,15 @@ function accountprefs_validate(Pieform $form, $values) {
$form->set_error('oldpassword', get_string('mustspecifyoldpassword'));
}
}
if ($authobj->authname == 'internal' && $values['username'] != $USER->get('username')) {
if (!AuthInternal::is_username_valid($values['username'])) {
$form->set_error('username', get_string('usernameinvalidform', 'auth.internal'));
}
if (!$form->get_error('username') && record_exists_select('usr', 'LOWER(username) = ?', strtolower($values['username']))) {
$form->set_error('username', get_string('usernamealreadytaken', 'auth.internal'));
}
}
}
function accountprefs_submit(Pieform $form, $values) {
......@@ -184,6 +205,11 @@ function accountprefs_submit(Pieform $form, $values) {
}
}
if (isset($values['username']) && $values['username'] != $USER->get('username')) {
$USER->username = $values['username'];
$USER->commit();
}
db_commit();
$form->json_reply(PIEFORM_OK, get_string('prefssaved', 'account'));
}
......
......@@ -58,7 +58,7 @@ $string['registeredok'] = '<p>You have successfully registered. Please check you
$string['registrationnosuchkey'] = 'Sorry, there does not seem to be a registration with this key. Perhaps you waited longer than 24 hours to complete your registration? Otherwise, it might be our fault.';
$string['registrationunsuccessful'] = 'Sorry, your registration attempt was unsuccessful. This is our fault, not yours. Please try again later.';
$string['usernamealreadytaken'] = 'Sorry, this username is already taken';
$string['usernameinvalidform'] = 'Your username may only include alphanumeric characters, full stops, underscores and @ symbols. Also, it must be between 3 and 30 characters long.';
$string['usernameinvalidform'] = 'Usernames may contain letters, numbers and most common symbols, and must be from 3 to 30 characters in length. Spaces are not allowed.';
$string['youmaynotregisterwithouttandc'] = 'You may not register unless you agree to abide by the <a href="terms.php">Terms and Conditions</a>';
$string['youmustagreetothetermsandconditions'] = 'You must agree to the <a href="terms.php">Terms and Conditions</a>';
......
......@@ -320,10 +320,6 @@ class User {
throw new InvalidArgumentException('quotaused should be set via the quota_* methods');
}
if ($key == 'username' && $this->id != 0) {
throw new InvalidArgumentException('We cannot change the username of an existing user');
}
$this->set($key, $value);
}
......
......@@ -30,6 +30,10 @@ $string['changepassworddesc'] = 'If you wish to change your password, please ent
$string['changepasswordotherinterface'] = 'You may <a href="%s">change your password</a> through a different interface</a>';
$string['oldpasswordincorrect'] = 'This is not your current password';
$string['changeusernameheading'] = 'Change username';
$string['changeusername'] = 'New username';
$string['changeusernamedesc'] = 'The username you use to log into %s. Usernames are 3-30 characters long, and may contain letters, numbers, and most common symbols excuding spaces.';
$string['accountoptionsdesc'] = 'You can set general account options here';
$string['friendsnobody'] = 'Nobody may add me as a friend';
$string['friendsauth'] = 'New friends require my authorisation';
......
Supports Markdown
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