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