Merge branch 'MDL-29318-master' of git://github.com/junpataleta/moodle

This commit is contained in:
Adrian Greeve 2019-04-18 13:48:33 +08:00
commit 48fa567da2
5 changed files with 77 additions and 15 deletions

View file

@ -4791,9 +4791,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) {
@ -4804,7 +4806,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);
@ -4831,8 +4833,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.