Skip to content

Commit

Permalink
Merge branch 'wip-mdl-16982' of git://github.com/rajeshtaneja/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Jun 17, 2013
2 parents e9d0112 + d8372b5 commit 03d34e0
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 25 deletions.
29 changes: 18 additions & 11 deletions admin/auth_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@
// 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'));
Expand All @@ -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"} = '';
}
Expand All @@ -128,14 +135,18 @@ 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');
} elseif (preg_match('/^(.+?)(\d+)$/', $fieldname, $matches)) {
$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);
}
Expand All @@ -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);
Expand All @@ -175,5 +184,3 @@ function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts,
echo '</tr>';
}
}


2 changes: 1 addition & 1 deletion auth/cas/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,6 @@ <h4><?php print_string('auth_sync_script', 'auth') ?></h4>
$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>
26 changes: 21 additions & 5 deletions auth/ldap/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -1160,11 +1160,19 @@ 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
$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
Expand All @@ -1177,9 +1185,9 @@ function user_update($olduser, $newuser) {
$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;
Expand Down Expand Up @@ -1442,7 +1450,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])) {
Expand Down
2 changes: 1 addition & 1 deletion auth/ldap/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,6 @@ <h4><?php print_string('auth_ntlmsso', 'auth_ldap') ?></h4>
$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>
29 changes: 29 additions & 0 deletions lib/authlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -522,6 +528,29 @@ function loginpage_idp_list($wantsurl) {
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;
}

}

/**
Expand Down
27 changes: 20 additions & 7 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
}
}
Expand All @@ -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)));
Expand Down

0 comments on commit 03d34e0

Please sign in to comment.