From 90a5693234fbf2a4a34e5585c6c283f5f08112eb Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Thu, 21 Feb 2019 15:24:56 +0800 Subject: [PATCH] MDL-64774 user: Make email query case-insensitive --- user/externallib.php | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/user/externallib.php b/user/externallib.php index 755cac14fc9..fd1d43511e3 100644 --- a/user/externallib.php +++ b/user/externallib.php @@ -199,9 +199,17 @@ class core_user_external extends external_api { // Make sure we validate current user info as handled by current GUI. See user/editadvanced_form.php func validation(). if (!validate_email($user['email'])) { throw new invalid_parameter_exception('Email address is invalid: '.$user['email']); - } else if (empty($CFG->allowaccountssameemail) && - $DB->record_exists('user', array('email' => $user['email'], 'mnethostid' => $user['mnethostid']))) { - throw new invalid_parameter_exception('Email address already exists: '.$user['email']); + } 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'; + $params = array( + 'email' => $user['email'], + 'mnethostid' => $user['mnethostid'] + ); + // If there are other user(s) that already have the same email, throw an error. + if ($DB->record_exists_select('user', $select, $params)) { + throw new invalid_parameter_exception('Email address already exists: '.$user['email']); + } } // End of user info validation. @@ -563,9 +571,18 @@ class core_user_external extends external_api { if (isset($user['email']) && $user['email'] !== $existinguser->email) { if (!validate_email($user['email'])) { continue; - } else if (empty($CFG->allowaccountssameemail) && - $DB->record_exists('user', array('email' => $user['email'], 'mnethostid' => $CFG->mnet_localhost_id))) { - continue; + } else if (empty($CFG->allowaccountssameemail)) { + // Make a case-insensitive query for the given email address and make sure to exclude the user being updated. + $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'] + ); + // Skip if there are other user(s) that already have the same email. + if ($DB->record_exists_select('user', $select, $params)) { + continue; + } } }