Skip to content

Commit

Permalink
MDL-16982 Administration: Integrated Inaki's suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajesh Taneja committed Jun 11, 2013
1 parent 57d135a commit d836e3e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 53 deletions.
44 changes: 18 additions & 26 deletions auth/ldap/auth.php
Expand Up @@ -129,12 +129,11 @@ function init_plugin($authtype) {
// 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] ';
Expand Down Expand Up @@ -294,10 +293,7 @@ function get_userinfo($username) {
} 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;
}
}
Expand Down Expand Up @@ -1164,17 +1160,18 @@ function user_update($olduser, $newuser) {
$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
Expand All @@ -1187,14 +1184,13 @@ function user_update($olduser, $newuser) {
$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) {
Expand Down Expand Up @@ -1453,26 +1449,22 @@ function ldap_unix2expirationtime($time) {

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])) {
$moodleattributes[$field] = explode(',', $moodleattributes[$field]); // split ?
}
}
}

// 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;
}
Expand Down
17 changes: 7 additions & 10 deletions lib/authlib.php
Expand Up @@ -121,7 +121,7 @@ class auth_plugin_base {
* Moodle custom fields to sync with.
* @var array()
*/
var $custom_fields = null;
var $customfields = null;

/**
Expand Down Expand Up @@ -535,23 +535,20 @@ function loginpage_idp_list($wantsurl) {
*/
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;
}

}
Expand Down
37 changes: 20 additions & 17 deletions lib/moodlelib.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand All @@ -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));
}
}
}
}
Expand Down

0 comments on commit d836e3e

Please sign in to comment.