diff --git a/user/edit_form.php b/user/edit_form.php index 4f2214f4c44..595c733389e 100644 --- a/user/edit_form.php +++ b/user/edit_form.php @@ -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)) { diff --git a/user/editadvanced_form.php b/user/editadvanced_form.php index 31dd330cd85..6506249ae57 100644 --- a/user/editadvanced_form.php +++ b/user/editadvanced_form.php @@ -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'); + } } } diff --git a/user/emailupdate.php b/user/emailupdate.php index bfc3945c20a..4080eada0ff 100644 --- a/user/emailupdate.php +++ b/user/emailupdate.php @@ -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);