MDL-29320 admin: Improve test for multiple accounts with same email
[moodle.git] / user / editadvanced_form.php
CommitLineData
aa6c1ced 1<?php
a2ed6e69
SH
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 * Form for editing a users profile
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 */
ad6226fb 24
bfebaf64 25if (!defined('MOODLE_INTERNAL')) {
a2ed6e69 26 die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
bfebaf64
MD
27}
28
ad6226fb 29require_once($CFG->dirroot.'/lib/formslib.php');
2159983a 30require_once($CFG->dirroot.'/user/lib.php');
ad6226fb 31
a2ed6e69
SH
32/**
33 * Class user_editadvanced_form.
34 *
35 * @copyright 1999 Martin Dougiamas http://dougiamas.com
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
ad6226fb 38class user_editadvanced_form extends moodleform {
39
a2ed6e69
SH
40 /**
41 * Define the form.
42 */
43 public function definition() {
d8734783 44 global $USER, $CFG, $COURSE;
ad6226fb 45
a2ed6e69 46 $mform = $this->_form;
fc3aa0fb
RT
47 $editoroptions = null;
48 $filemanageroptions = null;
fc3aa0fb 49
d6e7a63d
PS
50 if (!is_array($this->_customdata)) {
51 throw new coding_exception('invalid custom data for user_edit_form');
4e782b32 52 }
d6e7a63d
PS
53 $editoroptions = $this->_customdata['editoroptions'];
54 $filemanageroptions = $this->_customdata['filemanageroptions'];
55 $user = $this->_customdata['user'];
56 $userid = $user->id;
8bdc9cac 57
a2ed6e69 58 // Accessibility: "Required" is bad legend text.
c8149f0f 59 $strgeneral = get_string('general');
ad6226fb 60 $strrequired = get_string('required');
61
a2ed6e69 62 // Add some extra hidden fields.
ad6226fb 63 $mform->addElement('hidden', 'id');
ac9768fc 64 $mform->setType('id', core_user::get_property_type('id'));
d8734783 65 $mform->addElement('hidden', 'course', $COURSE->id);
d18e0fe6 66 $mform->setType('course', PARAM_INT);
ad6226fb 67
a2ed6e69 68 // Print the required moodle fields first.
c8149f0f 69 $mform->addElement('header', 'moodle', $strgeneral);
ad6226fb 70
bd3b3bba 71 $auths = core_component::get_plugin_list('auth');
c5772ede
PS
72 $enabled = get_string('pluginenabled', 'core_plugin');
73 $disabled = get_string('plugindisabled', 'core_plugin');
a2ed6e69 74 $authoptions = array($enabled => array(), $disabled => array());
6513cc03 75 $cannotchangepass = array();
8c2b696b 76 $cannotchangeusername = array();
17da2e6f 77 foreach ($auths as $auth => $unused) {
6513cc03 78 $authinst = get_auth_plugin($auth);
8c2b696b
CB
79
80 if (!$authinst->is_internal()) {
81 $cannotchangeusername[] = $auth;
82 }
83
6513cc03
AA
84 $passwordurl = $authinst->change_password_url();
85 if (!($authinst->can_change_password() && empty($passwordurl))) {
d6e7a63d 86 if ($userid < 1 and $authinst->is_internal()) {
0b3bd8cb
PS
87 // This is unlikely but we can not create account without password
88 // when plugin uses passwords, we need to set it initially at least.
89 } else {
90 $cannotchangepass[] = $auth;
91 }
6513cc03 92 }
c5772ede 93 if (is_enabled_auth($auth)) {
a2ed6e69 94 $authoptions[$enabled][$auth] = get_string('pluginname', "auth_{$auth}");
c5772ede 95 } else {
a2ed6e69 96 $authoptions[$disabled][$auth] = get_string('pluginname', "auth_{$auth}");
c5772ede 97 }
ad6226fb 98 }
8c2b696b 99
2159983a
DW
100 $purpose = user_edit_map_field_purpose($userid, 'username');
101 $mform->addElement('text', 'username', get_string('username'), 'size="20"' . $purpose);
8c2b696b 102 $mform->addHelpButton('username', 'username', 'auth');
90d8bf59 103 $mform->setType('username', PARAM_RAW);
6a806811
CB
104
105 if ($userid !== -1) {
106 $mform->disabledIf('username', 'auth', 'in', $cannotchangeusername);
107 }
8c2b696b 108
a2ed6e69 109 $mform->addElement('selectgroups', 'auth', get_string('chooseauthmethod', 'auth'), $authoptions);
f728d49b 110 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
ad6226fb 111
a2ed6e69 112 $mform->addElement('advcheckbox', 'suspended', get_string('suspended', 'auth'));
4ad72c28
PS
113 $mform->addHelpButton('suspended', 'suspended', 'auth');
114
a2ed6e69 115 $mform->addElement('checkbox', 'createpassword', get_string('createpassword', 'auth'));
0b3bd8cb 116 $mform->disabledIf('createpassword', 'auth', 'in', $cannotchangepass);
483b6804 117
a2ed6e69 118 if (!empty($CFG->passwordpolicy)) {
3bf11ca4 119 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
851481dc 120 }
2159983a
DW
121
122 $purpose = user_edit_map_field_purpose($userid, 'password');
123 $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"' . $purpose);
27d1a27c 124 $mform->addHelpButton('newpassword', 'newpassword');
ac9768fc 125 $mform->setType('newpassword', core_user::get_property_type('password'));
483b6804 126 $mform->disabledIf('newpassword', 'createpassword', 'checked');
ad6226fb 127
6513cc03
AA
128 $mform->disabledIf('newpassword', 'auth', 'in', $cannotchangepass);
129
9d382a94
JL
130 // Check if the user has active external tokens.
131 if ($userid and empty($CFG->passwordchangetokendeletion)) {
132 if ($tokens = webservice::get_active_tokens($userid)) {
133 $services = '';
134 foreach ($tokens as $token) {
135 $services .= format_string($token->servicename) . ',';
136 }
137 $services = get_string('userservices', 'webservice', rtrim($services, ','));
138 $mform->addElement('advcheckbox', 'signoutofotherservices', get_string('signoutofotherservices'), $services);
139 $mform->addHelpButton('signoutofotherservices', 'signoutofotherservices');
140 $mform->disabledIf('signoutofotherservices', 'newpassword', 'eq', '');
141 $mform->setDefault('signoutofotherservices', 1);
142 }
143 }
144
fa1d88eb 145 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
27d1a27c 146 $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
483b6804
PS
147 $mform->disabledIf('preference_auth_forcepasswordchange', 'createpassword', 'checked');
148
a2ed6e69 149 // Shared fields.
d6e7a63d 150 useredit_shared_definition($mform, $editoroptions, $filemanageroptions, $user);
ad6226fb 151
a2ed6e69 152 // Next the customisable profile fields.
fc3aa0fb 153 profile_definition($mform, $userid);
bb6d3d34 154
607fdfc3
AA
155 if ($userid == -1) {
156 $btnstring = get_string('createuser');
157 } else {
158 $btnstring = get_string('updatemyprofile');
159 }
160
7ce538be 161 $this->add_action_buttons(true, $btnstring);
d6e7a63d
PS
162
163 $this->set_data($user);
ad6226fb 164 }
165
a2ed6e69
SH
166 /**
167 * Extend the form definition after data has been parsed.
168 */
169 public function definition_after_data() {
f2f085ee 170 global $USER, $CFG, $DB, $OUTPUT;
ad6226fb 171
a2ed6e69 172 $mform = $this->_form;
e9c27d1e
DM
173
174 // Trim required name fields.
175 foreach (useredit_get_required_name_fields() as $field) {
176 $mform->applyFilter($field, 'trim');
177 }
178
9122137f 179 if ($userid = $mform->getElementValue('id')) {
a2ed6e69 180 $user = $DB->get_record('user', array('id' => $userid));
9122137f 181 } else {
182 $user = false;
183 }
ad6226fb 184
a2ed6e69 185 // User can not change own auth method.
e8e0bb2d 186 if ($userid == $USER->id) {
187 $mform->hardFreeze('auth');
188 $mform->hardFreeze('preference_auth_forcepasswordchange');
ad6226fb 189 }
190
a2ed6e69 191 // Admin must choose some password and supply correct email.
ad6226fb 192 if (!empty($USER->newadminuser)) {
193 $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
4ad72c28
PS
194 if ($mform->elementExists('suspended')) {
195 $mform->removeElement('suspended');
196 }
ad6226fb 197 }
198
a2ed6e69 199 // Require password for new users.
483b6804
PS
200 if ($userid > 0) {
201 if ($mform->elementExists('createpassword')) {
202 $mform->removeElement('createpassword');
203 }
e8e0bb2d 204 }
205
4ad72c28 206 if ($user and is_mnet_remote_user($user)) {
a2ed6e69 207 // Only local accounts can be suspended.
4ad72c28
PS
208 if ($mform->elementExists('suspended')) {
209 $mform->removeElement('suspended');
210 }
211 }
212 if ($user and ($user->id == $USER->id or is_siteadmin($user))) {
a2ed6e69 213 // Prevent self and admin mess ups.
4ad72c28
PS
214 if ($mform->elementExists('suspended')) {
215 $mform->hardFreeze('suspended');
216 }
217 }
218
a2ed6e69 219 // Print picture.
689096bc 220 if (empty($USER->newadminuser)) {
4125bdc1 221 if ($user) {
43731030 222 $context = context_user::instance($user->id, MUST_EXIST);
4125bdc1 223 $fs = get_file_storage();
ed9e0cb6 224 $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
4125bdc1 225 if (!empty($user->picture) && $hasuploadedpicture) {
a2ed6e69 226 $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size' => 64));
4125bdc1
SH
227 } else {
228 $imagevalue = get_string('none');
229 }
e8e0bb2d 230 } else {
4125bdc1
SH
231 $imagevalue = get_string('none');
232 }
233 $imageelement = $mform->getElement('currentpicture');
234 $imageelement->setValue($imagevalue);
235
236 if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) {
237 $mform->removeElement('deletepicture');
e8e0bb2d 238 }
239 }
bb6d3d34 240
a2ed6e69 241 // Next the customisable profile fields.
63466095 242 profile_definition_after_data($mform, $userid);
e8e0bb2d 243 }
244
a2ed6e69
SH
245 /**
246 * Validate the form data.
247 * @param array $usernew
248 * @param array $files
249 * @return array|bool
250 */
251 public function validation($usernew, $files) {
5d910388 252 global $CFG, $DB;
e8e0bb2d 253
254 $usernew = (object)$usernew;
f728d49b
PS
255 $usernew->username = trim($usernew->username);
256
a2ed6e69 257 $user = $DB->get_record('user', array('id' => $usernew->id));
04f639a0 258 $err = array();
259
483b6804
PS
260 if (!$user and !empty($usernew->createpassword)) {
261 if ($usernew->suspended) {
262 // Show some error because we can not mail suspended users.
263 $err['suspended'] = get_string('error');
264 }
265 } else {
266 if (!empty($usernew->newpassword)) {
267 $errmsg = ''; // Prevent eclipse warning.
268 if (!check_password_policy($usernew->newpassword, $errmsg)) {
269 $err['newpassword'] = $errmsg;
270 }
271 } else if (!$user) {
0b3bd8cb
PS
272 $auth = get_auth_plugin($usernew->auth);
273 if ($auth->is_internal()) {
274 // Internal accounts require password!
275 $err['newpassword'] = get_string('required');
276 }
83022298 277 }
278 }
279
04f639a0 280 if (empty($usernew->username)) {
a2ed6e69 281 // Might be only whitespace.
04f639a0 282 $err['username'] = get_string('required');
283 } else if (!$user or $user->username !== $usernew->username) {
a2ed6e69
SH
284 // Check new username does not exist.
285 if ($DB->record_exists('user', array('username' => $usernew->username, 'mnethostid' => $CFG->mnet_localhost_id))) {
e8e0bb2d 286 $err['username'] = get_string('usernameexists');
287 }
a2ed6e69 288 // Check allowed characters.
2f1e464a 289 if ($usernew->username !== core_text::strtolower($usernew->username)) {
e8e0bb2d 290 $err['username'] = get_string('usernamelowercase');
6b8ad965 291 } else {
ac9768fc 292 if ($usernew->username !== core_user::clean_field($usernew->username, 'username')) {
f4065447 293 $err['username'] = get_string('invalidusername');
e8e0bb2d 294 }
ad6226fb 295 }
296 }
e8e0bb2d 297
b47454f6 298 if (!$user or (isset($usernew->email) && $user->email !== $usernew->email)) {
e8e0bb2d 299 if (!validate_email($usernew->email)) {
300 $err['email'] = get_string('invalidemail');
95020832 301 } else if (empty($CFG->allowaccountssameemail)
bc983724 302 and $DB->record_exists('user', array('email' => $usernew->email, 'mnethostid' => $CFG->mnet_localhost_id))) {
e8e0bb2d 303 $err['email'] = get_string('emailexists');
304 }
305 }
306
a2ed6e69 307 // Next the customisable profile fields.
c8969c22 308 $err += profile_validation($usernew, $files);
bb6d3d34 309
a2ed6e69 310 if (count($err) == 0) {
e8e0bb2d 311 return true;
312 } else {
313 return $err;
314 }
ad6226fb 315 }
ad6226fb 316}
317
aa6c1ced 318