MDL-29320 user: Make email query case-insensitive
[moodle.git] / user / emailupdate.php
CommitLineData
ce221eb5 1<?php
ce221eb5 2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Change a users email address
19 *
20 * @copyright 1999 Martin Dougiamas http://dougiamas.com
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
a2ed6e69 22 * @package core_user
ce221eb5 23 */
184c7638 24
d6ace123 25require_once('../config.php');
26require_once($CFG->libdir.'/adminlib.php');
05c38e2b 27require_once($CFG->dirroot.'/user/editlib.php');
bb78e249 28require_once($CFG->dirroot.'/user/lib.php');
d6ace123 29
30$key = required_param('key', PARAM_ALPHANUM);
31$id = required_param('id', PARAM_INT);
32
a2ed6e69 33$PAGE->set_url('/user/emailupdate.php', array('id' => $id, 'key' => $key));
43731030 34$PAGE->set_context(context_system::instance());
ce221eb5 35
d6ace123 36if (!$user = $DB->get_record('user', array('id' => $id))) {
06e84d52 37 print_error('invaliduserid');
d6ace123 38}
39
05c38e2b 40$preferences = get_user_preferences(null, null, $user->id);
d6ace123 41$a = new stdClass();
42$a->fullname = fullname($user, true);
c6a074f8 43$stremailupdate = get_string('emailupdate', 'auth', $a);
caa8363f 44
45$PAGE->set_title(format_string($SITE->fullname) . ": $stremailupdate");
46$PAGE->set_heading(format_string($SITE->fullname) . ": $stremailupdate");
47
d6ace123 48if (empty($preferences['newemailattemptsleft'])) {
49 redirect("$CFG->wwwroot/user/view.php?id=$user->id");
50
a2ed6e69 51} else if ($preferences['newemailattemptsleft'] < 1) {
05c38e2b 52 cancel_email_update($user->id);
d6ace123 53
372d6b92
AN
54 echo $OUTPUT->header();
55 echo $OUTPUT->box(get_string('auth_outofnewemailupdateattempts', 'auth'), 'center');
56 echo $OUTPUT->footer();
a2ed6e69 57} else if ($key == $preferences['newemailkey']) {
12058e63 58 $olduser = clone($user);
05c38e2b 59 cancel_email_update($user->id);
d6ace123 60 $user->email = $preferences['newemail'];
61
bb78e249 62 // Detect duplicate before saving.
1b3a6e77
JP
63 if (empty($CFG->allowaccountssameemail)) {
64 // Make a case-insensitive query for the given email address.
65 $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
66 $params = array(
67 'email' => $user->email,
68 'mnethostid' => $CFG->mnet_localhost_id,
69 'userid' => $user->id
70 );
71 // If there are other user(s) that already have the same email, cancel and redirect.
72 if ($DB->record_exists_select('user', $select, $params)) {
73 redirect(new moodle_url('/user/view.php', ['id' => $user->id]), get_string('emailnowexists', 'auth'));
74 }
d6ace123 75 }
76
1b3a6e77
JP
77 // Update user email.
78 $authplugin = get_auth_plugin($user->auth);
79 $authplugin->user_update($olduser, $user);
80 user_update_user($user, false);
81 $a->email = $user->email;
82 redirect(
83 new moodle_url('/user/view.php', ['id' => $user->id]),
84 get_string('emailupdatesuccess', 'auth', $a),
85 null,
86 \core\output\notification::NOTIFY_SUCCESS
87 );
88
d6ace123 89} else {
90 $preferences['newemailattemptsleft']--;
91 set_user_preference('newemailattemptsleft', $preferences['newemailattemptsleft'], $user->id);
372d6b92
AN
92 echo $OUTPUT->header();
93 echo $OUTPUT->box(get_string('auth_invalidnewemailkey', 'auth'), 'center');
94 echo $OUTPUT->footer();
d6ace123 95}