From 0c4807abba5da9debff1c2c98222cca7af15f4bc Mon Sep 17 00:00:00 2001 From: skodak Date: Mon, 5 Nov 2007 09:36:46 +0000 Subject: [PATCH] MDL-11079 detect/prevent email duplicates when uploading users --- admin/uploaduser.php | 46 +++++++++++++++++++++++++++++---------- admin/uploaduser_form.php | 9 ++++++++ lang/en_utf8/admin.php | 1 + lang/en_utf8/error.php | 1 + 4 files changed, 46 insertions(+), 11 deletions(-) diff --git a/admin/uploaduser.php b/admin/uploaduser.php index 3be938c3a4..5cbe006d2e 100755 --- a/admin/uploaduser.php +++ b/admin/uploaduser.php @@ -58,7 +58,7 @@ $strcannotassignrole = get_string('cannotassignrole', 'error'); $strduplicateusername = get_string('duplicateusername', 'error'); $struserauthunsupported = get_string('userauthunsupported', 'error'); - +$stremailduplicate = get_string('useremailduplicate', 'error');; $errorstr = get_string('error'); @@ -72,7 +72,7 @@ $STD_FIELDS = array('firstname', 'lastname', 'username', 'email', 'city', 'count $PRF_FIELDS = array(); -if ($prof_fields = $fields = get_records_select('user_info_field')) { +if ($prof_fields = get_records('user_info_field')) { foreach ($prof_fields as $prof_field) { $PRF_FIELDS[] = 'profile_field_'.$prof_field->shortname; } @@ -128,12 +128,13 @@ if ($formdata = $mform->is_cancelled()) { $optype = $formdata->uutype; - $createpasswords = (!empty($formdata->uupasswordnew) and $optype != UU_UPDATE); - $updatepasswords = (!empty($formdata->uupasswordold) and $optype != UU_ADDNEW and $optype != UU_ADDINC); - $allowrenames = (!empty($formdata->uuallowrenames) and $optype != UU_ADDNEW and $optype != UU_ADDINC); - $allowdeletes = (!empty($formdata->uuallowdeletes) and $optype != UU_ADDNEW and $optype != UU_ADDINC); - $updatetype = isset($formdata->uuupdatetype) ? $formdata->uuupdatetype : 0; - $bulk = $formdata->uubulk; + $createpasswords = (!empty($formdata->uupasswordnew) and $optype != UU_UPDATE); + $updatepasswords = (!empty($formdata->uupasswordold) and $optype != UU_ADDNEW and $optype != UU_ADDINC); + $allowrenames = (!empty($formdata->uuallowrenames) and $optype != UU_ADDNEW and $optype != UU_ADDINC); + $allowdeletes = (!empty($formdata->uuallowdeletes) and $optype != UU_ADDNEW and $optype != UU_ADDINC); + $updatetype = isset($formdata->uuupdatetype) ? $formdata->uuupdatetype : 0; + $bulk = $formdata->uubulk; + $noemailduplicates = $formdata->uunoemailduplicates; // verification moved to two places: after upload and into form2 $usersnew = 0; @@ -337,7 +338,7 @@ if ($formdata = $mform->is_cancelled()) { continue; } - if ($olduser = get_record('user', 'username', addslashes($oldusername), 'mnethostid', $user->mnethostid)) { + if ($olduser = get_record('user', 'username', addslashes($oldusername), 'mnethostid', addslashes($user->mnethostid))) { $upt->track('id', $olduser->id, 'normal', false); if (has_capability('moodle/site:doanything', $systemcontext, $olduser->id)) { $upt->track('status', $strusernotrenamedadmin, 'error'); @@ -438,6 +439,18 @@ if ($formdata = $mform->is_cancelled()) { continue; } if ($existinguser->$column !== $user->$column) { + if ($column == 'email') { + if (record_exists('user', 'email', addslashes($user->email))) { + if ($noemailduplicates) { + $upt->track('email', $stremailduplicate, 'error'); + $upt->track('status', $strusernotupdated, 'error'); + $userserrors++; + continue 2; + } else { + $upt->track('email', $stremailduplicate, 'warning'); + } + } + } if ($column != 'password' and in_array($column, $upt->columns)) { $upt->track($column, '', 'normal', false); // clear previous $upt->track($column, $existinguser->$column.'-->'.$user->$column, 'info'); @@ -499,6 +512,17 @@ if ($formdata = $mform->is_cancelled()) { } } + if (record_exists('user', 'email', addslashes($user->email))) { + if ($noemailduplicates) { + $upt->track('email', $stremailduplicate, 'error'); + $upt->track('status', $strusernotaddederror, 'error'); + $userserrors++; + continue; + } else { + $upt->track('email', $stremailduplicate, 'warning'); + } + } + if ($user->id = insert_record('user', addslashes_recursive($user))) { $info = ': ' . $user->username .' (ID = ' . $user->id . ')'; $upt->track('status', $struseradded); @@ -535,7 +559,7 @@ if ($formdata = $mform->is_cancelled()) { $shortname = $user->{'course'.$i}; if (!array_key_exists($shortname, $ccache)) { - if (!$course = get_record('course', 'shortname', $shortname, '', '', '', '', 'id, shortname, defaultrole')) { + if (!$course = get_record('course', 'shortname', addslashes($shortname), '', '', '', '', 'id, shortname, defaultrole')) { $upt->track('enrolments', get_string('unknowncourse', 'error', $shortname), 'error'); continue; } @@ -837,7 +861,7 @@ function increment_username($username, $mnethostid) { $username = $matches[1][0].($matches[2][0]+1); } - if (record_exists('user', 'username', addslashes($username), 'mnethostid', $mnethostid)) { + if (record_exists('user', 'username', addslashes($username), 'mnethostid', addslashes($mnethostid))) { return increment_username($username, $mnethostid); } else { return $username; diff --git a/admin/uploaduser_form.php b/admin/uploaduser_form.php index 4eefd7bee4..6db55297ef 100644 --- a/admin/uploaduser_form.php +++ b/admin/uploaduser_form.php @@ -61,6 +61,7 @@ class admin_uploaduser_form2 extends moodleform { $choices = array(0 => get_string('infilefield', 'auth'), 1 => get_string('createpasswordifneeded', 'auth')); $mform->addElement('select', 'uupasswordnew', get_string('uupasswordnew', 'admin'), $choices); + $mform->setDefault('uupasswordnew', 0); $mform->disabledIf('uupasswordnew', 'uutype', 'eq', UU_UPDATE); $choices = array(0 => get_string('nochanges', 'admin'), @@ -68,29 +69,37 @@ class admin_uploaduser_form2 extends moodleform { 2 => get_string('uuupdateall', 'admin'), 3 => get_string('uuupdatemissing', 'admin')); $mform->addElement('select', 'uuupdatetype', get_string('uuupdatetype', 'admin'), $choices); + $mform->setDefault('uuupdatetype', 0); $mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_ADDNEW); $mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_ADDINC); $choices = array(0 => get_string('nochanges', 'admin'), 1 => get_string('update')); $mform->addElement('select', 'uupasswordold', get_string('uupasswordold', 'admin'), $choices); + $mform->setDefault('uupasswordold', 0); $mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_ADDNEW); $mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_ADDINC); $mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 0); $mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 3); $mform->addElement('selectyesno', 'uuallowrenames', get_string('allowrenames', 'admin')); + $mform->setDefault('uuallowrenames', 0); $mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_ADDNEW); $mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_ADDINC); $mform->addElement('selectyesno', 'uuallowdeletes', get_string('allowdeletes', 'admin')); + $mform->setDefault('uuallowdeletes', 0); $mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_ADDNEW); $mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_ADDINC); + $mform->addElement('selectyesno', 'uunoemailduplicates', get_string('uunoemailduplicates', 'admin')); + $mform->setDefault('uunoemailduplicates', 0); + $choices = array(0 => get_string('no'), 1 => get_string('uubulknew', 'admin'), 2 => get_string('uubulkupdated', 'admin'), 3 => get_string('uubulkall', 'admin')); $mform->addElement('select', 'uubulk', get_string('uubulk', 'admin'), $choices); + $mform->setDefault('uubulk', 0); // roles selection $showroles = false; diff --git a/lang/en_utf8/admin.php b/lang/en_utf8/admin.php index 417e95d24e..d1f1d59476 100644 --- a/lang/en_utf8/admin.php +++ b/lang/en_utf8/admin.php @@ -659,6 +659,7 @@ $string['uucoursedefaultrole'] = 'Default course role'; $string['uulegacy1role'] = '(Original Student) typeN=1'; $string['uulegacy2role'] = '(Original Teacher) typeN=2'; $string['uulegacy3role'] = '(Original Non-editing teacher) typeN=3'; +$string['uunoemailduplicates'] = 'Prevent email address duplicates'; $string['uuoptype_addinc'] = 'Add all, append counter to usernames if needed'; $string['uuoptype_addnew'] = 'Add new only, skip existing users'; $string['uuoptype_addupdate'] = 'Add new and update existing users'; diff --git a/lang/en_utf8/error.php b/lang/en_utf8/error.php index fda552b925..a1e12c8425 100644 --- a/lang/en_utf8/error.php +++ b/lang/en_utf8/error.php @@ -97,6 +97,7 @@ $string['unknownrole'] = 'Unknown role \"$a\"'; $string['unknownuseraction'] = 'Sorry, I do not understand this user action.'; $string['userautherror'] = 'Unknown auth plugin.'; $string['userauthunsupported'] = 'Auth plugin not supported here.'; +$string['useremailduplicate'] = 'Duplicate address'; $string['usernotaddedadmin'] = 'Can not delete admin accounts.'; $string['usernotaddederror'] = 'User not added - error.'; $string['usernotaddedregistered'] = 'User not added - already registered.'; -- 2.39.5