diff --git a/auth/ldap/auth.php b/auth/ldap/auth.php index 7f59282d3d2..761913904f8 100644 --- a/auth/ldap/auth.php +++ b/auth/ldap/auth.php @@ -960,6 +960,31 @@ class auth_plugin_ldap extends auth_plugin_base { role_assign($creatorrole->id, $id, $sitecontext->id, $this->roleauth); } + // Save custom profile fields. + $euser->profile = array(); + foreach ($user as $key => $value) { + if (preg_match('/^profile_field_(.*)$/', $key, $match)) { + $field = $match[1]; + $euser->profile[$field] = $user->$key; + } + } + + // Now, save the profile fields if the user has any. + if ($fields = $DB->get_records('user_info_field')) { + foreach ($fields as $field) { + if (isset($euser->profile[$field->shortname])) { + $conditions = array('fieldid' => $field->id, 'userid' => $euser->id); + $id = $DB->get_field('user_info_data', 'id', $conditions); + $data = $euser->profile[$field->shortname]; + if ($id) { + $DB->set_field('user_info_data', 'data', $data, array('id' => $id)); + } else { + $record = array('fieldid' => $field->id, 'userid' => $euser->id, 'data' => $data); + $DB->insert_record('user_info_data', $record); + } + } + } + } } $transaction->allow_commit(); unset($add_users); // free mem @@ -990,6 +1015,8 @@ class auth_plugin_ldap extends auth_plugin_base { function update_user_record($username, $updatekeys = false, $triggerevent = false) { global $CFG, $DB; + require_once($CFG->dirroot.'/user/profile/lib.php'); + // Just in case check text case $username = trim(core_text::strtolower($username)); @@ -1001,9 +1028,14 @@ class auth_plugin_ldap extends 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; + $needsupdate = false; + if ($newinfo = $this->get_userinfo($username)) { $newinfo = truncate_userinfo($newinfo); @@ -1016,6 +1048,7 @@ class auth_plugin_ldap extends auth_plugin_base { $newuser->id = $userid; // The cast to int is a workaround for MDL-53959. $newuser->suspended = (int)$this->is_user_suspended((object) $newinfo); + $newuser->profile = array(); foreach ($updatekeys as $key) { if (isset($newinfo[$key])) { @@ -1025,18 +1058,50 @@ class auth_plugin_ldap extends auth_plugin_base { } if (!empty($this->config->{'field_updatelocal_' . $key})) { - // Only update if it's changed. - if ($user->{$key} != $value) { + 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; + } else { + // Standard field. + $currentvalue = isset($user->$key) ? $user->$key : null; $newuser->$key = $value; } } + + // Only update if it's changed. + if ($currentvalue !== $value) { + $needsupdate = true; + } } - user_update_user($newuser, false, $triggerevent); } - } else { - return false; + + if ($needsupdate) { + user_update_user($newuser, false, $triggerevent); + + // Now, save the profile fields if the user has any. + if ($fields = $DB->get_records('user_info_field')) { + foreach ($fields as $field) { + if (isset($newuser->profile[$field->shortname])) { + $conditions = array('fieldid' => $field->id, 'userid' => $newuser->id); + $id = $DB->get_field('user_info_data', 'id', $conditions); + $data = $newuser->profile[$field->shortname]; + if ($id) { + $DB->set_field('user_info_data', 'data', $data, array('id' => $id)); + } else { + $record = array('fieldid' => $field->id, 'userid' => $newuser->id, 'data' => $data); + $DB->insert_record('user_info_data', $record); + } + } + } + } + + return $DB->get_record('user', array('id' => $userid, 'deleted' => 0)); + } } - return $DB->get_record('user', array('id'=>$userid, 'deleted'=>0)); + + return false; } /** @@ -1186,6 +1251,14 @@ class auth_plugin_ldap extends auth_plugin_base { return false; } + // Load old custom fields. + $olduser->profile = (array)profile_user_record($olduser->id, false); + + $fields = array(); + foreach (profile_get_custom_fields(false) as $field) { + $fields[$field->shortname] = $field; + } + $success = true; $user_info_result = ldap_read($ldapconnection, $user_dn, '(objectClass=*)', $search_attribs); if ($user_info_result) { @@ -1204,19 +1277,24 @@ class auth_plugin_ldap extends auth_plugin_base { $user_entry = $user_entry[0]; foreach ($attrmap as $key => $ldapkeys) { - $profilefield = ''; - // Only process if the moodle field ($key) has changed and we - // are set to update LDAP with it - $customprofilefield = 'profile_field_' . $key; - if (isset($olduser->$key) and isset($newuser->$key) - and ($olduser->$key !== $newuser->$key)) { - $profilefield = $key; - } else if (isset($olduser->$customprofilefield) && isset($newuser->$customprofilefield) - && $olduser->$customprofilefield !== $newuser->$customprofilefield) { - $profilefield = $customprofilefield; + if (preg_match('/^profile_field_(.*)$/', $key, $match)) { + // Custom field. + $fieldname = $match[1]; + 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; + } else { + $oldvalue = null; + } + $newvalue = $formfield->edit_save_data_preprocess($newuser->{$formfield->inputname}, new stdClass); + } else { + // Standard field. + $oldvalue = isset($olduser->$key) ? $olduser->$key : null; + $newvalue = isset($newuser->$key) ? $newuser->$key : null; } - if (!empty($profilefield) && !empty($this->config->{'field_updateremote_' . $key})) { + if ($newvalue !== null and $newvalue !== $oldvalue and !empty($this->config->{'field_updateremote_' . $key})) { // For ldap values that could be in more than one // ldap key, we will do our best to match // where they came from @@ -1229,9 +1307,9 @@ class auth_plugin_ldap extends auth_plugin_base { $ambiguous = false; } - $nuvalue = core_text::convert($newuser->$profilefield, 'utf-8', $this->config->ldapencoding); + $nuvalue = core_text::convert($newvalue, 'utf-8', $this->config->ldapencoding); empty($nuvalue) ? $nuvalue = array() : $nuvalue; - $ouvalue = core_text::convert($olduser->$profilefield, 'utf-8', $this->config->ldapencoding); + $ouvalue = core_text::convert($oldvalue, 'utf-8', $this->config->ldapencoding); foreach ($ldapkeys as $ldapkey) { $ldapkey = $ldapkey;