user.pages.inc

You are here

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

User page callback file for the user module.

Functions

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