From 0503fc7a355109e5d5165c53d44e43c616fe3a7f Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Wed, 6 Mar 2019 15:34:38 +0800 Subject: [PATCH] MDL-29318 login: Handle email in case-insensitive manner * Let get_complete_user_data() handle the fetching of user data and handle the logic of the errors to be shown based on the exception it throws. This also saves us 1 DB query by eliminating the need to count for the users that match a given email first before fetching user information. --- login/lib.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/login/lib.php b/login/lib.php index 8927e03f6be..2ca4e47eca1 100644 --- a/login/lib.php +++ b/login/lib.php @@ -355,17 +355,20 @@ function core_login_validate_forgot_password_data($data) { if (!validate_email($data['email'])) { $errors['email'] = get_string('invalidemail'); - } else if ($DB->count_records('user', array('email' => $data['email'])) > 1) { - $errors['email'] = get_string('forgottenduplicate'); - } else { - if ($user = get_complete_user_data('email', $data['email'])) { + try { + $user = get_complete_user_data('email', $data['email'], null, true); if (empty($user->confirmed)) { $errors['email'] = get_string('confirmednot'); } - } - if (!$user and empty($CFG->protectusernames)) { - $errors['email'] = get_string('emailnotfound'); + } catch (dml_missing_record_exception $missingexception) { + // User not found. Show error when $CFG->protectusernames is turned off. + if (empty($CFG->protectusernames)) { + $errors['email'] = get_string('emailnotfound'); + } + } catch (dml_multiple_records_exception $multipleexception) { + // Multiple records found. Ask the user to enter a username instead. + $errors['email'] = get_string('forgottenduplicate'); } }