diff --git a/auth/ldap/auth.php b/auth/ldap/auth.php index a87cc1174ab..d0c39c50662 100644 --- a/auth/ldap/auth.php +++ b/auth/ldap/auth.php @@ -941,14 +941,14 @@ class auth_plugin_ldap extends auth_plugin_base { } // Save custom profile fields. - $euser->profile = array(); + $profilefields = array(); foreach ($user as $key => $value) { if (preg_match('/^profile_field_(.*)$/', $key, $match)) { $field = $match[1]; - $euser->profile[$field] = $user->$key; + $profilefields[$field] = $user->$key; } } - profile_save_custom_fields($euser); + profile_save_custom_fields($euser->id, $profilefields); // Add roles if needed. $this->sync_roles($euser); @@ -1152,7 +1152,7 @@ class auth_plugin_ldap extends auth_plugin_base { } // Load old custom fields. - profile_load_custom_fields($olduser, false); + $olduserprofilefields = (array) profile_user_record($olduser->id, false); $fields = array(); foreach (profile_get_custom_fields(false) as $field) { @@ -1183,7 +1183,7 @@ class auth_plugin_ldap extends auth_plugin_base { if (isset($fields[$fieldname])) { $class = 'profile_field_' . $fields[$fieldname]->datatype; $formfield = new $class($fields[$fieldname]->id, $olduser->id); - $oldvalue = isset($olduser->profile[$fieldname]) ? $olduser->profile[$fieldname] : null; + $oldvalue = isset($olduserprofilefields[$fieldname]) ? $olduserprofilefields[$fieldname] : null; } else { $oldvalue = null; } diff --git a/lib/authlib.php b/lib/authlib.php index 41e7cd3c4b5..6752ae4e1b1 100644 --- a/lib/authlib.php +++ b/lib/authlib.php @@ -653,9 +653,6 @@ class auth_plugin_base { die; } - // Load all custom fields into $user->profile. - profile_load_custom_fields($user, false); - // Protect the userid from being overwritten. $userid = $user->id; @@ -673,7 +670,9 @@ class auth_plugin_base { $newuser->id = $userid; // The cast to int is a workaround for MDL-53959. $newuser->suspended = (int) $suspenduser; - $newuser->profile = array(); + // Load all custom fields. + $profilefields = (array) profile_user_record($user->id, false); + $newprofilefields = []; foreach ($updatekeys as $key) { if (isset($newinfo[$key])) { @@ -686,8 +685,8 @@ class auth_plugin_base { if (preg_match('/^profile_field_(.*)$/', $key, $match)) { // Custom field. $field = $match[1]; - $currentvalue = isset($user->profile[$field]) ? $user->profile[$field] : null; - $newuser->profile[$field] = $value; + $currentvalue = isset($profilefields[$field]) ? $profilefields[$field] : null; + $newprofilefields[$field] = $value; } else { // Standard field. $currentvalue = isset($user->$key) ? $user->$key : null; @@ -704,7 +703,7 @@ class auth_plugin_base { if ($needsupdate) { user_update_user($newuser, false, $triggerevent); - profile_save_custom_fields($newuser); + profile_save_custom_fields($newuser->id, $newprofilefields); return $DB->get_record('user', array('id' => $userid, 'deleted' => 0)); } } diff --git a/user/profile/lib.php b/user/profile/lib.php index b9ad8081de5..29cb7b73504 100644 --- a/user/profile/lib.php +++ b/user/profile/lib.php @@ -770,30 +770,30 @@ function profile_get_custom_fields($onlyinuserobject = false) { * Load custom profile fields into user object * * @param stdClass $user user object - * @param bool $onlyinuserobject True if you only want the ones in $USER */ -function profile_load_custom_fields($user, $onlyinuserobject = true) { - $user->profile = (array)profile_user_record($user->id, $onlyinuserobject); +function profile_load_custom_fields($user) { + $user->profile = (array)profile_user_record($user->id); } /** - * Save custom profile fields in user object + * Save custom profile fields for a user. * - * @param stdClass $user user object + * @param int $userid The user id + * @param array $profilefields The fields to save */ -function profile_save_custom_fields($user) { +function profile_save_custom_fields($userid, $profilefields) { global $DB; if ($fields = $DB->get_records('user_info_field')) { foreach ($fields as $field) { - if (isset($user->profile[$field->shortname])) { - $conditions = array('fieldid' => $field->id, 'userid' => $user->id); + if (isset($profilefields[$field->shortname])) { + $conditions = array('fieldid' => $field->id, 'userid' => $userid); $id = $DB->get_field('user_info_data', 'id', $conditions); - $data = $user->profile[$field->shortname]; + $data = $profilefields[$field->shortname]; if ($id) { $DB->set_field('user_info_data', 'data', $data, array('id' => $id)); } else { - $record = array('fieldid' => $field->id, 'userid' => $user->id, 'data' => $data); + $record = array('fieldid' => $field->id, 'userid' => $userid, 'data' => $data); $DB->insert_record('user_info_data', $record); } }