user.pages.inc

  1. drupal
    1. 6 modules/user/user.pages.inc
    2. 7 modules/user/user.pages.inc
    3. 8 core/modules/user/user.pages.inc

User page callback file for the user module.

Functions & methods

NameDescription
template_preprocess_user_profileProcess variables for user-profile.tpl.php.
template_preprocess_user_profile_categoryProcess variables for user-profile-category.tpl.php.
template_preprocess_user_profile_itemProcess variables for user-profile-item.tpl.php.
user_autocompleteMenu callback; Retrieve a JSON object containing autocomplete suggestions for existing users.
user_cancel_confirmMenu callback; Cancel a user account via e-mail confirmation link.
user_cancel_confirm_formForm builder; confirm form for cancelling user account.
user_cancel_confirm_form_submitSubmit handler for the account cancellation confirm form.
user_cancel_methodsHelper function to return available account cancellation methods.
user_edit_cancel_submitSubmit function for the 'Cancel account' button on the user edit form.
user_logoutMenu callback; logs the current user out, and redirects to the home page.
user_pageAccess callback for path /user.
user_passForm builder; Request a password reset.
user_pass_resetMenu callback; process one time login link and redirects to the user page on success.
user_pass_submit
user_pass_validate
user_profile_formForm builder; edit a user account or one of their profile categories.
user_profile_form_submitSubmit function for the user account and profile editing form.
user_profile_form_validateValidation function for the user account and profile editing form.

File

modules/user/user.pages.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * User page callback file for the user module.
  5. */
  6. /**
  7. * Menu callback; Retrieve a JSON object containing autocomplete suggestions for existing users.
  8. */
  9. function user_autocomplete($string = '') {
  10. $matches = array();
  11. if ($string) {
  12. $result = db_select('users')->fields('users', array('name'))->condition('name', db_like($string) . '%', 'LIKE')->range(0, 10)->execute();
  13. foreach ($result as $user) {
  14. $matches[$user->name] = check_plain($user->name);
  15. }
  16. }
  17. drupal_json_output($matches);
  18. }
  19. /**
  20. * Form builder; Request a password reset.
  21. *
  22. * @ingroup forms
  23. * @see user_pass_validate()
  24. * @see user_pass_submit()
  25. */
  26. function user_pass() {
  27. global $user;
  28. $form['name'] = array(
  29. '#type' => 'textfield',
  30. '#title' => t('Username or e-mail address'),
  31. '#size' => 60,
  32. '#maxlength' => max(USERNAME_MAX_LENGTH, EMAIL_MAX_LENGTH),
  33. '#required' => TRUE,
  34. );
  35. // Allow logged in users to request this also.
  36. if ($user->uid > 0) {
  37. $form['name']['#type'] = 'value';
  38. $form['name']['#value'] = $user->mail;
  39. $form['mail'] = array(
  40. '#prefix' => '<p>',
  41. '#markup' => t('Password reset instructions will be mailed to %email. You must log out to use the password reset link in the e-mail.', array('%email' => $user->mail)),
  42. '#suffix' => '</p>',
  43. );
  44. }
  45. $form['actions'] = array('#type' => 'actions');
  46. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('E-mail new password'));
  47. return $form;
  48. }
  49. function user_pass_validate($form, &$form_state) {
  50. $name = trim($form_state['values']['name']);
  51. // Try to load by email.
  52. $users = user_load_multiple(array(), array('mail' => $name, 'status' => '1'));
  53. $account = reset($users);
  54. if (!$account) {
  55. // No success, try to load by name.
  56. $users = user_load_multiple(array(), array('name' => $name, 'status' => '1'));
  57. $account = reset($users);
  58. }
  59. if (isset($account->uid)) {
  60. form_set_value(array('#parents' => array('account')), $account, $form_state);
  61. }
  62. else {
  63. form_set_error('name', t('Sorry, %name is not recognized as a user name or an e-mail address.', array('%name' => $name)));
  64. }
  65. }
  66. function user_pass_submit($form, &$form_state) {
  67. global $language;
  68. $account = $form_state['values']['account'];
  69. // Mail one time login URL and instructions using current language.
  70. $mail = _user_mail_notify('password_reset', $account, $language);
  71. if (!empty($mail)) {
  72. watchdog('user', 'Password reset instructions mailed to %name at %email.', array('%name' => $account->name, '%email' => $account->mail));
  73. drupal_set_message(t('Further instructions have been sent to your e-mail address.'));
  74. }
  75. $form_state['redirect'] = 'user';
  76. return;
  77. }
  78. /**
  79. * Menu callback; process one time login link and redirects to the user page on success.
  80. */
  81. function user_pass_reset($form, &$form_state, $uid, $timestamp, $hashed_pass, $action = NULL) {
  82. global $user;
  83. // When processing the one-time login link, we have to make sure that a user
  84. // isn't already logged in.
  85. if ($user->uid) {
  86. // The existing user is already logged in.
  87. if ($user->uid == $uid) {
  88. drupal_set_message(t('You are logged in as %user. <a href="!user_edit">Change your password.</a>', array('%user' => $user->name, '!user_edit' => url("user/$user->uid/edit"))));
  89. }
  90. // A different user is already logged in on the computer.
  91. else {
  92. $reset_link_account = user_load($uid);
  93. if (!empty($reset_link_account)) {
  94. drupal_set_message(t('Another user (%other_user) is already logged into the site on this computer, but you tried to use a one-time link for user %resetting_user. Please <a href="!logout">logout</a> and try using the link again.',
  95. array('%other_user' => $user->name, '%resetting_user' => $reset_link_account->name, '!logout' => url('user/logout'))));
  96. } else {
  97. // Invalid one-time link specifies an unknown user.
  98. drupal_set_message(t('The one-time login link you clicked is invalid.'));
  99. }
  100. }
  101. drupal_goto();
  102. }
  103. else {
  104. // Time out, in seconds, until login URL expires. 24 hours = 86400 seconds.
  105. $timeout = 86400;
  106. $current = REQUEST_TIME;
  107. // Some redundant checks for extra security ?
  108. $users = user_load_multiple(array($uid), array('status' => '1'));
  109. if ($timestamp <= $current && $account = reset($users)) {
  110. // No time out for first time login.
  111. if ($account->login && $current - $timestamp > $timeout) {
  112. drupal_set_message(t('You have tried to use a one-time login link that has expired. Please request a new one using the form below.'));
  113. drupal_goto('user/password');
  114. }
  115. elseif ($account->uid && $timestamp >= $account->login && $timestamp <= $current && $hashed_pass == user_pass_rehash($account->pass, $timestamp, $account->login)) {
  116. // First stage is a confirmation form, then login
  117. if ($action == 'login') {
  118. watchdog('user', 'User %name used one-time login link at time %timestamp.', array('%name' => $account->name, '%timestamp' => $timestamp));
  119. // Set the new user.
  120. $user = $account;
  121. // user_login_finalize() also updates the login timestamp of the
  122. // user, which invalidates further use of the one-time login link.
  123. user_login_finalize();
  124. drupal_set_message(t('You have just used your one-time login link. It is no longer necessary to use this link to log in. Please change your password.'));
  125. // Let the user's password be changed without the current password check.
  126. $token = drupal_hash_base64(drupal_random_bytes(55));
  127. $_SESSION['pass_reset_' . $user->uid] = $token;
  128. drupal_goto('user/' . $user->uid . '/edit', array('query' => array('pass-reset-token' => $token)));
  129. }
  130. else {
  131. $form['message'] = array('#markup' => t('<p>This is a one-time login for %user_name and will expire on %expiration_date.</p><p>Click on this button to log in to the site and change your password.</p>', array('%user_name' => $account->name, '%expiration_date' => format_date($timestamp + $timeout))));
  132. $form['help'] = array('#markup' => '<p>' . t('This login can be used only once.') . '</p>');
  133. $form['actions'] = array('#type' => 'actions');
  134. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Log in'));
  135. $form['#action'] = url("user/reset/$uid/$timestamp/$hashed_pass/login");
  136. return $form;
  137. }
  138. }
  139. else {
  140. drupal_set_message(t('You have tried to use a one-time login link that has either been used or is no longer valid. Please request a new one using the form below.'));
  141. drupal_goto('user/password');
  142. }
  143. }
  144. else {
  145. // Deny access, no more clues.
  146. // Everything will be in the watchdog's URL for the administrator to check.
  147. drupal_access_denied();
  148. }
  149. }
  150. }
  151. /**
  152. * Menu callback; logs the current user out, and redirects to the home page.
  153. */
  154. function user_logout() {
  155. global $user;
  156. watchdog('user', 'Session closed for %name.', array('%name' => $user->name));
  157. module_invoke_all('user_logout', $user);
  158. // Destroy the current session, and reset $user to the anonymous user.
  159. session_destroy();
  160. drupal_goto();
  161. }
  162. /**
  163. * Process variables for user-profile.tpl.php.
  164. *
  165. * The $variables array contains the following arguments:
  166. * - $account
  167. *
  168. * @see user-profile.tpl.php
  169. */
  170. function template_preprocess_user_profile(&$variables) {
  171. $account = $variables['elements']['#account'];
  172. // Helpful $user_profile variable for templates.
  173. foreach (element_children($variables['elements']) as $key) {
  174. $variables['user_profile'][$key] = $variables['elements'][$key];
  175. }
  176. // Preprocess fields.
  177. field_attach_preprocess('user', $account, $variables['elements'], $variables);
  178. }
  179. /**
  180. * Process variables for user-profile-item.tpl.php.
  181. *
  182. * The $variables array contains the following arguments:
  183. * - $element
  184. *
  185. * @see user-profile-item.tpl.php
  186. */
  187. function template_preprocess_user_profile_item(&$variables) {
  188. $variables['title'] = $variables['element']['#title'];
  189. $variables['value'] = $variables['element']['#markup'];
  190. $variables['attributes'] = '';
  191. if (isset($variables['element']['#attributes'])) {
  192. $variables['attributes'] = drupal_attributes($variables['element']['#attributes']);
  193. }
  194. }
  195. /**
  196. * Process variables for user-profile-category.tpl.php.
  197. *
  198. * The $variables array contains the following arguments:
  199. * - $element
  200. *
  201. * @see user-profile-category.tpl.php
  202. */
  203. function template_preprocess_user_profile_category(&$variables) {
  204. $variables['title'] = check_plain($variables['element']['#title']);
  205. $variables['profile_items'] = $variables['element']['#children'];
  206. $variables['attributes'] = '';
  207. if (isset($variables['element']['#attributes'])) {
  208. $variables['attributes'] = drupal_attributes($variables['element']['#attributes']);
  209. }
  210. }
  211. /**
  212. * Form builder; edit a user account or one of their profile categories.
  213. *
  214. * @ingroup forms
  215. * @see user_account_form()
  216. * @see user_account_form_validate()
  217. * @see user_profile_form_validate()
  218. * @see user_profile_form_submit()
  219. * @see user_cancel_confirm_form_submit()
  220. */
  221. function user_profile_form($form, &$form_state, $account, $category = 'account') {
  222. global $user;
  223. // During initial form build, add the entity to the form state for use during
  224. // form building and processing. During a rebuild, use what is in the form
  225. // state.
  226. if (!isset($form_state['user'])) {
  227. $form_state['user'] = $account;
  228. }
  229. else {
  230. $account = $form_state['user'];
  231. }
  232. // @todo Legacy support. Modules are encouraged to access the entity using
  233. // $form_state. Remove in Drupal 8.
  234. $form['#user'] = $account;
  235. $form['#user_category'] = $category;
  236. if ($category == 'account') {
  237. user_account_form($form, $form_state);
  238. // Attach field widgets.
  239. field_attach_form('user', $account, $form, $form_state);
  240. }
  241. $form['actions'] = array('#type' => 'actions');
  242. $form['actions']['submit'] = array(
  243. '#type' => 'submit',
  244. '#value' => t('Save'),
  245. );
  246. if ($category == 'account') {
  247. $form['actions']['cancel'] = array(
  248. '#type' => 'submit',
  249. '#value' => t('Cancel account'),
  250. '#submit' => array('user_edit_cancel_submit'),
  251. '#access' => $account->uid > 1 && (($account->uid == $user->uid && user_access('cancel account')) || user_access('administer users')),
  252. );
  253. }
  254. $form['#validate'][] = 'user_profile_form_validate';
  255. // Add the final user profile form submit handler.
  256. $form['#submit'][] = 'user_profile_form_submit';
  257. return $form;
  258. }
  259. /**
  260. * Validation function for the user account and profile editing form.
  261. */
  262. function user_profile_form_validate($form, &$form_state) {
  263. entity_form_field_validate('user', $form, $form_state);
  264. }
  265. /**
  266. * Submit function for the user account and profile editing form.
  267. */
  268. function user_profile_form_submit($form, &$form_state) {
  269. $account = $form_state['user'];
  270. $category = $form['#user_category'];
  271. // Remove unneeded values.
  272. form_state_values_clean($form_state);
  273. // Before updating the account entity, keep an unchanged copy for use with
  274. // user_save() later. This is necessary for modules implementing the user
  275. // hooks to be able to react on changes by comparing the values of $account
  276. // and $edit.
  277. $account_unchanged = clone $account;
  278. entity_form_submit_build_entity('user', $account, $form, $form_state);
  279. // Populate $edit with the properties of $account, which have been edited on
  280. // this form by taking over all values, which appear in the form values too.
  281. $edit = array_intersect_key((array) $account, $form_state['values']);
  282. user_save($account_unchanged, $edit, $category);
  283. $form_state['values']['uid'] = $account->uid;
  284. if ($category == 'account' && !empty($edit['pass'])) {
  285. // Remove the password reset tag since a new password was saved.
  286. unset($_SESSION['pass_reset_'. $account->uid]);
  287. }
  288. // Clear the page cache because pages can contain usernames and/or profile information:
  289. cache_clear_all();
  290. drupal_set_message(t('The changes have been saved.'));
  291. }
  292. /**
  293. * Submit function for the 'Cancel account' button on the user edit form.
  294. */
  295. function user_edit_cancel_submit($form, &$form_state) {
  296. $destination = array();
  297. if (isset($_GET['destination'])) {
  298. $destination = drupal_get_destination();
  299. unset($_GET['destination']);
  300. }
  301. // Note: We redirect from user/uid/edit to user/uid/cancel to make the tabs disappear.
  302. $form_state['redirect'] = array("user/" . $form['#user']->uid . "/cancel", array('query' => $destination));
  303. }
  304. /**
  305. * Form builder; confirm form for cancelling user account.
  306. *
  307. * @ingroup forms
  308. * @see user_edit_cancel_submit()
  309. */
  310. function user_cancel_confirm_form($form, &$form_state, $account) {
  311. global $user;
  312. $form['_account'] = array('#type' => 'value', '#value' => $account);
  313. // Display account cancellation method selection, if allowed.
  314. $default_method = variable_get('user_cancel_method', 'user_cancel_block');
  315. $admin_access = user_access('administer users');
  316. $can_select_method = $admin_access || user_access('select account cancellation method');
  317. $form['user_cancel_method'] = array(
  318. '#type' => 'item',
  319. '#title' => ($account->uid == $user->uid ? t('When cancelling your account') : t('When cancelling the account')),
  320. '#access' => $can_select_method,
  321. );
  322. $form['user_cancel_method'] += user_cancel_methods();
  323. // Allow user administrators to skip the account cancellation confirmation
  324. // mail (by default), as long as they do not attempt to cancel their own
  325. // account.
  326. $override_access = $admin_access && ($account->uid != $user->uid);
  327. $form['user_cancel_confirm'] = array(
  328. '#type' => 'checkbox',
  329. '#title' => t('Require e-mail confirmation to cancel account.'),
  330. '#default_value' => ($override_access ? FALSE : TRUE),
  331. '#access' => $override_access,
  332. '#description' => t('When enabled, the user must confirm the account cancellation via e-mail.'),
  333. );
  334. // Also allow to send account canceled notification mail, if enabled.
  335. $default_notify = variable_get('user_mail_status_canceled_notify', FALSE);
  336. $form['user_cancel_notify'] = array(
  337. '#type' => 'checkbox',
  338. '#title' => t('Notify user when account is canceled.'),
  339. '#default_value' => ($override_access ? FALSE : $default_notify),
  340. '#access' => $override_access && $default_notify,
  341. '#description' => t('When enabled, the user will receive an e-mail notification after the account has been cancelled.'),
  342. );
  343. // Prepare confirmation form page title and description.
  344. if ($account->uid == $user->uid) {
  345. $question = t('Are you sure you want to cancel your account?');
  346. }
  347. else {
  348. $question = t('Are you sure you want to cancel the account %name?', array('%name' => $account->name));
  349. }
  350. $description = '';
  351. if ($can_select_method) {
  352. $description = t('Select the method to cancel the account above.');
  353. foreach (element_children($form['user_cancel_method']) as $element) {
  354. unset($form['user_cancel_method'][$element]['#description']);
  355. }
  356. }
  357. else {
  358. // The radio button #description is used as description for the confirmation
  359. // form.
  360. foreach (element_children($form['user_cancel_method']) as $element) {
  361. if ($form['user_cancel_method'][$element]['#default_value'] == $form['user_cancel_method'][$element]['#return_value']) {
  362. $description = $form['user_cancel_method'][$element]['#description'];
  363. }
  364. unset($form['user_cancel_method'][$element]['#description']);
  365. }
  366. }
  367. // Always provide entity id in the same form key as in the entity edit form.
  368. $form['uid'] = array('#type' => 'value', '#value' => $account->uid);
  369. return confirm_form($form,
  370. $question,
  371. 'user/' . $account->uid,
  372. $description . ' ' . t('This action cannot be undone.'),
  373. t('Cancel account'), t('Cancel'));
  374. }
  375. /**
  376. * Submit handler for the account cancellation confirm form.
  377. *
  378. * @see user_cancel_confirm_form()
  379. * @see user_multiple_cancel_confirm_submit()
  380. */
  381. function user_cancel_confirm_form_submit($form, &$form_state) {
  382. global $user;
  383. $account = $form_state['values']['_account'];
  384. // Cancel account immediately, if the current user has administrative
  385. // privileges, no confirmation mail shall be sent, and the user does not
  386. // attempt to cancel the own account.
  387. if (user_access('administer users') && empty($form_state['values']['user_cancel_confirm']) && $account->uid != $user->uid) {
  388. user_cancel($form_state['values'], $account->uid, $form_state['values']['user_cancel_method']);
  389. $form_state['redirect'] = 'admin/people';
  390. }
  391. else {
  392. // Store cancelling method and whether to notify the user in $account for
  393. // user_cancel_confirm().
  394. $edit = array(
  395. 'user_cancel_method' => $form_state['values']['user_cancel_method'],
  396. 'user_cancel_notify' => $form_state['values']['user_cancel_notify'],
  397. );
  398. $account = user_save($account, $edit);
  399. _user_mail_notify('cancel_confirm', $account);
  400. drupal_set_message(t('A confirmation request to cancel your account has been sent to your e-mail address.'));
  401. watchdog('user', 'Sent account cancellation request to %name %email.', array('%name' => $account->name, '%email' => '<' . $account->mail . '>'), WATCHDOG_NOTICE);
  402. $form_state['redirect'] = "user/$account->uid";
  403. }
  404. }
  405. /**
  406. * Helper function to return available account cancellation methods.
  407. *
  408. * See documentation of hook_user_cancel_methods_alter().
  409. *
  410. * @return
  411. * An array containing all account cancellation methods as form elements.
  412. *
  413. * @see hook_user_cancel_methods_alter()
  414. * @see user_admin_settings()
  415. * @see user_cancel_confirm_form()
  416. * @see user_multiple_cancel_confirm()
  417. */
  418. function user_cancel_methods() {
  419. $methods = array(
  420. 'user_cancel_block' => array(
  421. 'title' => t('Disable the account and keep its content.'),
  422. 'description' => t('Your account will be blocked and you will no longer be able to log in. All of your content will remain attributed to your user name.'),
  423. ),
  424. 'user_cancel_block_unpublish' => array(
  425. 'title' => t('Disable the account and unpublish its content.'),
  426. 'description' => t('Your account will be blocked and you will no longer be able to log in. All of your content will be hidden from everyone but administrators.'),
  427. ),
  428. 'user_cancel_reassign' => array(
  429. 'title' => t('Delete the account and make its content belong to the %anonymous-name user.', array('%anonymous-name' => variable_get('anonymous', t('Anonymous')))),
  430. 'description' => t('Your account will be removed and all account information deleted. All of your content will be assigned to the %anonymous-name user.', array('%anonymous-name' => variable_get('anonymous', t('Anonymous')))),
  431. ),
  432. 'user_cancel_delete' => array(
  433. 'title' => t('Delete the account and its content.'),
  434. 'description' => t('Your account will be removed and all account information deleted. All of your content will also be deleted.'),
  435. 'access' => user_access('administer users'),
  436. ),
  437. );
  438. // Allow modules to customize account cancellation methods.
  439. drupal_alter('user_cancel_methods', $methods);
  440. // Turn all methods into real form elements.
  441. $default_method = variable_get('user_cancel_method', 'user_cancel_block');
  442. foreach ($methods as $name => $method) {
  443. $form[$name] = array(
  444. '#type' => 'radio',
  445. '#title' => $method['title'],
  446. '#description' => (isset($method['description']) ? $method['description'] : NULL),
  447. '#return_value' => $name,
  448. '#default_value' => $default_method,
  449. '#parents' => array('user_cancel_method'),
  450. );
  451. }
  452. return $form;
  453. }
  454. /**
  455. * Menu callback; Cancel a user account via e-mail confirmation link.
  456. *
  457. * @see user_cancel_confirm_form()
  458. * @see user_cancel_url()
  459. */
  460. function user_cancel_confirm($account, $timestamp = 0, $hashed_pass = '') {
  461. // Time out in seconds until cancel URL expires; 24 hours = 86400 seconds.
  462. $timeout = 86400;
  463. $current = REQUEST_TIME;
  464. // Basic validation of arguments.
  465. if (isset($account->data['user_cancel_method']) && !empty($timestamp) && !empty($hashed_pass)) {
  466. // Validate expiration and hashed password/login.
  467. if ($timestamp <= $current && $current - $timestamp < $timeout && $account->uid && $timestamp >= $account->login && $hashed_pass == user_pass_rehash($account->pass, $timestamp, $account->login)) {
  468. $edit = array(
  469. 'user_cancel_notify' => isset($account->data['user_cancel_notify']) ? $account->data['user_cancel_notify'] : variable_get('user_mail_status_canceled_notify', FALSE),
  470. );
  471. user_cancel($edit, $account->uid, $account->data['user_cancel_method']);
  472. // Since user_cancel() is not invoked via Form API, batch processing needs
  473. // to be invoked manually and should redirect to the front page after
  474. // completion.
  475. batch_process('');
  476. }
  477. else {
  478. drupal_set_message(t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'));
  479. drupal_goto("user/$account->uid/cancel");
  480. }
  481. }
  482. drupal_access_denied();
  483. }
  484. /**
  485. * Access callback for path /user.
  486. *
  487. * Displays user profile if user is logged in, or login form for anonymous
  488. * users.
  489. */
  490. function user_page() {
  491. global $user;
  492. if ($user->uid) {
  493. menu_set_active_item('user/' . $user->uid);
  494. return menu_execute_active_handler(NULL, FALSE);
  495. }
  496. else {
  497. return drupal_get_form('user_login');
  498. }
  499. }
Login or register to post comments