PageRenderTime 79ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/user/editadvanced_form.php

https://github.com/dongsheng/moodle
PHP | 327 lines | 215 code | 49 blank | 63 comment | 63 complexity | 76a78bb7c0faa45516a0f46392005b37 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, GPL-3.0, Apache-2.0, LGPL-2.1
  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. * Form for editing a users profile
  18. *
  19. * @copyright 1999 Martin Dougiamas http://dougiamas.com
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package core_user
  22. */
  23. if (!defined('MOODLE_INTERNAL')) {
  24. die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
  25. }
  26. require_once($CFG->dirroot.'/lib/formslib.php');
  27. require_once($CFG->dirroot.'/user/lib.php');
  28. /**
  29. * Class user_editadvanced_form.
  30. *
  31. * @copyright 1999 Martin Dougiamas http://dougiamas.com
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. */
  34. class user_editadvanced_form extends moodleform {
  35. /**
  36. * Define the form.
  37. */
  38. public function definition() {
  39. global $USER, $CFG, $COURSE;
  40. $mform = $this->_form;
  41. $editoroptions = null;
  42. $filemanageroptions = null;
  43. if (!is_array($this->_customdata)) {
  44. throw new coding_exception('invalid custom data for user_edit_form');
  45. }
  46. $editoroptions = $this->_customdata['editoroptions'];
  47. $filemanageroptions = $this->_customdata['filemanageroptions'];
  48. $user = $this->_customdata['user'];
  49. $userid = $user->id;
  50. // Accessibility: "Required" is bad legend text.
  51. $strgeneral = get_string('general');
  52. $strrequired = get_string('required');
  53. // Add some extra hidden fields.
  54. $mform->addElement('hidden', 'id');
  55. $mform->setType('id', core_user::get_property_type('id'));
  56. $mform->addElement('hidden', 'course', $COURSE->id);
  57. $mform->setType('course', PARAM_INT);
  58. // Print the required moodle fields first.
  59. $mform->addElement('header', 'moodle', $strgeneral);
  60. $auths = core_component::get_plugin_list('auth');
  61. $enabled = get_string('pluginenabled', 'core_plugin');
  62. $disabled = get_string('plugindisabled', 'core_plugin');
  63. $authoptions = array($enabled => array(), $disabled => array());
  64. $cannotchangepass = array();
  65. $cannotchangeusername = array();
  66. foreach ($auths as $auth => $unused) {
  67. $authinst = get_auth_plugin($auth);
  68. if (!$authinst->is_internal()) {
  69. $cannotchangeusername[] = $auth;
  70. }
  71. $passwordurl = $authinst->change_password_url();
  72. if (!($authinst->can_change_password() && empty($passwordurl))) {
  73. if ($userid < 1 and $authinst->is_internal()) {
  74. // This is unlikely but we can not create account without password
  75. // when plugin uses passwords, we need to set it initially at least.
  76. } else {
  77. $cannotchangepass[] = $auth;
  78. }
  79. }
  80. if (is_enabled_auth($auth)) {
  81. $authoptions[$enabled][$auth] = get_string('pluginname', "auth_{$auth}");
  82. } else {
  83. $authoptions[$disabled][$auth] = get_string('pluginname', "auth_{$auth}");
  84. }
  85. }
  86. $purpose = user_edit_map_field_purpose($userid, 'username');
  87. $mform->addElement('text', 'username', get_string('username'), 'size="20"' . $purpose);
  88. $mform->addHelpButton('username', 'username', 'auth');
  89. $mform->setType('username', PARAM_RAW);
  90. if ($userid !== -1) {
  91. $mform->disabledIf('username', 'auth', 'in', $cannotchangeusername);
  92. }
  93. $mform->addElement('selectgroups', 'auth', get_string('chooseauthmethod', 'auth'), $authoptions);
  94. $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
  95. $mform->addElement('advcheckbox', 'suspended', get_string('suspended', 'auth'));
  96. $mform->addHelpButton('suspended', 'suspended', 'auth');
  97. $mform->addElement('checkbox', 'createpassword', get_string('createpassword', 'auth'));
  98. $mform->disabledIf('createpassword', 'auth', 'in', $cannotchangepass);
  99. if (!empty($CFG->passwordpolicy)) {
  100. $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
  101. }
  102. $purpose = user_edit_map_field_purpose($userid, 'password');
  103. $mform->addElement('passwordunmask', 'newpassword', get_string('newpassword'), 'size="20"' . $purpose);
  104. $mform->addHelpButton('newpassword', 'newpassword');
  105. $mform->setType('newpassword', core_user::get_property_type('password'));
  106. $mform->disabledIf('newpassword', 'createpassword', 'checked');
  107. $mform->disabledIf('newpassword', 'auth', 'in', $cannotchangepass);
  108. // Check if the user has active external tokens.
  109. if ($userid and empty($CFG->passwordchangetokendeletion)) {
  110. if ($tokens = webservice::get_active_tokens($userid)) {
  111. $services = '';
  112. foreach ($tokens as $token) {
  113. $services .= format_string($token->servicename) . ',';
  114. }
  115. $services = get_string('userservices', 'webservice', rtrim($services, ','));
  116. $mform->addElement('advcheckbox', 'signoutofotherservices', get_string('signoutofotherservices'), $services);
  117. $mform->addHelpButton('signoutofotherservices', 'signoutofotherservices');
  118. $mform->disabledIf('signoutofotherservices', 'newpassword', 'eq', '');
  119. $mform->setDefault('signoutofotherservices', 1);
  120. }
  121. }
  122. $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
  123. $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
  124. $mform->disabledIf('preference_auth_forcepasswordchange', 'createpassword', 'checked');
  125. // Shared fields.
  126. useredit_shared_definition($mform, $editoroptions, $filemanageroptions, $user);
  127. // Next the customisable profile fields.
  128. profile_definition($mform, $userid);
  129. if ($userid == -1) {
  130. $btnstring = get_string('createuser');
  131. } else {
  132. $btnstring = get_string('updatemyprofile');
  133. }
  134. $this->add_action_buttons(true, $btnstring);
  135. $this->set_data($user);
  136. }
  137. /**
  138. * Extend the form definition after data has been parsed.
  139. */
  140. public function definition_after_data() {
  141. global $USER, $CFG, $DB, $OUTPUT;
  142. $mform = $this->_form;
  143. // Trim required name fields.
  144. foreach (useredit_get_required_name_fields() as $field) {
  145. $mform->applyFilter($field, 'trim');
  146. }
  147. if ($userid = $mform->getElementValue('id')) {
  148. $user = $DB->get_record('user', array('id' => $userid));
  149. } else {
  150. $user = false;
  151. }
  152. // User can not change own auth method.
  153. if ($userid == $USER->id) {
  154. $mform->hardFreeze('auth');
  155. $mform->hardFreeze('preference_auth_forcepasswordchange');
  156. }
  157. // Admin must choose some password and supply correct email.
  158. if (!empty($USER->newadminuser)) {
  159. $mform->addRule('newpassword', get_string('required'), 'required', null, 'client');
  160. if ($mform->elementExists('suspended')) {
  161. $mform->removeElement('suspended');
  162. }
  163. }
  164. // Require password for new users.
  165. if ($userid > 0) {
  166. if ($mform->elementExists('createpassword')) {
  167. $mform->removeElement('createpassword');
  168. }
  169. }
  170. if ($user and is_mnet_remote_user($user)) {
  171. // Only local accounts can be suspended.
  172. if ($mform->elementExists('suspended')) {
  173. $mform->removeElement('suspended');
  174. }
  175. }
  176. if ($user and ($user->id == $USER->id or is_siteadmin($user))) {
  177. // Prevent self and admin mess ups.
  178. if ($mform->elementExists('suspended')) {
  179. $mform->hardFreeze('suspended');
  180. }
  181. }
  182. // Print picture.
  183. if (empty($USER->newadminuser)) {
  184. if ($user) {
  185. $context = context_user::instance($user->id, MUST_EXIST);
  186. $fs = get_file_storage();
  187. $hasuploadedpicture = ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.png') || $fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f2.jpg'));
  188. if (!empty($user->picture) && $hasuploadedpicture) {
  189. $imagevalue = $OUTPUT->user_picture($user, array('courseid' => SITEID, 'size' => 64));
  190. } else {
  191. $imagevalue = get_string('none');
  192. }
  193. } else {
  194. $imagevalue = get_string('none');
  195. }
  196. $imageelement = $mform->getElement('currentpicture');
  197. $imageelement->setValue($imagevalue);
  198. if ($user && $mform->elementExists('deletepicture') && !$hasuploadedpicture) {
  199. $mform->removeElement('deletepicture');
  200. }
  201. }
  202. // Next the customisable profile fields.
  203. profile_definition_after_data($mform, $userid);
  204. }
  205. /**
  206. * Validate the form data.
  207. * @param array $usernew
  208. * @param array $files
  209. * @return array|bool
  210. */
  211. public function validation($usernew, $files) {
  212. global $CFG, $DB;
  213. $usernew = (object)$usernew;
  214. $usernew->username = trim($usernew->username);
  215. $user = $DB->get_record('user', array('id' => $usernew->id));
  216. $err = array();
  217. if (!$user and !empty($usernew->createpassword)) {
  218. if ($usernew->suspended) {
  219. // Show some error because we can not mail suspended users.
  220. $err['suspended'] = get_string('error');
  221. }
  222. } else {
  223. if (!empty($usernew->newpassword)) {
  224. $errmsg = ''; // Prevent eclipse warning.
  225. if (!check_password_policy($usernew->newpassword, $errmsg, $usernew)) {
  226. $err['newpassword'] = $errmsg;
  227. }
  228. } else if (!$user) {
  229. $auth = get_auth_plugin($usernew->auth);
  230. if ($auth->is_internal()) {
  231. // Internal accounts require password!
  232. $err['newpassword'] = get_string('required');
  233. }
  234. }
  235. }
  236. if (empty($usernew->username)) {
  237. // Might be only whitespace.
  238. $err['username'] = get_string('required');
  239. } else if (!$user or $user->username !== $usernew->username) {
  240. // Check new username does not exist.
  241. if ($DB->record_exists('user', array('username' => $usernew->username, 'mnethostid' => $CFG->mnet_localhost_id))) {
  242. $err['username'] = get_string('usernameexists');
  243. }
  244. // Check allowed characters.
  245. if ($usernew->username !== core_text::strtolower($usernew->username)) {
  246. $err['username'] = get_string('usernamelowercase');
  247. } else {
  248. if ($usernew->username !== core_user::clean_field($usernew->username, 'username')) {
  249. $err['username'] = get_string('invalidusername');
  250. }
  251. }
  252. }
  253. if (!$user or (isset($usernew->email) && $user->email !== $usernew->email)) {
  254. if (!validate_email($usernew->email)) {
  255. $err['email'] = get_string('invalidemail');
  256. } else if (empty($CFG->allowaccountssameemail)) {
  257. // Make a case-insensitive query for the given email address.
  258. $select = $DB->sql_equal('email', ':email', false) . ' AND mnethostid = :mnethostid AND id <> :userid';
  259. $params = array(
  260. 'email' => $usernew->email,
  261. 'mnethostid' => $CFG->mnet_localhost_id,
  262. 'userid' => $usernew->id
  263. );
  264. // If there are other user(s) that already have the same email, show an error.
  265. if ($DB->record_exists_select('user', $select, $params)) {
  266. $err['email'] = get_string('emailexists');
  267. }
  268. }
  269. }
  270. // Next the customisable profile fields.
  271. $err += profile_validation($usernew, $files);
  272. if (count($err) == 0) {
  273. return true;
  274. } else {
  275. return $err;
  276. }
  277. }
  278. }