Skip to content

Commit

Permalink
Merge branch 'MDL-80271-main' of https://github.com/PhMemmel/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Jan 23, 2024
2 parents a515bda + ca7b355 commit 58036bd
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions user/lib.php
Expand Up @@ -157,10 +157,11 @@ function user_update_user($user, $updatepassword = true, $triggerevent = true) {
$user = (object) $user;
}

$currentrecord = $DB->get_record('user', ['id' => $user->id]);

// Communication api update for user.
if (core_communication\api::is_available()) {
$usercourses = enrol_get_users_courses($user->id);
$currentrecord = $DB->get_record('user', ['id' => $user->id]);
if (!empty($currentrecord) && isset($user->suspended) && $currentrecord->suspended !== $user->suspended) {
foreach ($usercourses as $usercourse) {
$communication = \core_communication\api::load_by_instance(
Expand Down Expand Up @@ -208,8 +209,6 @@ function user_update_user($user, $updatepassword = true, $triggerevent = true) {
unset($user->calendartype);
}

$user->timemodified = time();

// Validate user data object.
$uservalidation = core_user::validate($user);
if ($uservalidation !== true) {
Expand All @@ -219,17 +218,36 @@ function user_update_user($user, $updatepassword = true, $triggerevent = true) {
}
}

$DB->update_record('user', $user);
$changedattributes = [];
foreach ($user as $attributekey => $attributevalue) {
// We explicitly want to ignore 'timemodified' attribute for checking, if an update is needed.
if (!property_exists($currentrecord, $attributekey) || $attributekey === 'timemodified') {
continue;
}
if ($currentrecord->{$attributekey} != $attributevalue) {
$changedattributes[$attributekey] = $attributevalue;
}
}
if (!empty($changedattributes)) {
$changedattributes['timemodified'] = time();
$updaterecord = (object) $changedattributes;
$updaterecord->id = $currentrecord->id;
$DB->update_record('user', $updaterecord);
}

if ($updatepassword) {
// Get full user record.
$updateduser = $DB->get_record('user', array('id' => $user->id));
// If there have been changes, update user record with changed attributes.
if (!empty($changedattributes)) {
foreach ($changedattributes as $attributekey => $attributevalue) {
$currentrecord->{$attributekey} = $attributevalue;
}
}

// If password was set, then update its hash.
if (isset($passwd)) {
$authplugin = get_auth_plugin($updateduser->auth);
$authplugin = get_auth_plugin($currentrecord->auth);
if ($authplugin->can_change_password()) {
$authplugin->user_update_password($updateduser, $passwd);
$authplugin->user_update_password($currentrecord, $passwd);
}
}
}
Expand Down

0 comments on commit 58036bd

Please sign in to comment.