diff --git a/admin/tool/uploaduser/index.php b/admin/tool/uploaduser/index.php index 384a900c7c612..a43a08de75e62 100644 --- a/admin/tool/uploaduser/index.php +++ b/admin/tool/uploaduser/index.php @@ -94,6 +94,7 @@ 'oldusername', // use when renaming users - this is the original username 'suspended', // 1 means suspend user account, 0 means activate user account, nothing means keep as is for existing users 'deleted', // 1 means delete user + 'mnethostid', // Can not be used for adding, updating or deleting of users - only for enrolments, groups, cohorts and suspending. ); $PRF_FIELDS = array(); @@ -285,19 +286,58 @@ $userserrors++; continue; } + if ($user->username !== clean_param($user->username, PARAM_USERNAME)) { $upt->track('status', get_string('invalidusername', 'error', 'username'), 'error'); $upt->track('username', $errorstr, 'error'); $userserrors++; } - if ($existinguser = $DB->get_record('user', array('username'=>$user->username, 'mnethostid'=>$CFG->mnet_localhost_id))) { + + if (empty($user->mnethostid)) { + $user->mnethostid = $CFG->mnet_localhost_id; + } + + if ($existinguser = $DB->get_record('user', array('username'=>$user->username, 'mnethostid'=>$user->mnethostid))) { $upt->track('id', $existinguser->id, 'normal', false); } - // find out in username incrementing required - if ($existinguser and $optype == UU_USER_ADDINC) { - $user->username = uu_increment_username($user->username); - $existinguser = false; + if ($user->mnethostid == $CFG->mnet_localhost_id) { + $remoteuser = false; + + // Find out if username incrementing required. + if ($existinguser and $optype == UU_USER_ADDINC) { + $user->username = uu_increment_username($user->username); + $existinguser = false; + } + + } else { + if (!$existinguser or $optype == UU_USER_ADDINC) { + $upt->track('status', get_string('errormnetadd', 'tool_uploaduser'), 'error'); + $userserrors++; + continue; + } + + $remoteuser = true; + + // Make sure there are no changes of existing fields except the suspended status. + foreach ((array)$existinguser as $k => $v) { + if ($k === 'suspended') { + continue; + } + if (property_exists($user, $k)) { + $user->$k = $v; + } + if (in_array($k, $upt->columns)) { + if ($k === 'password' or $k === 'oldusername' or $k === 'deleted') { + $upt->track($k, '', 'normal', false); + } else { + $upt->track($k, s($v), 'normal', false); + } + } + } + unset($user->oldusername); + unset($user->password); + $user->auth = $existinguser->auth; } // notify about nay username changes @@ -337,7 +377,7 @@ // delete user if (!empty($user->deleted)) { - if (!$allowdeletes) { + if (!$allowdeletes or $remoteuser) { $usersskipped++; $upt->track('status', $strusernotdeletedoff, 'warning'); continue; @@ -475,7 +515,7 @@ $doupdate = false; $dologout = false; - if ($updatetype != UU_UPDATE_NOCHANGES) { + if ($updatetype != UU_UPDATE_NOCHANGES and !$remoteuser) { if (!empty($user->auth) and $user->auth !== $existinguser->auth) { $upt->track('auth', s($existinguser->auth).'-->'.s($user->auth), 'info', false); $existinguser->auth = $user->auth; @@ -573,7 +613,11 @@ // changing of passwords is a special case // do not force password changes for external auth plugins! $oldpw = $existinguser->password; - if (!$isinternalauth) { + + if ($remoteuser) { + // Do not mess with passwords of remote users. + + } else if (!$isinternalauth) { $existinguser->password = 'not cached'; $upt->track('password', '-', 'normal', false); // clean up prefs @@ -609,10 +653,13 @@ $upt->track('status', $struserupdated); $usersupdated++; - // pre-process custom profile menu fields data from csv file - $existinguser = uu_pre_process_custom_profile_data($existinguser); - // save custom profile fields data from csv file - profile_save_data($existinguser); + + if (!$remoteuser) { + // pre-process custom profile menu fields data from csv file + $existinguser = uu_pre_process_custom_profile_data($existinguser); + // save custom profile fields data from csv file + profile_save_data($existinguser); + } events_trigger('user_updated', $existinguser); diff --git a/admin/tool/uploaduser/lang/en/tool_uploaduser.php b/admin/tool/uploaduser/lang/en/tool_uploaduser.php index 74bb55ee81499..13e59926e9766 100644 --- a/admin/tool/uploaduser/lang/en/tool_uploaduser.php +++ b/admin/tool/uploaduser/lang/en/tool_uploaduser.php @@ -30,6 +30,7 @@ $string['defaultvalues'] = 'Default values'; $string['deleteerrors'] = 'Delete errors'; $string['encoding'] = 'Encoding'; +$string['errormnetadd'] = 'Can not add remote users'; $string['errors'] = 'Errors'; $string['nochanges'] = 'No changes'; $string['pluginname'] = 'User upload'; diff --git a/admin/tool/uploaduser/version.php b/admin/tool/uploaduser/version.php index 25e07495242c5..8645f1ccf4fb7 100644 --- a/admin/tool/uploaduser/version.php +++ b/admin/tool/uploaduser/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2012112900; // The current plugin version (Date: YYYYMMDDXX) +$plugin->version = 2012120700; // The current plugin version (Date: YYYYMMDDXX) $plugin->requires = 2012112900; // Requires this Moodle version $plugin->component = 'tool_uploaduser'; // Full name of the plugin (used for diagnostics)