MDL-16982 Administration: Integrated Inaki's suggestions

This commit is contained in:
Rajesh Taneja 2013-01-31 13:12:17 +08:00
parent 57d135a1c6
commit d836e3ed1f
3 changed files with 45 additions and 53 deletions

View File

@ -129,12 +129,11 @@ class auth_plugin_ldap extends auth_plugin_base {
// so leave $this->config->objectclass as is.
}
}
/**
* Constructor with initialisation.
*/
function auth_plugin_ldap() {
global $DB;
$this->authtype = 'ldap';
$this->roleauth = 'auth_ldap';
$this->errorlogtag = '[AUTH LDAP] ';
@ -294,10 +293,7 @@ class auth_plugin_ldap extends auth_plugin_base {
} else {
$newval = textlib::convert($entry[$value], $this->config->ldapencoding, 'utf-8');
}
// Need to allow for 0 or '0' will ldap ever return an empty string
// or will the array_key_exists catch such things.
if (isset($newval) && ($newval !== '')) { // Favour ldap entries that are set.
if (!empty($newval)) { // favour ldap entries that are set
$ldapval = $newval;
}
}
@ -1164,17 +1160,18 @@ class auth_plugin_ldap extends auth_plugin_base {
$user_entry = array_change_key_case($user_entry[0], CASE_LOWER);
foreach ($attrmap as $key => $ldapkeys) {
$profilefield = '';
// Only process if the moodle field ($key) has changed and we
// are set to update LDAP with it
if (isset($olduser->$key) and isset($newuser->$key)
and ($olduser->$key !== $newuser->$key)) {
$profile_field = $key;
$profilefield = $key;
} else if (isset($olduser->{'profile_field_' . $key}) && isset($newuser->{'profile_field_' . $key})
&& $olduser->{'profile_field_' . $key} !== $newuser->{'profile_field_' . $key}) {
$profile_field = 'profile_field_' . $key;
$profilefield = 'profile_field_' . $key;
}
if (!empty($profile_field) && !empty($this->config->{'field_updateremote_' . $key})) {
if (!empty($profilefield) && !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
@ -1187,14 +1184,13 @@ class auth_plugin_ldap extends auth_plugin_base {
$ambiguous = false;
}
$nuvalue = textlib::convert($newuser->$profile_field, 'utf-8', $this->config->ldapencoding);
$nuvalue = textlib::convert($newuser->$profilefield, 'utf-8', $this->config->ldapencoding);
empty($nuvalue) ? $nuvalue = array() : $nuvalue;
$ouvalue = textlib::convert($olduser->$profile_field, 'utf-8', $this->config->ldapencoding);
$ouvalue = textlib::convert($olduser->$profilefield, 'utf-8', $this->config->ldapencoding);
foreach ($ldapkeys as $ldapkey) {
$ldapkey = $ldapkey;
$ldapvalue = empty($user_entry[$ldapkey][0]) ? null : $ldapvalue;
$ldapvalue = $user_entry[$ldapkey][0];
if (!$ambiguous) {
// Skip update if the values already match
if ($nuvalue !== $ldapvalue) {
@ -1453,7 +1449,15 @@ class auth_plugin_ldap extends auth_plugin_base {
function ldap_attributes () {
$moodleattributes = array();
foreach ($this->userfields as $field) {
// If we have custom fields then merge them with user fields.
$customfields = $this->get_custom_user_profile_fields();
if (!empty($customfields) && !empty($this->userfields)) {
$userfields = array_merge($this->userfields, $customfields);
} else {
$userfields = $this->userfields;
}
foreach ($userfields as $field) {
if (!empty($this->config->{"field_map_$field"})) {
$moodleattributes[$field] = textlib::strtolower(trim($this->config->{"field_map_$field"}));
if (preg_match('/,/', $moodleattributes[$field])) {
@ -1461,18 +1465,6 @@ class auth_plugin_ldap extends auth_plugin_base {
}
}
}
// Add custom fields.
$customfields = $this->get_custom_user_profile_fields();
foreach ($customfields as $field) {
if (!empty($this->config->{"field_map_$field"})) {
$moodleattributes[$field] = $this->config->{"field_map_$field"};
if (preg_match('/,/',$moodleattributes[$field])) {
$moodleattributes[$field] = explode(',', $moodleattributes[$field]);
}
}
}
$moodleattributes['username'] = textlib::strtolower(trim($this->config->user_attribute));
return $moodleattributes;
}

View File

@ -121,7 +121,7 @@ class auth_plugin_base {
* Moodle custom fields to sync with.
* @var array()
*/
var $custom_fields = null;
var $customfields = null;
/**
@ -535,23 +535,20 @@ class auth_plugin_base {
*/
public function get_custom_user_profile_fields() {
global $DB;
// If already retrived then return.
if (!is_null($this->custom_fields)) {
return $this->custom_fields;
// If already retrieved then return.
if (!is_null($this->customfields)) {
return $this->customfields;
}
$this->custom_fields = array();
$customfields = array();
$this->customfields = array();
if ($proffields = $DB->get_records('user_info_field')) {
foreach ($proffields as $proffield) {
$customfields[] = 'profile_field_'.$proffield->shortname;
$this->customfields[] = 'profile_field_'.$proffield->shortname;
}
}
unset($proffields);
$this->custom_fields = $customfields;
return $customfields;
return $this->customfields;
}
}

View File

@ -3897,7 +3897,7 @@ function create_user_record($username, $password, $auth = 'manual') {
foreach ($customfield as $key) {
$key->userid = $newuser->id;
$key->id = $DB->insert_record("user_info_data", $key);
$key->id = $DB->insert_record('user_info_data', $key);
}
$user = get_complete_user_data('id', $newuser->id);
@ -3961,24 +3961,18 @@ function update_user_record($username) {
}
} else if ($iscustom) {
$shortname = str_replace('profile_field_', '', $key);
// If there is no value in the user_info_data then.
$infofield = $DB->get_record('user_info_field', array('shortname' => $shortname));
$userid = $DB->get_field('user', 'id', array('username' => $username));
$data = $DB->get_field('user_info_data', 'data', array('userid' => $userid, 'fieldid' => $infofield->id));
$data = $DB->get_field('user_info_data', 'data', array('userid' => $oldinfo->id, 'fieldid' => $infofield->id));
// If there is no value in the user_info_data then use default value for comparison.
if ($data === false) {
$data = $infofield->defaultdata;
if (strcmp($data, $value) !== 0) {
$row = new stdClass();
$row->userid = $userid;
$row->fieldid = $infofield->id;
$row->data = $data;
$row->id = $DB->insert_record("user_info_data", $row);
}
$originalvalue = $infofield->defaultdata;
} else {
$originalvalue = $data;
}
if (strcmp($data, $value) !== 0) {
// If passed value is different then original value then update/insert.
if (strcmp($originalvalue, $value) !== 0) {
$valid = true;
// Check to make sure that the value we are placing in is a valid one.
// Check to make sure that the value is a valid.
if (strcmp($info_field->datatype, 'menu') == 0) {
$validValues = explode("\n", $info_field->param1);
if (!in_array($value, $validValues)) {
@ -3990,9 +3984,18 @@ function update_user_record($username) {
}
}
// Update value if it is diffrent then old.
// Insert/update if value is valid.
if ($valid) {
$DB->set_field('user_info_data', 'data', $value, array('userid'=>$userid, 'fieldid'=>$infofield->id));
if ($data === false) {
$row = new stdClass();
$row->userid = $oldinfo->id;
$row->fieldid = $infofield->id;
$row->data = $value;
$row->id = $DB->insert_record('user_info_data', $row);
} else {
$DB->set_field('user_info_data', 'data', $value,
array('userid' => $oldinfo->id, 'fieldid' => $infofield->id));
}
}
}
}