]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-11079 detect/prevent email duplicates when uploading users
authorskodak <skodak>
Mon, 5 Nov 2007 09:36:46 +0000 (09:36 +0000)
committerskodak <skodak>
Mon, 5 Nov 2007 09:36:46 +0000 (09:36 +0000)
admin/uploaduser.php
admin/uploaduser_form.php
lang/en_utf8/admin.php
lang/en_utf8/error.php

index 3be938c3a4d7973a3a8d143353ed60273c98c169..5cbe006d2e604b49fa7f3accd9b04cc011f9bca8 100755 (executable)
@@ -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;
index 4eefd7bee436a2fe7ddeb9432240bcb9b66afb1f..6db55297ef0791be4690b407bf0949c5854a4e8b 100644 (file)
@@ -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;
index 417e95d24ef01e170927615a3f068f29316215c9..d1f1d5947633590174d54e254e5733b93c6f6f33 100644 (file)
@@ -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';
index fda552b9259a69850b89a3f1d770092fbe46773a..a1e12c842550a7e0c2c472fbfd60da5176cceb49 100644 (file)
@@ -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.';