Merge branch 'wip-mdl-16982' of git://github.com/rajeshtaneja/moodle

This commit is contained in:
Sam Hemelryk 2013-06-18 10:04:18 +12:00
commit 03d34e07c0
6 changed files with 90 additions and 25 deletions

View File

@ -87,8 +87,8 @@ exit;
// but some may want a custom one if they are offering
// other options
// Note: lockconfig_ fields have special handling.
function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts, $updateopts) {
global $OUTPUT;
function print_auth_lock_options($auth, $user_fields, $helptext, $retrieveopts, $updateopts, $customfields = array()) {
global $DB, $OUTPUT;
echo '<tr><td colspan="3">';
if ($retrieveopts) {
echo $OUTPUT->heading(get_string('auth_data_mapping', 'auth'));
@ -107,14 +107,21 @@ function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts,
$pluginconfig = get_config("auth/$auth");
// helptext is on a field with rowspan
// Helptext is on a field with rowspan.
if (empty($helptext)) {
$helptext = '&nbsp;';
$helptext = '&nbsp;';
}
foreach ($user_fields as $field) {
// If we have custom fields then merge them with user fields.
if (!empty($customfields)) {
$user_fields = array_merge($user_fields, $customfields);
}
// Define some vars we'll work with
if (!empty($customfields)) {
$customfieldname = $DB->get_records('user_info_field', null, '', 'shortname, name');
}
foreach ($user_fields as $field) {
// Define some vars we'll work with.
if (!isset($pluginconfig->{"field_map_$field"})) {
$pluginconfig->{"field_map_$field"} = '';
}
@ -128,7 +135,7 @@ function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts,
$pluginconfig->{"field_lock_$field"} = '';
}
// define the fieldname we display to the user
// Define the fieldname we display to the user.
$fieldname = $field;
if ($fieldname === 'lang') {
$fieldname = get_string('language');
@ -136,6 +143,10 @@ function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts,
$fieldname = get_string($matches[1]) . ' ' . $matches[2];
} elseif ($fieldname == 'url') {
$fieldname = get_string('webpage');
} elseif (!empty($customfields) && in_array($field, $customfields)) {
// If custom field then pick name from database.
$fieldshortname = str_replace('profile_field_', '', $fieldname);
$fieldname = $customfieldname[$fieldshortname]->name;
} else {
$fieldname = get_string($fieldname);
}
@ -155,8 +166,6 @@ function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts,
echo '<label for="menulockconfig_field_updateremote_'.$field.'">'.get_string('auth_updateremote', 'auth') . '</label>&nbsp;';
echo html_writer::select($updateextoptions, "lockconfig_field_updateremote_{$field}", $pluginconfig->{"field_updateremote_$field"}, false);
echo '<br />';
}
echo '<label for="menulockconfig_field_lock_'.$field.'">'.get_string('auth_fieldlock', 'auth') . '</label>&nbsp;';
echo html_writer::select($lockoptions, "lockconfig_field_lock_{$field}", $pluginconfig->{"field_lock_$field"}, false);
@ -175,5 +184,3 @@ function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts,
echo '</tr>';
}
}

View File

@ -491,6 +491,6 @@ $help .= get_string('auth_updateremote_expl', 'auth');
$help .= '<hr />';
$help .= get_string('auth_updateremote_ldap', 'auth');
print_auth_lock_options($this->authtype, $user_fields, $help, true, true);
print_auth_lock_options($this->authtype, $user_fields, $help, true, true, $this->get_custom_user_profile_fields());
?>
</table>

View File

@ -1160,11 +1160,19 @@ 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
$customprofilefield = 'profile_field_' . $key;
if (isset($olduser->$key) and isset($newuser->$key)
and $olduser->$key !== $newuser->$key
and !empty($this->config->{'field_updateremote_'. $key})) {
and ($olduser->$key !== $newuser->$key)) {
$profilefield = $key;
} else if (isset($olduser->$customprofilefield) && isset($newuser->$customprofilefield)
&& $olduser->$customprofilefield !== $newuser->$customprofilefield) {
$profilefield = $customprofilefield;
}
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
@ -1177,9 +1185,9 @@ class auth_plugin_ldap extends auth_plugin_base {
$ambiguous = false;
}
$nuvalue = textlib::convert($newuser->$key, 'utf-8', $this->config->ldapencoding);
$nuvalue = textlib::convert($newuser->$profilefield, 'utf-8', $this->config->ldapencoding);
empty($nuvalue) ? $nuvalue = array() : $nuvalue;
$ouvalue = textlib::convert($olduser->$key, 'utf-8', $this->config->ldapencoding);
$ouvalue = textlib::convert($olduser->$profilefield, 'utf-8', $this->config->ldapencoding);
foreach ($ldapkeys as $ldapkey) {
$ldapkey = $ldapkey;
@ -1442,7 +1450,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])) {

View File

@ -604,6 +604,6 @@ $help .= get_string('auth_updateremote_expl', 'auth');
$help .= '<hr />';
$help .= get_string('auth_updateremote_ldap', 'auth');
print_auth_lock_options($this->authtype, $user_fields, $help, true, true);
print_auth_lock_options($this->authtype, $user_fields, $help, true, true, $this->get_custom_user_profile_fields());
?>
</table>

View File

@ -117,6 +117,12 @@ class auth_plugin_base {
'address'
);
/**
* Moodle custom fields to sync with.
* @var array()
*/
var $customfields = null;
/**
* This is the primary method that is used by the authenticate_user_login()
@ -522,6 +528,29 @@ class auth_plugin_base {
return array();
}
/**
* Return custom user profile fields.
*
* @return array list of custom fields.
*/
public function get_custom_user_profile_fields() {
global $DB;
// If already retrieved then return.
if (!is_null($this->customfields)) {
return $this->customfields;
}
$this->customfields = array();
if ($proffields = $DB->get_records('user_info_field')) {
foreach ($proffields as $proffield) {
$this->customfields[] = 'profile_field_'.$proffield->shortname;
}
}
unset($proffields);
return $this->customfields;
}
}
/**

View File

@ -3839,18 +3839,19 @@ function get_user_fieldnames() {
*/
function create_user_record($username, $password, $auth = 'manual') {
global $CFG, $DB;
require_once($CFG->dirroot."/user/profile/lib.php");
//just in case check text case
$username = trim(textlib::strtolower($username));
$authplugin = get_auth_plugin($auth);
$customfields = $authplugin->get_custom_user_profile_fields();
$newuser = new stdClass();
if ($newinfo = $authplugin->get_userinfo($username)) {
$newinfo = truncate_userinfo($newinfo);
foreach ($newinfo as $key => $value){
$newuser->$key = $value;
if (in_array($key, $authplugin->userfields) || (in_array($key, $customfields))) {
$newuser->$key = $value;
}
}
}
@ -3880,6 +3881,10 @@ function create_user_record($username, $password, $auth = 'manual') {
$newuser->mnethostid = $CFG->mnet_localhost_id;
$newuser->id = $DB->insert_record('user', $newuser);
// Save user profile data.
profile_save_data($newuser);
$user = get_complete_user_data('id', $newuser->id);
if (!empty($CFG->{'auth_'.$newuser->auth.'_forcechangepassword'})){
set_user_preference('auth_forcepasswordchange', 1, $user);
@ -3903,7 +3908,7 @@ function create_user_record($username, $password, $auth = 'manual') {
*/
function update_user_record($username) {
global $DB, $CFG;
require_once($CFG->dirroot."/user/profile/lib.php");
$username = trim(textlib::strtolower($username)); /// just in case check text case
$oldinfo = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id), '*', MUST_EXIST);
@ -3912,9 +3917,12 @@ function update_user_record($username) {
if ($newinfo = $userauth->get_userinfo($username)) {
$newinfo = truncate_userinfo($newinfo);
$customfields = $userauth->get_custom_user_profile_fields();
foreach ($newinfo as $key => $value){
$key = strtolower($key);
if (!property_exists($oldinfo, $key) or $key === 'username' or $key === 'id'
$iscustom = in_array($key, $customfields);
if ((!property_exists($oldinfo, $key) && !$iscustom) or $key === 'username' or $key === 'id'
or $key === 'auth' or $key === 'mnethostid' or $key === 'deleted') {
// unknown or must not be changed
continue;
@ -3932,7 +3940,8 @@ function update_user_record($username) {
// nothing_ for this field. Thus it makes sense to let this value
// stand in until LDAP is giving a value for this field.
if (!(empty($value) && $lockval === 'unlockedifempty')) {
if ((string)$oldinfo->$key !== (string)$value) {
if ($iscustom || (in_array($key, $userauth->userfields) &&
((string)$oldinfo->$key !== (string)$value))) {
$newuser[$key] = (string)$value;
}
}
@ -3942,6 +3951,10 @@ function update_user_record($username) {
$newuser['id'] = $oldinfo->id;
$newuser['timemodified'] = time();
$DB->update_record('user', $newuser);
// Save user profile data.
profile_save_data((object) $newuser);
// fetch full user record for the event, the complete user data contains too much info
// and we want to be consistent with other places that trigger this event
events_trigger('user_updated', $DB->get_record('user', array('id'=>$oldinfo->id)));