From accb66f90530e55a2c2bf9f13b0ea9f1b8e7208e Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Thu, 16 Nov 2023 10:34:38 +0000 Subject: [PATCH] MDL-78888 tool_uploaduser: case-insensitive email matching on upload. --- admin/tool/uploaduser/classes/process.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/admin/tool/uploaduser/classes/process.php b/admin/tool/uploaduser/classes/process.php index 8569812b878..6d9cdc53bb1 100644 --- a/admin/tool/uploaduser/classes/process.php +++ b/admin/tool/uploaduser/classes/process.php @@ -461,9 +461,22 @@ class process { return; } - $matchparam = $this->get_match_on_email() ? ['email' => $user->email] : ['username' => $user->username]; - if ($existinguser = $DB->get_records('user', $matchparam + ['mnethostid' => $user->mnethostid])) { - if (is_array($existinguser) && count($existinguser) !== 1) { + if ($this->get_match_on_email()) { + // Case-insensitive query for the given email address. + $userselect = $DB->sql_equal('email', ':email', false); + $userparams = ['email' => $user->email]; + } else { + $userselect = 'username = :username'; + $userparams = ['username' => $user->username]; + } + + // Match the user, also accounting for multiple records by email. + $existinguser = $DB->get_records_select('user', "{$userselect} AND mnethostid = :mnethostid", + $userparams + ['mnethostid' => $user->mnethostid]); + $existingusercount = count($existinguser); + + if ($existingusercount > 0) { + if ($existingusercount !== 1) { $this->upt->track('status', get_string('duplicateemail', 'tool_uploaduser', $user->email), 'warning'); $this->userserrors++; return;