MDL-80271 core_user: Only update record if necessary

This commit is contained in:
Philipp Memmel
2024-01-19 05:59:24 +00:00
parent 72c1d1921f
commit 09db8f17d4
+25 -7
View File
@@ -186,8 +186,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) {
@@ -197,17 +195,37 @@ function user_update_user($user, $updatepassword = true, $triggerevent = true) {
}
}
$DB->update_record('user', $user);
$currentrecord = $DB->get_record('user', ['id' => $user->id]);
$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);
}
}
}