MDL-29320 user: Make email query case-insensitive

This commit is contained in:
Jun Pataleta
2019-04-30 16:21:25 +08:00
parent 90f26d99ab
commit 9a6022b235
3 changed files with 48 additions and 21 deletions
+12 -4
View File
@@ -210,10 +210,18 @@ class user_edit_form extends moodleform {
// Mail not confirmed yet.
} else if (!validate_email($usernew->email)) {
$errors['email'] = get_string('invalidemail');
} else if (($usernew->email !== $user->email)
and empty($CFG->allowaccountssameemail)
and $DB->record_exists('user', array('email' => $usernew->email, 'mnethostid' => $CFG->mnet_localhost_id))) {
$errors['email'] = get_string('emailexists');
} else if (($usernew->email !== $user->email) && empty($CFG->allowaccountssameemail)) {
// Make a case-insensitive query for the given email address.
$select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
$params = array(
'email' => $usernew->email,
'mnethostid' => $CFG->mnet_localhost_id,
'userid' => $usernew->id
);
// If there are other user(s) that already have the same email, show an error.
if ($DB->record_exists_select('user', $select, $params)) {
$errors['email'] = get_string('emailexists');
}
}
if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
+12 -3
View File
@@ -298,9 +298,18 @@ class user_editadvanced_form extends moodleform {
if (!$user or (isset($usernew->email) && $user->email !== $usernew->email)) {
if (!validate_email($usernew->email)) {
$err['email'] = get_string('invalidemail');
} else if (empty($CFG->allowaccountssameemail)
and $DB->record_exists('user', array('email' => $usernew->email, 'mnethostid' => $CFG->mnet_localhost_id))) {
$err['email'] = get_string('emailexists');
} else if (empty($CFG->allowaccountssameemail)) {
// Make a case-insensitive query for the given email address.
$select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
$params = array(
'email' => $usernew->email,
'mnethostid' => $CFG->mnet_localhost_id,
'userid' => $usernew->id
);
// If there are other user(s) that already have the same email, show an error.
if ($DB->record_exists_select('user', $select, $params)) {
$err['email'] = get_string('emailexists');
}
}
}
+24 -14
View File
@@ -60,22 +60,32 @@ if (empty($preferences['newemailattemptsleft'])) {
$user->email = $preferences['newemail'];
// Detect duplicate before saving.
if ($DB->get_record('user', array('email' => $user->email))) {
redirect(new moodle_url('/user/view.php', ['id' => $user->id]), get_string('emailnowexists', 'auth'));
} else {
// Update user email.
$authplugin = get_auth_plugin($user->auth);
$authplugin->user_update($olduser, $user);
user_update_user($user, false);
$a->email = $user->email;
redirect(
new moodle_url('/user/view.php', ['id' => $user->id]),
get_string('emailupdatesuccess', 'auth', $a),
null,
\core\output\notification::NOTIFY_SUCCESS
);
if (empty($CFG->allowaccountssameemail)) {
// Make a case-insensitive query for the given email address.
$select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
$params = array(
'email' => $user->email,
'mnethostid' => $CFG->mnet_localhost_id,
'userid' => $user->id
);
// If there are other user(s) that already have the same email, cancel and redirect.
if ($DB->record_exists_select('user', $select, $params)) {
redirect(new moodle_url('/user/view.php', ['id' => $user->id]), get_string('emailnowexists', 'auth'));
}
}
// Update user email.
$authplugin = get_auth_plugin($user->auth);
$authplugin->user_update($olduser, $user);
user_update_user($user, false);
$a->email = $user->email;
redirect(
new moodle_url('/user/view.php', ['id' => $user->id]),
get_string('emailupdatesuccess', 'auth', $a),
null,
\core\output\notification::NOTIFY_SUCCESS
);
} else {
$preferences['newemailattemptsleft']--;
set_user_preference('newemailattemptsleft', $preferences['newemailattemptsleft'], $user->id);