MDL-29320 user: Make email query case-insensitive
authorJun Pataleta <jun@moodle.com>
Thu, 21 Feb 2019 08:30:54 +0000 (16:30 +0800)
committerJun Pataleta <jun@moodle.com>
Tue, 30 Apr 2019 08:19:20 +0000 (16:19 +0800)
user/edit_form.php
user/editadvanced_form.php
user/emailupdate.php

index 4f2214f..595c733 100644 (file)
@@ -210,10 +210,18 @@ class user_edit_form extends moodleform {
             // Mail not confirmed yet.
         } else if (!validate_email($usernew->email)) {
             $errors['email'] = get_string('invalidemail');
-        } else if (($usernew->email !== $user->email)
-                and empty($CFG->allowaccountssameemail)
-                and $DB->record_exists('user', array('email' => $usernew->email, 'mnethostid' => $CFG->mnet_localhost_id))) {
-            $errors['email'] = get_string('emailexists');
+        } else if (($usernew->email !== $user->email) && empty($CFG->allowaccountssameemail)) {
+            // Make a case-insensitive query for the given email address.
+            $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
+            $params = array(
+                'email' => $usernew->email,
+                'mnethostid' => $CFG->mnet_localhost_id,
+                'userid' => $usernew->id
+            );
+            // If there are other user(s) that already have the same email, show an error.
+            if ($DB->record_exists_select('user', $select, $params)) {
+                $errors['email'] = get_string('emailexists');
+            }
         }
 
         if (isset($usernew->email) and $usernew->email === $user->email and over_bounce_threshold($user)) {
index 31dd330..6506249 100644 (file)
@@ -298,9 +298,18 @@ class user_editadvanced_form extends moodleform {
         if (!$user or (isset($usernew->email) && $user->email !== $usernew->email)) {
             if (!validate_email($usernew->email)) {
                 $err['email'] = get_string('invalidemail');
-            } else if (empty($CFG->allowaccountssameemail)
-                    and $DB->record_exists('user', array('email' => $usernew->email, 'mnethostid' => $CFG->mnet_localhost_id))) {
-                $err['email'] = get_string('emailexists');
+            } else if (empty($CFG->allowaccountssameemail)) {
+                // Make a case-insensitive query for the given email address.
+                $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
+                $params = array(
+                    'email' => $usernew->email,
+                    'mnethostid' => $CFG->mnet_localhost_id,
+                    'userid' => $usernew->id
+                );
+                // If there are other user(s) that already have the same email, show an error.
+                if ($DB->record_exists_select('user', $select, $params)) {
+                    $err['email'] = get_string('emailexists');
+                }
             }
         }
 
index bfc3945..4080ead 100644 (file)
@@ -60,22 +60,32 @@ if (empty($preferences['newemailattemptsleft'])) {
     $user->email = $preferences['newemail'];
 
     // Detect duplicate before saving.
-    if ($DB->get_record('user', array('email' => $user->email))) {
-        redirect(new moodle_url('/user/view.php', ['id' => $user->id]), get_string('emailnowexists', 'auth'));
-    } else {
-        // Update user email.
-        $authplugin = get_auth_plugin($user->auth);
-        $authplugin->user_update($olduser, $user);
-        user_update_user($user, false);
-        $a->email = $user->email;
-        redirect(
-                new moodle_url('/user/view.php', ['id' => $user->id]),
-                get_string('emailupdatesuccess', 'auth', $a),
-                null,
-                \core\output\notification::NOTIFY_SUCCESS
-            );
+    if (empty($CFG->allowaccountssameemail)) {
+        // Make a case-insensitive query for the given email address.
+        $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
+        $params = array(
+            'email' => $user->email,
+            'mnethostid' => $CFG->mnet_localhost_id,
+            'userid' => $user->id
+        );
+        // If there are other user(s) that already have the same email, cancel and redirect.
+        if ($DB->record_exists_select('user', $select, $params)) {
+            redirect(new moodle_url('/user/view.php', ['id' => $user->id]), get_string('emailnowexists', 'auth'));
+        }
     }
 
+    // Update user email.
+    $authplugin = get_auth_plugin($user->auth);
+    $authplugin->user_update($olduser, $user);
+    user_update_user($user, false);
+    $a->email = $user->email;
+    redirect(
+        new moodle_url('/user/view.php', ['id' => $user->id]),
+        get_string('emailupdatesuccess', 'auth', $a),
+        null,
+        \core\output\notification::NOTIFY_SUCCESS
+    );
+
 } else {
     $preferences['newemailattemptsleft']--;
     set_user_preference('newemailattemptsleft', $preferences['newemailattemptsleft'], $user->id);