Skip to content

Commit

Permalink
MDL-36602 support mnethostid in user csv upload tool
Browse files Browse the repository at this point in the history
Remote users can not be created, updated or deleted. The only allowed operations are suspending, enrolling and adding to cohort or group.
  • Loading branch information
skodak committed Jan 19, 2013
1 parent a5ec499 commit 9dcae46
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
71 changes: 59 additions & 12 deletions admin/tool/uploaduser/index.php
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -337,7 +377,7 @@

// delete user
if (!empty($user->deleted)) {
if (!$allowdeletes) {
if (!$allowdeletes or $remoteuser) {
$usersskipped++;
$upt->track('status', $strusernotdeletedoff, 'warning');
continue;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions admin/tool/uploaduser/lang/en/tool_uploaduser.php
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/uploaduser/version.php
Expand Up @@ -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)

0 comments on commit 9dcae46

Please sign in to comment.