MDL-40613 auth_ldap: sync user profile fields
This commit is contained in:
+100
-21
@@ -928,14 +928,40 @@ class auth_plugin_ldap extends auth_plugin_base {
|
||||
|
||||
$id = user_create_user($user, false);
|
||||
echo "\t"; print_string('auth_dbinsertuser', 'auth_db', array('name'=>$user->username, 'id'=>$id)); echo "\n";
|
||||
$user = $DB->get_record('user', array('id' => $id));
|
||||
$euser = $DB->get_record('user', array('id' => $id));
|
||||
|
||||
if (!empty($this->config->forcechangepassword)) {
|
||||
set_user_preference('auth_forcepasswordchange', 1, $id);
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add roles if needed.
|
||||
$this->sync_roles($user);
|
||||
$this->sync_roles($euser);
|
||||
|
||||
}
|
||||
$transaction->allow_commit();
|
||||
@@ -967,6 +993,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));
|
||||
|
||||
@@ -978,9 +1006,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);
|
||||
|
||||
@@ -993,6 +1026,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])) {
|
||||
@@ -1002,18 +1036,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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1201,6 +1267,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) {
|
||||
@@ -1219,19 +1293,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
|
||||
@@ -1244,9 +1323,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;
|
||||
|
||||
Reference in New Issue
Block a user