From d836e3ed1f673f017b1df4fbadddd174d21a85ea Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Thu, 31 Jan 2013 13:12:17 +0800 Subject: [PATCH] MDL-16982 Administration: Integrated Inaki's suggestions --- auth/ldap/auth.php | 44 ++++++++++++++++++-------------------------- lib/authlib.php | 17 +++++++---------- lib/moodlelib.php | 37 ++++++++++++++++++++----------------- 3 files changed, 45 insertions(+), 53 deletions(-) diff --git a/auth/ldap/auth.php b/auth/ldap/auth.php index 79a3624095050..6b26a36a47a2b 100644 --- a/auth/ldap/auth.php +++ b/auth/ldap/auth.php @@ -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] '; @@ -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; } } @@ -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 @@ -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) { @@ -1453,7 +1449,15 @@ 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])) { @@ -1461,18 +1465,6 @@ function ldap_attributes () { } } } - - // 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; } diff --git a/lib/authlib.php b/lib/authlib.php index daf14a0f63e11..fd8057e1d10c6 100644 --- a/lib/authlib.php +++ b/lib/authlib.php @@ -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 @@ 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; } } diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 0e9e6fb640274..dae404f5e0368 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -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)); + } } } }