From 307ed32403889fce08d0bf574e6401518aaafab3 Mon Sep 17 00:00:00 2001 From: Niko Hoogeveen Date: Wed, 10 Dec 2025 14:15:29 -0500 Subject: [PATCH] MDL-73396 core_user: prioritize active users when retrieving by email Updated get_users_by_email() to return active users at the start of the returned array, suspended users last. --- lib/classes/user.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/classes/user.php b/lib/classes/user.php index 065692233d3..69854ccc4fa 100644 --- a/lib/classes/user.php +++ b/lib/classes/user.php @@ -156,7 +156,14 @@ class user { $mnethostid = $CFG->mnet_localhost_id; } - return $DB->get_record('user', ['email' => $email, 'mnethostid' => $mnethostid], $fields, $strictness); + // Build SQL query to prioritise active users. + $sql = "SELECT $fields + FROM {user} + WHERE email = :email AND mnethostid = :mnethostid + ORDER BY suspended ASC, timecreated DESC"; + $params = ['email' => $email, 'mnethostid' => $mnethostid]; + // Return the first user matching the query criteria. + return $DB->get_record_sql($sql, $params, $strictness); } /**