1 |
efrain |
1 |
<?php
|
|
|
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
|
|
|
22 |
* @package core_user
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
require_once('../config.php');
|
|
|
26 |
require_once($CFG->libdir.'/adminlib.php');
|
|
|
27 |
require_once($CFG->dirroot.'/user/editlib.php');
|
|
|
28 |
require_once($CFG->dirroot.'/user/lib.php');
|
|
|
29 |
|
|
|
30 |
$key = required_param('key', PARAM_ALPHANUM);
|
|
|
31 |
$id = required_param('id', PARAM_INT);
|
|
|
32 |
|
|
|
33 |
$PAGE->set_url('/user/emailupdate.php', array('id' => $id, 'key' => $key));
|
|
|
34 |
$PAGE->set_context(context_system::instance());
|
|
|
35 |
|
|
|
36 |
if (!$user = $DB->get_record('user', array('id' => $id))) {
|
|
|
37 |
throw new \moodle_exception('invaliduserid');
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
$preferences = get_user_preferences(null, null, $user->id);
|
|
|
41 |
$a = new stdClass();
|
|
|
42 |
$a->fullname = fullname($user, true);
|
|
|
43 |
$stremailupdate = get_string('emailupdate', 'auth', $a);
|
|
|
44 |
|
|
|
45 |
$PAGE->set_title($stremailupdate);
|
|
|
46 |
$PAGE->set_heading(format_string($SITE->fullname) . ": $stremailupdate");
|
1441 |
ariadna |
47 |
// Validate the key.
|
|
|
48 |
$errormessage = get_string('auth_invalidnewemailkey', 'auth');
|
|
|
49 |
try {
|
|
|
50 |
$userkey = validate_user_key($key, 'core_user/email_change', null);
|
|
|
51 |
} catch (moodle_exception $e) {
|
|
|
52 |
$userkey = null;
|
|
|
53 |
$errormessage = $e->getMessage();
|
|
|
54 |
}
|
1 |
efrain |
55 |
|
|
|
56 |
if (empty($preferences['newemailattemptsleft'])) {
|
|
|
57 |
redirect("$CFG->wwwroot/user/view.php?id=$user->id");
|
|
|
58 |
|
|
|
59 |
} else if ($preferences['newemailattemptsleft'] < 1) {
|
|
|
60 |
cancel_email_update($user->id);
|
|
|
61 |
|
|
|
62 |
echo $OUTPUT->header();
|
|
|
63 |
echo $OUTPUT->box(get_string('auth_outofnewemailupdateattempts', 'auth'), 'center');
|
|
|
64 |
echo $OUTPUT->footer();
|
1441 |
ariadna |
65 |
} else if ($userkey && $userkey->userid == $user->id) {
|
|
|
66 |
// Key validated, continue with email update.
|
1 |
efrain |
67 |
$olduser = clone($user);
|
|
|
68 |
cancel_email_update($user->id);
|
|
|
69 |
$user->email = $preferences['newemail'];
|
|
|
70 |
|
|
|
71 |
// Detect duplicate before saving.
|
|
|
72 |
if (empty($CFG->allowaccountssameemail)) {
|
|
|
73 |
// Make a case-insensitive query for the given email address.
|
|
|
74 |
$select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
|
|
|
75 |
$params = array(
|
|
|
76 |
'email' => $user->email,
|
|
|
77 |
'mnethostid' => $CFG->mnet_localhost_id,
|
|
|
78 |
'userid' => $user->id
|
|
|
79 |
);
|
|
|
80 |
// If there are other user(s) that already have the same email, cancel and redirect.
|
|
|
81 |
if ($DB->record_exists_select('user', $select, $params)) {
|
|
|
82 |
redirect(new moodle_url('/user/view.php', ['id' => $user->id]), get_string('emailnowexists', 'auth'));
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
// Update user email.
|
|
|
87 |
$authplugin = get_auth_plugin($user->auth);
|
|
|
88 |
$authplugin->user_update($olduser, $user);
|
|
|
89 |
user_update_user($user, false);
|
|
|
90 |
$a->email = $user->email;
|
|
|
91 |
redirect(
|
|
|
92 |
new moodle_url('/user/view.php', ['id' => $user->id]),
|
|
|
93 |
get_string('emailupdatesuccess', 'auth', $a),
|
|
|
94 |
null,
|
|
|
95 |
\core\output\notification::NOTIFY_SUCCESS
|
|
|
96 |
);
|
|
|
97 |
|
|
|
98 |
} else {
|
|
|
99 |
$preferences['newemailattemptsleft']--;
|
|
|
100 |
set_user_preference('newemailattemptsleft', $preferences['newemailattemptsleft'], $user->id);
|
|
|
101 |
echo $OUTPUT->header();
|
1441 |
ariadna |
102 |
echo $OUTPUT->box($errormessage, 'center');
|
1 |
efrain |
103 |
echo $OUTPUT->footer();
|
|
|
104 |
}
|