Permalink
Browse files

MDL-16982 Administration: Adding data mapping for custom user fields

  • Loading branch information...
1 parent b3be471 commit b88adb55fc1ba3d8e4c43aa6855567f72cde7712 @leblangi leblangi committed with rajeshtaneja Apr 12, 2012
Showing with 192 additions and 16 deletions.
  1. +70 −3 admin/auth_config.php
  2. +13 −1 auth/cas/auth.php
  3. +1 −1 auth/cas/config.html
  4. +50 −7 auth/ldap/auth.php
  5. +1 −1 auth/ldap/config.html
  6. +57 −3 lib/moodlelib.php
View
73 admin/auth_config.php
@@ -49,6 +49,7 @@
}
$user_fields = $authplugin->userfields;
+$custom_fields = $authplugin->custom_fields;
//$user_fields = array("firstname", "lastname", "email", "phone1", "phone2", "institution", "department", "address", "city", "country", "description", "idnumber", "lang");
/// Get the auth title (from core or own auth lang files)
@@ -72,7 +73,7 @@
echo $authdescription;
echo $OUTPUT->box_end();
echo "<hr />\n";
-$authplugin->config_form($frm, $err, $user_fields);
+$authplugin->config_form($frm, $err, $user_fields, $custom_fields);
echo $OUTPUT->box_end();
echo '<p style="text-align: center"><input type="submit" value="' . get_string("savechanges") . "\" /></p>\n";
echo "</div>\n";
@@ -87,8 +88,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, $custom_fields = array()) {
+ global $DB, $OUTPUT;
echo '<tr><td colspan="3">';
if ($retrieveopts) {
echo $OUTPUT->heading(get_string('auth_data_mapping', 'auth'));
@@ -174,6 +175,72 @@ function print_auth_lock_options ($auth, $user_fields, $helptext, $retrieveopts,
}
echo '</tr>';
}
+ if(!empty($custom_fields)) {
+ echo '<tr><td colspan="3">';
+
+ echo '<h4>' . get_string('profilefields', 'admin') . '</h4>';
+
+ echo '</td></tr>';
+
+ foreach($custom_fields as $field) {
+
+ // Define some vars we'll work with
+ if (!isset($pluginconfig->{"field_map_$field"})) {
+ $pluginconfig->{"field_map_$field"} = '';
+ }
+ if (!isset($pluginconfig->{"field_updatelocal_$field"})) {
+ $pluginconfig->{"field_updatelocal_$field"} = '';
+ }
+ if (!isset($pluginconfig->{"field_updateremote_$field"})) {
+ $pluginconfig->{"field_updateremote_$field"} = '';
+ }
+ if (!isset($pluginconfig->{"field_lock_$field"})) {
+ $pluginconfig->{"field_lock_$field"} = '';
+ }
+
+ // 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');
+ } else {
+ $fieldname = $DB->get_field('user_info_field', 'name', array('shortname'=>$fieldname));
+ }
+ if ($retrieveopts) {
+ $varname = 'field_map_' . $field;
+
+ echo '<tr valign="top"><td align="right">';
+ echo '<label for="lockconfig_'.$varname.'">'.$fieldname.'</label>';
+ echo '</td><td>';
+
+ echo "<input id=\"lockconfig_{$varname}\" name=\"lockconfig_{$varname}\" type=\"text\" size=\"30\" value=\"{$pluginconfig->$varname}\" />";
+ echo '<div style="text-align: right">';
+ echo '<label for="menulockconfig_field_updatelocal_'.$field.'">'.get_string('auth_updatelocal', 'auth') . '</label>&nbsp;';
+ echo html_writer::select($updatelocaloptions, "lockconfig_field_updatelocal_{$field}", $pluginconfig->{"field_updatelocal_$field"}, false);
+ echo '<br />';
+ if ($updateopts) {
+ 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);
+ echo '</div>';
+ } else {
+ echo '<tr valign="top"><td align="right">';
+ echo '<label for="menulockconfig_field_lock_'.$field.'">'.$fieldname.'</label>';
+ echo '</td><td>';
+ echo html_writer::select($lockoptions, "lockconfig_field_lock_{$field}", $pluginconfig->{"field_lock_$field"}, false);
+ }
+ echo '</td>';
+ echo '</tr>';
+ }
+ }
}
View
14 auth/cas/auth.php
@@ -37,13 +37,25 @@
class auth_plugin_cas extends auth_plugin_ldap {
/**
+ * moodle custom fields to sync with
+ * @var array()
+ */
+ var $custom_fields = array();
+ /*
+ /**
* Constructor.
*/
function auth_plugin_cas() {
+ global $DB;
$this->authtype = 'cas';
$this->roleauth = 'auth_cas';
$this->errorlogtag = '[AUTH CAS] ';
$this->init_plugin($this->authtype);
+ $custom_fields = $DB->get_records('user_info_field');
+
+ foreach($custom_fields as $cf) {
+ $this->custom_fields[] = $cf->shortname;
+ }
}
function prevent_local_passwords() {
@@ -222,7 +234,7 @@ function connectCAS() {
*
* @param array $page An object containing all the data for this page.
*/
- function config_form($config, $err, $user_fields) {
+ function config_form($config, $err, $user_fields, $custom_fields=array()) {
global $CFG, $OUTPUT;
if (!function_exists('ldap_connect')) { // Is php-ldap really there?
View
2 auth/cas/config.html
@@ -491,6 +491,6 @@
$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, $custom_fields);
?>
</table>
View
57 auth/ldap/auth.php
@@ -130,14 +130,27 @@ function init_plugin($authtype) {
}
}
+
+ /**
+ * moodle custom fields to sync with
+ * @var array()
+ */
+ var $custom_fields = array();
+ /*
+
/**
* Constructor with initialisation.
*/
function auth_plugin_ldap() {
+ global $DB;
$this->authtype = 'ldap';
$this->roleauth = 'auth_ldap';
$this->errorlogtag = '[AUTH LDAP] ';
$this->init_plugin($this->authtype);
+ $custom_fields = $DB->get_records('user_info_field');
+ foreach($custom_fields as $cf) {
+ $this->custom_fields[] = $cf->shortname;
+ }
}
/**
@@ -293,7 +306,9 @@ function get_userinfo($username) {
} else {
$newval = textlib::convert($entry[$value], $this->config->ldapencoding, 'utf-8');
}
- if (!empty($newval)) { // favour ldap entries that are set
+
+ // 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
$ldapval = $newval;
}
}
@@ -1162,9 +1177,23 @@ function user_update($olduser, $newuser) {
foreach ($attrmap as $key => $ldapkeys) {
// Only process if the moodle field ($key) has changed and we
// are set to update LDAP with it
+
+ //updating a custom field or a standard field/ 0 for none at all
+ $update_attr_type = 0;
+
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) {
+ //updating a user profile field
+ $update_attr_type = 2;
+ $profile_field = $key;
+ } else if(isset($olduser->{'profile_field_' . $key}) and isset($newuser->{'profile_field_' . $key})
+ and $olduser->{'profile_field_' . $key} !== $newuser->{'profile_field_' . $key}){
+ //updating a custom profile field
+ $update_attr_type = 1;
+ $profile_field = 'profile_field_' . $key;
+ }
+
+ if (!empty($profile_field) and !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,13 +1206,14 @@ function user_update($olduser, $newuser) {
$ambiguous = false;
}
- $nuvalue = textlib::convert($newuser->$key, 'utf-8', $this->config->ldapencoding);
+ $nuvalue = textlib::convert($newuser->$profile_field, 'utf-8', $this->config->ldapencoding);
empty($nuvalue) ? $nuvalue = array() : $nuvalue;
- $ouvalue = textlib::convert($olduser->$key, 'utf-8', $this->config->ldapencoding);
+ $ouvalue = textlib::convert($olduser->$profile_field, 'utf-8', $this->config->ldapencoding);
foreach ($ldapkeys as $ldapkey) {
$ldapkey = $ldapkey;
- $ldapvalue = $user_entry[$ldapkey][0];
+ $ldapvalue = empty($user_entry[$ldapkey][0])? null: $ldapvalue;
+
if (!$ambiguous) {
// Skip update if the values already match
if ($nuvalue !== $ldapvalue) {
@@ -1450,6 +1480,19 @@ function ldap_attributes () {
}
}
}
+
+ //add custom fields
+ foreach($this->custom_fields as $field) {
+ if(!in_array($field, $this->userfields)) { //just in case so we don't overwrite any values in the user fields
+ if(!empty($this->config->{"field_map_$field"})) {
+ $moodleattributes[$field] = $this->config->{"field_map_$field"};
+ if (preg_match('/,/',$moodleattributes[$field])) {
+ $moodleattributes[$field] = explode(',', $moodleattributes[$field]); // split ?
+ }
+ }
+ }
+ }
+
$moodleattributes['username'] = textlib::strtolower(trim($this->config->user_attribute));
return $moodleattributes;
}
@@ -1749,7 +1792,7 @@ function sync_roles($user) {
*
* @param array $page An object containing all the data for this page.
*/
- function config_form($config, $err, $user_fields) {
+ function config_form($config, $err, $user_fields, $custom_fields=array()) {
global $CFG, $OUTPUT;
if (!function_exists('ldap_connect')) { // Is php-ldap really there?
View
2 auth/ldap/config.html
@@ -604,6 +604,6 @@
$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, $custom_fields);
?>
</table>
View
60 lib/moodlelib.php
@@ -3846,11 +3846,21 @@ function create_user_record($username, $password, $auth = 'manual') {
$authplugin = get_auth_plugin($auth);
$newuser = new stdClass();
-
+ $customfield = 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)) {
+ $newuser->$key = $value;
+ }else if(isset($authplugin->custom_fields) && in_array($key, $authplugin->custom_fields)) {
+ $info_field = $DB->get_record('user_info_field',array('shortname'=>$key));
+ $data = $info_field->defaultdata;
+ if(strcmp($data, $value) !== 0) {
+ $customfield->$key = new stdClass();
+ $customfield->$key->fieldid = $info_field->id;
+ $customfield->$key->data = $value;
+ }
+ }
}
}
@@ -3880,6 +3890,12 @@ function create_user_record($username, $password, $auth = 'manual') {
$newuser->mnethostid = $CFG->mnet_localhost_id;
$newuser->id = $DB->insert_record('user', $newuser);
+
+ foreach ($customfield as $key) {
+ $key->userid = $newuser->id;
+ $key->id = $DB->insert_record("user_info_data",$key);
+ }
+
$user = get_complete_user_data('id', $newuser->id);
if (!empty($CFG->{'auth_'.$newuser->auth.'_forcechangepassword'})){
set_user_preference('auth_forcepasswordchange', 1, $user);
@@ -3914,7 +3930,8 @@ function update_user_record($username) {
$newinfo = truncate_userinfo($newinfo);
foreach ($newinfo as $key => $value){
$key = strtolower($key);
- if (!property_exists($oldinfo, $key) or $key === 'username' or $key === 'id'
+ $iscustom = in_array($key, $userauth->custom_fields);
+ 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,9 +3949,46 @@ 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(in_array($key, $userauth->userfields)) {
if ((string)$oldinfo->$key !== (string)$value) {
$newuser[$key] = (string)$value;
}
+ }else if($iscustom) {
+
+ //if there is no value in the user_info_data then
+ $info_field = $DB->get_record('user_info_field',array('shortname'=>$key));
+ $userid = $DB->get_field('user', 'id', array('username'=>$username));
+ $data = $DB->get_field('user_info_data', 'data', array('userid'=>$userid, 'fieldid'=>$info_field->id));
+ if($data === false) {
+ $data = $info_field->defaultdata;
+ if(strcmp($data, $value) !== 0) {
+ $row = new stdClass();
+ $row->userid = $userid;
+ $row->fieldid = $info_field->id;
+ $row->data = $data;
+ $row->id = $DB->insert_record("user_info_data", $row);
+ }
+ }
+
+ if(strcmp($data, $value) !== 0) {
+ $valid = true;
+
+ //check to make sure that the value we are placing in is a valid one
+ if(strcmp($info_field->datatype, 'menu') == 0){
+ $validValues = explode("\n", $info_field->param1);
+ if(!in_array($value, $validValues)) {
+ $valid = false;
+ }
+ } else if(strcmp($info_field->datatype, 'checkbox') == 0) {
+ if($value != 1 && $value != 0) {
+ $valid = false;
+ }
+ }
+ if($valid) {
+ $DB->set_field('user_info_data','data',$value, array('userid'=>$userid, 'fieldid'=>$info_field->id));
+ }
+ }
+ }
}
}
}

0 comments on commit b88adb5

Please sign in to comment.