Skip to content

Commit

Permalink
MDL-29318 core: Fixes for get_complete_user_data()
Browse files Browse the repository at this point in the history
* Added email in the list of case-insensitive fields.
* New optional parameter $throwexception for \get_complete_user_data().
  If true, an exception will be thrown when there's no matching record
  found or when there are multiple records found for the given field
  value. If false, it will simply return false.
  Defaults to false when not set. This ensures that
  get_complete_user_data() fetches the correct user data.
  • Loading branch information
junpataleta committed Mar 29, 2019
1 parent b53ec2b commit 8cb4579
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/moodlelib.php
Expand Up @@ -4767,9 +4767,11 @@ function update_internal_user_password($user, $password, $fasthash = false) {
* @param string $field The user field to be checked for a given value.
* @param string $value The value to match for $field.
* @param int $mnethostid
* @param bool $throwexception If true, it will throw an exception when there's no record found or when there are multiple records
* found. Otherwise, it will just return false.
* @return mixed False, or A {@link $USER} object.
*/
function get_complete_user_data($field, $value, $mnethostid = null) {
function get_complete_user_data($field, $value, $mnethostid = null, $throwexception = false) {
global $CFG, $DB;

if (!$field || !$value) {
Expand All @@ -4780,7 +4782,7 @@ function get_complete_user_data($field, $value, $mnethostid = null) {
$field = core_text::strtolower($field);

// List of case insensitive fields.
$caseinsensitivefields = ['username'];
$caseinsensitivefields = ['username', 'email'];

// Build the WHERE clause for an SQL query.
$params = array('fieldval' => $value);
Expand All @@ -4807,8 +4809,18 @@ function get_complete_user_data($field, $value, $mnethostid = null) {
}

// Get all the basic user data.
if (! $user = $DB->get_record_select('user', $constraints, $params)) {
return false;
try {
// Make sure that there's only a single record that matches our query.
// For example, when fetching by email, multiple records might match the query as there's no guarantee that email addresses
// are unique. Therefore we can't reliably tell whether the user profile data that we're fetching is the correct one.
$user = $DB->get_record_select('user', $constraints, $params, '*', MUST_EXIST);
} catch (dml_exception $exception) {
if ($throwexception) {
throw $exception;
} else {
// Return false when no records or multiple records were found.
return false;
}
}

// Get various settings and preferences.
Expand Down
3 changes: 3 additions & 0 deletions lib/upgrade.txt
Expand Up @@ -6,6 +6,9 @@ information provided here is intended especially for developers.
* Behat timeout constants behat_base::TIMEOUT, EXTENDED_TIMEOUT, and REDUCED_TIMEOUT will be
deprecated in 3.7. Please instead use the functions behat_base::get_timeout(), get_extended_timeout(),
and get_reduced_timeout(). These allow for timeouts to be increased by a setting in config.php.
* New optional parameter $throwexception for \get_complete_user_data(). If true, an exception will be thrown when there's no
matching record found or when there are multiple records found for the given field value. If false, it will simply return false.
Defaults to false when not set.

=== 3.6.3 ===

Expand Down

0 comments on commit 8cb4579

Please sign in to comment.