From 907be50c4233727af4ee65c018b241450dc21c8a Mon Sep 17 00:00:00 2001 From: Andrew Madden Date: Tue, 18 Jun 2019 11:58:31 +1000 Subject: [PATCH] MDL-65926 moodlelib: Convert username to lowercase prior to SQL query Within a very large project, it was found that searching for a user with their username in the user table using a case insensitive search was inefficient. Instead it is proposed that the username input is made to be lowercase prior to executing a database query as the username for each user in the database must be lowercase. This allows for a case sensitive query to find the user data. Essentially we are moving a case insensitive search for a user from SQL into PHP to increase performance. --- lib/moodlelib.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 19319dde27b..3fb8ac24a9c 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -4826,7 +4826,12 @@ function get_complete_user_data($field, $value, $mnethostid = null, $throwexcept $field = core_text::strtolower($field); // List of case insensitive fields. - $caseinsensitivefields = ['username', 'email']; + $caseinsensitivefields = ['email']; + + // Username input is forced to lowercase and should be case sensitive. + if ($field == 'username') { + $value = core_text::strtolower($value); + } // Build the WHERE clause for an SQL query. $params = array('fieldval' => $value);