mirror of
https://github.com/moodle/moodle.git
synced 2025-04-19 07:25:30 +02:00
MDL-36602 support mnethostid in user csv upload tool
Remote users can not be created, updated or deleted. The only allowed operations are suspending, enrolling and adding to cohort or group.
This commit is contained in:
parent
a5ec499521
commit
9dcae46d93
@ -94,6 +94,7 @@ $STD_FIELDS = array('id', 'firstname', 'lastname', 'username', 'email',
|
||||
'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 @@ if ($formdata = $mform2->is_cancelled()) {
|
||||
$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 @@ if ($formdata = $mform2->is_cancelled()) {
|
||||
|
||||
// delete user
|
||||
if (!empty($user->deleted)) {
|
||||
if (!$allowdeletes) {
|
||||
if (!$allowdeletes or $remoteuser) {
|
||||
$usersskipped++;
|
||||
$upt->track('status', $strusernotdeletedoff, 'warning');
|
||||
continue;
|
||||
@ -475,7 +515,7 @@ if ($formdata = $mform2->is_cancelled()) {
|
||||
$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 @@ if ($formdata = $mform2->is_cancelled()) {
|
||||
// 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 @@ if ($formdata = $mform2->is_cancelled()) {
|
||||
|
||||
$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);
|
||||
|
||||
|
@ -30,6 +30,7 @@ $string['csvdelimiter'] = 'CSV delimiter';
|
||||
$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';
|
||||
|
@ -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)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user