Skip to content

Commit

Permalink
MDL-40613 auth_ldap: removed usage of profile_load_custom_fields()
Browse files Browse the repository at this point in the history
Also reverted profile_load_custom_fields() signature and changed
behaviour of the new function profile_save_custom_fields().
  • Loading branch information
mdjnelson committed Jan 3, 2018
1 parent 80d7aa1 commit e8a1a58
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
10 changes: 5 additions & 5 deletions auth/ldap/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -941,14 +941,14 @@ function sync_users($do_updates=true) {
}

// 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);
Expand Down Expand Up @@ -1152,7 +1152,7 @@ function user_update($olduser, $newuser) {
}

// 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) {
Expand Down Expand Up @@ -1183,7 +1183,7 @@ function user_update($olduser, $newuser) {
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;
}
Expand Down
13 changes: 6 additions & 7 deletions lib/authlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,9 +653,6 @@ protected function update_user_record($username, $updatekeys = false, $triggerev
die;
}

// Load all custom fields into $user->profile.
profile_load_custom_fields($user, false);

// Protect the userid from being overwritten.
$userid = $user->id;

Expand All @@ -673,7 +670,9 @@ protected function update_user_record($username, $updatekeys = false, $triggerev
$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])) {
Expand All @@ -686,8 +685,8 @@ protected function update_user_record($username, $updatekeys = false, $triggerev
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;
Expand All @@ -704,7 +703,7 @@ protected function update_user_record($username, $updatekeys = false, $triggerev

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));
}
}
Expand Down
20 changes: 10 additions & 10 deletions user/profile/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit e8a1a58

Please sign in to comment.