Skip to content

Commit

Permalink
MDL-55062 tool_uploaduser: prevent defaults when not selected
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Dec 13, 2016
1 parent 45fe30f commit e4f1771
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
58 changes: 30 additions & 28 deletions admin/tool/uploaduser/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,38 +358,40 @@

// add default values for remaining fields
$formdefaults = array();
foreach ($STD_FIELDS as $field) {
if (isset($user->$field)) {
continue;
}
// all validation moved to form2
if (isset($formdata->$field)) {
// process templates
$user->$field = uu_process_template($formdata->$field, $user);
$formdefaults[$field] = true;
if (in_array($field, $upt->columns)) {
$upt->track($field, s($user->$field), 'normal');
if ($updatetype != UU_UPDATE_FILEOVERRIDE && $updatetype != UU_UPDATE_NOCHANGES) {
foreach ($STD_FIELDS as $field) {
if (isset($user->$field)) {
continue;
}
// all validation moved to form2
if (isset($formdata->$field)) {
// process templates
$user->$field = uu_process_template($formdata->$field, $user);
$formdefaults[$field] = true;
if (in_array($field, $upt->columns)) {
$upt->track($field, s($user->$field), 'normal');
}
}
}
}
foreach ($PRF_FIELDS as $field) {
if (isset($user->$field)) {
continue;
}
if (isset($formdata->$field)) {
// process templates
$user->$field = uu_process_template($formdata->$field, $user);

// Form contains key and later code expects value.
// Convert key to value for required profile fields.
require_once($CFG->dirroot.'/user/profile/field/'.$proffields[$field]->datatype.'/field.class.php');
$profilefieldclass = 'profile_field_'.$proffields[$field]->datatype;
$profilefield = new $profilefieldclass($proffields[$field]->id);
if (method_exists($profilefield, 'convert_external_data')) {
$user->$field = $profilefield->edit_save_data_preprocess($user->$field, null);
foreach ($PRF_FIELDS as $field) {
if (isset($user->$field)) {
continue;
}
if (isset($formdata->$field)) {
// process templates
$user->$field = uu_process_template($formdata->$field, $user);

// Form contains key and later code expects value.
// Convert key to value for required profile fields.
require_once($CFG->dirroot.'/user/profile/field/'.$proffields[$field]->datatype.'/field.class.php');
$profilefieldclass = 'profile_field_'.$proffields[$field]->datatype;
$profilefield = new $profilefieldclass($proffields[$field]->id);
if (method_exists($profilefield, 'convert_external_data')) {
$user->$field = $profilefield->edit_save_data_preprocess($user->$field, null);
}

$formdefaults[$field] = true;
$formdefaults[$field] = true;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions admin/tool/uploaduser/lang/en/tool_uploaduser.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
$string['encoding'] = 'Encoding';
$string['errormnetadd'] = 'Can not add remote users';
$string['errors'] = 'Errors';
$string['invalidupdatetype'] = 'You can not select this option with the chosen \'Upload type\'';
$string['invaliduserdata'] = 'Invalid data detected for user {$a} and it has been automatically cleaned.';
$string['nochanges'] = 'No changes';
$string['pluginname'] = 'User upload';
Expand Down
7 changes: 7 additions & 0 deletions admin/tool/uploaduser/user_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ function validation($data, $files) {
$errors = parent::validation($data, $files);
$columns = $this->_customdata['columns'];
$optype = $data['uutype'];
$updatetype = $data['uuupdatetype'];

// detect if password column needed in file
if (!in_array('password', $columns)) {
Expand Down Expand Up @@ -377,6 +378,12 @@ function validation($data, $files) {
}
}

// If the 'Existing user details' value is set we need to ensure that the
// 'Upload type' is not set to something invalid.
if (!empty($updatetype) && ($optype == UU_USER_ADDNEW || $optype == UU_USER_ADDINC)) {
$errors['uuupdatetype'] = get_string('invalidupdatetype', 'tool_uploaduser');
}

// look for other required data
if ($optype != UU_USER_UPDATE) {
$requiredusernames = useredit_get_required_name_fields();
Expand Down

0 comments on commit e4f1771

Please sign in to comment.