user.admin.inc 38.1 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Admin page callback file for the user module.
 */

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/**
 * Page callback: Generates the appropriate user administration form.
 *
 * This function generates the user registration, multiple user cancellation,
 * or filtered user list admin form, depending on the argument and the POST
 * form values.
 *
 * @param string $callback_arg
 *   (optional) Indicates which form to build. Defaults to '', which will
 *   trigger the user filter form. If the POST value 'op' is present, this
 *   function uses that value as the callback argument.
 *
 * @return string
 *   A renderable form array for the respective request.
 */
23 24 25 26 27 28
function user_admin($callback_arg = '') {
  $op = isset($_POST['op']) ? $_POST['op'] : $callback_arg;

  switch ($op) {
    case t('Create new account'):
    case 'create':
29 30
      $account = entity_create('user', array());
      $build['user_register'] = entity_get_form($account, 'register');
31 32
      break;
    default:
33
      if (!empty($_POST['accounts']) && isset($_POST['operation']) && ($_POST['operation'] == 'cancel')) {
34
        $build['user_multiple_cancel_confirm'] = drupal_get_form('user_multiple_cancel_confirm');
35 36
      }
      else {
37 38
        $build['user_filter_form'] = drupal_get_form('user_filter_form');
        $build['user_admin_account'] = drupal_get_form('user_admin_account');
39 40
      }
  }
41
  return $build;
42 43 44 45
}

/**
 * Form builder; Return form for user administration filters.
46
 *
47
 * @ingroup forms
48
 * @see user_filter_form_submit()
49 50
 */
function user_filter_form() {
51
  $session = isset($_SESSION['user_overview_filter']) ? $_SESSION['user_overview_filter'] : array();
52 53 54
  $filters = user_filters();

  $i = 0;
55
  $form['filters'] = array(
56
    '#type' => 'details',
57
    '#title' => t('Show only users where'),
58
    '#theme' => 'exposed_filters__user',
59
  );
60 61
  foreach ($session as $filter) {
    list($type, $value) = $filter;
62 63 64 65 66 67 68 69 70
    if ($type == 'permission') {
      // Merge arrays of module permissions into one.
      // Slice past the first element '[any]' whose value is not an array.
      $options = call_user_func_array('array_merge', array_slice($filters[$type]['options'], 1));
      $value = $options[$value];
    }
    else {
      $value = $filters[$type]['options'][$value];
    }
71
    $t_args = array('%property' => $filters[$type]['title'], '%value' => $value);
72
    if ($i++) {
73
      $form['filters']['current'][] = array('#markup' => t('and where %property is %value', $t_args));
74 75
    }
    else {
76
      $form['filters']['current'][] = array('#markup' => t('%property is %value', $t_args));
77
    }
78 79
  }

80 81 82 83 84 85 86 87 88
  $form['filters']['status'] = array(
    '#type' => 'container',
    '#attributes' => array('class' => array('clearfix')),
    '#prefix' => ($i ? '<div class="additional-filters">' . t('and where') . '</div>' : ''),
  );
  $form['filters']['status']['filters'] = array(
    '#type' => 'container',
    '#attributes' => array('class' => array('filters')),
  );
89
  foreach ($filters as $key => $filter) {
90
    $form['filters']['status']['filters'][$key] = array(
91 92
      '#type' => 'select',
      '#options' => $filter['options'],
93 94
      '#title' => $filter['title'],
      '#default_value' => '[any]',
95
    );
96 97
  }

98
  $form['filters']['status']['actions'] = array(
99 100
    '#type' => 'actions',
    '#attributes' => array('class' => array('container-inline')),
101
  );
102
  $form['filters']['status']['actions']['submit'] = array(
103 104 105
    '#type' => 'submit',
    '#value' => (count($session) ? t('Refine') : t('Filter')),
  );
106
  if (count($session)) {
107
    $form['filters']['status']['actions']['undo'] = array(
108 109 110
      '#type' => 'submit',
      '#value' => t('Undo'),
    );
111
    $form['filters']['status']['actions']['reset'] = array(
112 113 114
      '#type' => 'submit',
      '#value' => t('Reset'),
    );
115 116
  }

117
  drupal_add_library('system', 'drupal.form');
118

119 120 121 122 123 124 125 126 127 128
  return $form;
}

/**
 * Process result from user administration filter form.
 */
function user_filter_form_submit($form, &$form_state) {
  $op = $form_state['values']['op'];
  $filters = user_filters();
  switch ($op) {
129 130 131 132 133
    case t('Filter'):
    case t('Refine'):
      // Apply every filter that has a choice selected other than 'any'.
      foreach ($filters as $filter => $options) {
        if (isset($form_state['values'][$filter]) && $form_state['values'][$filter] != '[any]') {
134
          $_SESSION['user_overview_filter'][] = array($filter, $form_state['values'][$filter]);
135 136 137 138 139 140 141
        }
      }
      break;
    case t('Undo'):
      array_pop($_SESSION['user_overview_filter']);
      break;
    case t('Reset'):
142
      $_SESSION['user_overview_filter'] = array();
143 144 145 146 147
      break;
    case t('Update'):
      return;
  }

148
  $form_state['redirect'] = 'admin/people';
149 150 151 152 153 154 155
  return;
}

/**
 * Form builder; User administration page.
 *
 * @ingroup forms
156 157
 * @see user_admin_account_validate()
 * @see user_admin_account_submit()
158 159 160 161
 */
function user_admin_account() {

  $header = array(
162
    'username' => array('data' => t('Username'), 'field' => 'u.name'),
163 164 165 166
    'status' => array('data' => t('Status'), 'field' => 'u.status', 'class' => array(RESPONSIVE_PRIORITY_LOW)),
    'roles' => array('data' => t('Roles'), 'class' => array(RESPONSIVE_PRIORITY_LOW)),
    'member_for' => array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc', 'class' => array(RESPONSIVE_PRIORITY_LOW)),
    'access' => array('data' => t('Last access'), 'field' => 'u.access', 'class' => array(RESPONSIVE_PRIORITY_LOW)),
167
    'operations' => t('Operations'),
168 169
  );

170
  $query = db_select('users', 'u');
171 172
  $query->condition('u.uid', 0, '<>');
  user_build_filter_query($query);
173

174
  $count_query = clone $query;
175
  $count_query->addExpression('COUNT(u.uid)');
176

177 178
  $query = $query
    ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
179
    ->extend('Drupal\Core\Database\Query\TableSortExtender');
180 181 182
  $query
    ->fields('u', array('uid', 'name', 'status', 'created', 'access'))
    ->limit(50)
183
    ->orderByHeader($header)
184 185
    ->setCountQuery($count_query);
  $result = $query->execute();
186 187

  $form['options'] = array(
188
    '#type' => 'details',
189
    '#title' => t('Update options'),
190
    '#attributes' => array('class' => array('container-inline')),
191 192 193 194 195 196 197
  );
  $options = array();
  foreach (module_invoke_all('user_operations') as $operation => $array) {
    $options[$operation] = $array['label'];
  }
  $form['options']['operation'] = array(
    '#type' => 'select',
198 199
    '#title' => t('Operation'),
    '#title_display' => 'invisible',
200 201 202
    '#options' => $options,
    '#default_value' => 'unblock',
  );
203
  $options = array();
204 205 206 207 208 209 210
  $form['options']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update'),
  );

  $destination = drupal_get_destination();
  $status = array(t('blocked'), t('active'));
211
  $roles = array_map('check_plain', user_role_names(TRUE));
212
  $accounts = array();
213
  foreach ($result as $account) {
214
    $account = user_load($account->uid);
215
    $users_roles = array();
216 217
    $roles_result = db_query('SELECT rid FROM {users_roles} WHERE uid = :uid', array(':uid' => $account->uid));
    foreach ($roles_result as $user_role) {
218
      $users_roles[] = $roles[$user_role->rid];
219
    }
220
    asort($users_roles);
221

222
    $options[$account->uid] = array(
223
      'username' => theme('username', array('account' => $account)),
224
      'status' =>  $status[$account->status],
225
      'roles' => theme('item_list', array('items' => $users_roles)),
226 227
      'member_for' => format_interval(REQUEST_TIME - $account->created),
      'access' =>  $account->access ? t('@time ago', array('@time' => format_interval(REQUEST_TIME - $account->access))) : t('never'),
228 229 230 231
    );
    $links = array();
    $links['edit'] = array(
      'title' => t('edit'),
232
      'href' => 'user/' . $account->uid . '/edit',
233 234
      'query' => $destination,
    );
235 236 237 238 239 240 241
    if (module_invoke('translation_entity', 'translate_access', $account)) {
      $links['translate'] = array(
        'title' => t('translate'),
        'href' => 'user/' . $account->uid . '/translations',
        'query' => $destination,
      );
    }
242 243 244
    $options[$account->uid]['operations']['data'] = array(
      '#type' => 'operations',
      '#links' => $links,
245
    );
246
  }
247

248
  $form['accounts'] = array(
249 250 251 252
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('No people available.'),
253
  );
254
  $form['pager'] = array('#markup' => theme('pager'));
255 256 257 258 259 260 261 262

  return $form;
}

/**
 * Submit the user administration update form.
 */
function user_admin_account_submit($form, &$form_state) {
263
  $operations = module_invoke_all('user_operations', $form, $form_state);
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
  $operation = $operations[$form_state['values']['operation']];
  // Filter out unchecked accounts.
  $accounts = array_filter($form_state['values']['accounts']);
  if ($function = $operation['callback']) {
    // Add in callback arguments if present.
    if (isset($operation['callback arguments'])) {
      $args = array_merge(array($accounts), $operation['callback arguments']);
    }
    else {
      $args = array($accounts);
    }
    call_user_func_array($function, $args);

    drupal_set_message(t('The update has been performed.'));
  }
}

function user_admin_account_validate($form, &$form_state) {
  $form_state['values']['accounts'] = array_filter($form_state['values']['accounts']);
  if (count($form_state['values']['accounts']) == 0) {
    form_set_error('', t('No users selected.'));
  }
}

/**
 * Form builder; Configure user settings for this site.
 *
 * @ingroup forms
292
 * @see user_admin_settings_submit()
293
 */
294 295
function user_admin_settings($form, &$form_state) {
  $config = config('user.settings');
296 297
  $mail_config = config('user.mail');

298 299
  // Settings for anonymous users.
  $form['anonymous_settings'] = array(
300
    '#type' => 'details',
301 302 303 304 305
    '#title' => t('Anonymous users'),
  );
  $form['anonymous_settings']['anonymous'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
306
    '#default_value' => $config->get('anonymous'),
307 308 309
    '#description' => t('The name used to indicate anonymous users.'),
    '#required' => TRUE,
  );
310

311 312
  // Administrative role option.
  $form['admin_role'] = array(
313
    '#type' => 'details',
314
    '#title' => t('Administrator role'),
315 316
  );

317
  // Do not allow users to set the anonymous or authenticated user roles as the
318
  // administrator role.
319
  $roles = user_role_names();
320 321
  unset($roles[DRUPAL_ANONYMOUS_RID]);
  unset($roles[DRUPAL_AUTHENTICATED_RID]);
322 323 324 325 326
  $roles[0] = t('disabled');

  $form['admin_role']['user_admin_role'] = array(
    '#type' => 'select',
    '#title' => t('Administrator role'),
327
    '#default_value' => $config->get('admin_role'),
328 329 330 331
    '#options' => $roles,
    '#description' => t('This role will be automatically assigned new permissions whenever a module is enabled. Changing this setting will not affect existing permissions.'),
  );

332 333 334
  // @todo Remove this check once language settings are generalized.
  if (module_exists('translation_entity')) {
    $form['language'] = array(
335
      '#type' => 'details',
336 337 338 339 340 341 342
      '#title' => t('Language settings'),
      '#tree' => TRUE,
    );
    $form_state['translation_entity']['key'] = 'language';
    $form['language'] += translation_entity_enable_widget('user', 'user', $form, $form_state);
  }

343 344
  // User registration settings.
  $form['registration_cancellation'] = array(
345
    '#type' => 'details',
346 347 348 349 350
    '#title' => t('Registration and cancellation'),
  );
  $form['registration_cancellation']['user_register'] = array(
    '#type' => 'radios',
    '#title' => t('Who can register accounts?'),
351
    '#default_value' => $config->get('register'),
352
    '#options' => array(
353 354 355
      USER_REGISTER_ADMINISTRATORS_ONLY => t('Administrators only'),
      USER_REGISTER_VISITORS => t('Visitors'),
      USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL => t('Visitors, but administrator approval is required'),
356 357 358 359 360
    )
  );
  $form['registration_cancellation']['user_email_verification'] = array(
    '#type' => 'checkbox',
    '#title' => t('Require e-mail verification when a visitor creates an account.'),
361
    '#default_value' => $config->get('verify_mail'),
362
    '#description' => t('New users will be required to validate their e-mail address prior to logging into the site, and will be assigned a system-generated password. With this setting disabled, users will be logged in immediately upon registering, and may select their own passwords during registration.')
363
  );
364 365 366 367 368
  $form['registration_cancellation']['user_password_strength'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable password strength indicator'),
    '#default_value' => $config->get('password_strength'),
  );
369
  form_load_include($form_state, 'inc', 'user', 'user.pages');
370
  $form['registration_cancellation']['user_cancel_method'] = array(
371
    '#type' => 'radios',
372
    '#title' => t('When cancelling a user account'),
373
    '#default_value' => $config->get('cancel_method'),
374
    '#description' => t('Users with the %select-cancel-method or %administer-users <a href="@permissions-url">permissions</a> can override this default method.', array('%select-cancel-method' => t('Select method for cancelling account'), '%administer-users' => t('Administer users'), '@permissions-url' => url('admin/people/permissions'))),
375
  );
376
  $form['registration_cancellation']['user_cancel_method'] += user_cancel_methods();
377 378 379 380 381 382
  foreach (element_children($form['registration_cancellation']['user_cancel_method']) as $key) {
    // All account cancellation methods that specify #access cannot be
    // configured as default method.
    // @see hook_user_cancel_methods_alter()
    if (isset($form['registration_cancellation']['user_cancel_method'][$key]['#access'])) {
      $form['registration_cancellation']['user_cancel_method'][$key]['#access'] = FALSE;
383 384 385
    }
  }

386 387
  // Account settings.
  $form['personalization'] = array(
388
    '#type' => 'details',
389 390 391 392 393
    '#title' => t('Personalization'),
  );
  $form['personalization']['user_signatures'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable signatures.'),
394
    '#default_value' => $config->get('signatures'),
395 396 397 398
  );

  $form['email'] = array(
    '#type' => 'vertical_tabs',
399
    '#title' => t('E-mails'),
400 401 402
  );
  // These email tokens are shared for all settings, so just define
  // the list once to help ensure they stay in sync.
403
  $email_token_help = t('Available variables are: [site:name], [site:url], [user:name], [user:mail], [site:login-url], [site:url-brief], [user:edit-url], [user:one-time-login-url], [user:cancel-url].');
404

405
  $form['email_admin_created'] = array(
406
    '#type' => 'details',
407
    '#title' => t('Welcome (new user created by administrator)'),
408
    '#collapsed' => ($config->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY),
409
    '#description' => t('Edit the welcome e-mail messages sent to new member accounts created by an administrator.') . ' ' . $email_token_help,
410
    '#group' => 'email',
411
  );
412
  $form['email_admin_created']['user_mail_register_admin_created_subject'] = array(
413 414
    '#type' => 'textfield',
    '#title' => t('Subject'),
415
    '#default_value' => $mail_config->get('register_admin_created.subject'),
416 417
    '#maxlength' => 180,
  );
418
  $form['email_admin_created']['user_mail_register_admin_created_body'] = array(
419 420
    '#type' => 'textarea',
    '#title' => t('Body'),
421
    '#default_value' =>  $mail_config->get('register_admin_created.body'),
422 423 424
    '#rows' => 15,
  );

425
  $form['email_pending_approval'] = array(
426
    '#type' => 'details',
427
    '#title' => t('Welcome (awaiting approval)'),
428
    '#collapsed' => ($config->get('register') != USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL),
429
    '#description' => t('Edit the welcome e-mail messages sent to new members upon registering, when administrative approval is required.') . ' ' . $email_token_help,
430
    '#group' => 'email',
431
  );
432
  $form['email_pending_approval']['user_mail_register_pending_approval_subject'] = array(
433 434
    '#type' => 'textfield',
    '#title' => t('Subject'),
435
    '#default_value' => $mail_config->get('register_pending_approval.subject'),
436 437
    '#maxlength' => 180,
  );
438
  $form['email_pending_approval']['user_mail_register_pending_approval_body'] = array(
439 440
    '#type' => 'textarea',
    '#title' => t('Body'),
441
    '#default_value' => $mail_config->get('register_pending_approval.body'),
442
    '#rows' => 8,
443 444
  );

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
  $form['email_pending_approval_admin'] = array(
    '#type' => 'details',
    '#title' => t('Admin (user awaiting approval)'),
    '#collapsed' => ($config->get('register') != USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL),
    '#description' => t('Edit the e-mail notifying the site administrator that there are new members awaiting administrative approval.') . ' ' . $email_token_help,
    '#group' => 'email',
  );
  $form['email_pending_approval_admin']['register_pending_approval_admin_subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#default_value' => $mail_config->get('register_pending_approval_admin.subject'),
    '#maxlength' => 180,
  );
  $form['email_pending_approval_admin']['register_pending_approval_admin_body'] = array(
    '#type' => 'textarea',
    '#title' => t('Body'),
    '#default_value' => $mail_config->get('register_pending_approval_admin.body'),
    '#rows' => 8,
  );

465
  $form['email_no_approval_required'] = array(
466
    '#type' => 'details',
467
    '#title' => t('Welcome (no approval required)'),
468
    '#collapsed' => ($config->get('register') != USER_REGISTER_VISITORS),
469
    '#description' => t('Edit the welcome e-mail messages sent to new members upon registering, when no administrator approval is required.') . ' ' . $email_token_help,
470
    '#group' => 'email',
471
  );
472
  $form['email_no_approval_required']['user_mail_register_no_approval_required_subject'] = array(
473 474
    '#type' => 'textfield',
    '#title' => t('Subject'),
475
    '#default_value' => $mail_config->get('register_no_approval_required.subject'),
476 477
    '#maxlength' => 180,
  );
478
  $form['email_no_approval_required']['user_mail_register_no_approval_required_body'] = array(
479 480
    '#type' => 'textarea',
    '#title' => t('Body'),
481
    '#default_value' => $mail_config->get('register_no_approval_required.body'),
482
    '#rows' => 15,
483
  );
484

485
  $form['email_password_reset'] = array(
486
    '#type' => 'details',
487
    '#title' => t('Password recovery'),
488
    '#collapsed' => TRUE,
489
    '#description' => t('Edit the e-mail messages sent to users who request a new password.') . ' ' . $email_token_help,
490 491
    '#group' => 'email',
    '#weight' => 10,
492
  );
493
  $form['email_password_reset']['user_mail_password_reset_subject'] = array(
494 495
    '#type' => 'textfield',
    '#title' => t('Subject'),
496
    '#default_value' => $mail_config->get('password_reset.subject'),
497 498
    '#maxlength' => 180,
  );
499
  $form['email_password_reset']['user_mail_password_reset_body'] = array(
500 501
    '#type' => 'textarea',
    '#title' => t('Body'),
502
    '#default_value' => $mail_config->get('password_reset.body'),
503 504 505
    '#rows' => 12,
  );

506
  $form['email_activated'] = array(
507
    '#type' => 'details',
508
    '#title' => t('Account activation'),
509
    '#collapsed' => TRUE,
510
    '#description' => t('Enable and edit e-mail messages sent to users upon account activation (when an administrator activates an account of a user who has already registered, on a site where administrative approval is required).') . ' ' . $email_token_help,
511
    '#group' => 'email',
512
  );
513
  $form['email_activated']['user_mail_status_activated_notify'] = array(
514 515
    '#type' => 'checkbox',
    '#title' => t('Notify user when account is activated.'),
516
    '#default_value' => $config->get('notify.status_activated'),
517
  );
518 519 520 521 522 523 524 525 526 527
  $form['email_activated']['settings'] = array(
    '#type' => 'container',
    '#states' => array(
      // Hide the additional settings when this email is disabled.
      'invisible' => array(
        'input[name="user_mail_status_activated_notify"]' => array('checked' => FALSE),
      ),
    ),
  );
  $form['email_activated']['settings']['user_mail_status_activated_subject'] = array(
528 529
    '#type' => 'textfield',
    '#title' => t('Subject'),
530
    '#default_value' => $mail_config->get('status_activated.subject'),
531 532
    '#maxlength' => 180,
  );
533
  $form['email_activated']['settings']['user_mail_status_activated_body'] = array(
534 535
    '#type' => 'textarea',
    '#title' => t('Body'),
536
    '#default_value' => $mail_config->get('status_activated.body'),
537 538 539
    '#rows' => 15,
  );

540
  $form['email_blocked'] = array(
541
    '#type' => 'details',
542
    '#title' => t('Account blocked'),
543
    '#collapsed' => TRUE,
544
    '#description' => t('Enable and edit e-mail messages sent to users when their accounts are blocked.') . ' ' . $email_token_help,
545
    '#group' => 'email',
546
  );
547
  $form['email_blocked']['user_mail_status_blocked_notify'] = array(
548 549
    '#type' => 'checkbox',
    '#title' => t('Notify user when account is blocked.'),
550
    '#default_value' => $config->get('notify.status_blocked'),
551
  );
552 553 554 555 556 557 558 559 560 561
  $form['email_blocked']['settings'] = array(
    '#type' => 'container',
    '#states' => array(
      // Hide the additional settings when the blocked email is disabled.
      'invisible' => array(
        'input[name="user_mail_status_blocked_notify"]' => array('checked' => FALSE),
      ),
    ),
  );
  $form['email_blocked']['settings']['user_mail_status_blocked_subject'] = array(
562 563
    '#type' => 'textfield',
    '#title' => t('Subject'),
564
    '#default_value' => $mail_config->get('status_blocked.subject'),
565 566
    '#maxlength' => 180,
  );
567
  $form['email_blocked']['settings']['user_mail_status_blocked_body'] = array(
568 569
    '#type' => 'textarea',
    '#title' => t('Body'),
570
    '#default_value' => $mail_config->get('status_blocked.body'),
571 572 573
    '#rows' => 3,
  );

574
  $form['email_cancel_confirm'] = array(
575
    '#type' => 'details',
576
    '#title' => t('Account cancellation confirmation'),
577
    '#collapsed' => TRUE,
578
    '#description' => t('Edit the e-mail messages sent to users when they attempt to cancel their accounts.') . ' ' . $email_token_help,
579
    '#group' => 'email',
580
  );
581
  $form['email_cancel_confirm']['user_mail_cancel_confirm_subject'] = array(
582 583
    '#type' => 'textfield',
    '#title' => t('Subject'),
584
    '#default_value' => $mail_config->get('cancel_confirm.subject'),
585 586
    '#maxlength' => 180,
  );
587
  $form['email_cancel_confirm']['user_mail_cancel_confirm_body'] = array(
588 589
    '#type' => 'textarea',
    '#title' => t('Body'),
590
    '#default_value' => $mail_config->get('cancel_confirm.body'),
591 592 593
    '#rows' => 3,
  );

594
  $form['email_canceled'] = array(
595
    '#type' => 'details',
596
    '#title' => t('Account canceled'),
597
    '#collapsed' => TRUE,
598
    '#description' => t('Enable and edit e-mail messages sent to users when their accounts are canceled.') . ' ' . $email_token_help,
599
    '#group' => 'email',
600
  );
601
  $form['email_canceled']['user_mail_status_canceled_notify'] = array(
602
    '#type' => 'checkbox',
603
    '#title' => t('Notify user when account is canceled.'),
604
    '#default_value' => $config->get('notify.status_canceled'),
605
  );
606 607 608
  $form['email_canceled']['settings'] = array(
    '#type' => 'container',
    '#states' => array(
609
      // Hide the settings when the cancel notify checkbox is disabled.
610 611 612 613 614 615
      'invisible' => array(
        'input[name="user_mail_status_canceled_notify"]' => array('checked' => FALSE),
      ),
    ),
  );
  $form['email_canceled']['settings']['user_mail_status_canceled_subject'] = array(
616 617
    '#type' => 'textfield',
    '#title' => t('Subject'),
618
    '#default_value' => $mail_config->get('status_canceled.subject'),
619 620
    '#maxlength' => 180,
  );
621
  $form['email_canceled']['settings']['user_mail_status_canceled_body'] = array(
622 623
    '#type' => 'textarea',
    '#title' => t('Body'),
624
    '#default_value' => $mail_config->get('status_canceled.body'),
625 626 627
    '#rows' => 3,
  );

628 629 630 631 632 633 634 635 636 637 638
  return system_config_form($form, $form_state);
}

/**
 * Form submission handler for user_admin_settings().
 */
function user_admin_settings_submit($form, &$form_state) {
  config('user.settings')
    ->set('anonymous', $form_state['values']['anonymous'])
    ->set('admin_role', $form_state['values']['user_admin_role'])
    ->set('register', $form_state['values']['user_register'])
639
    ->set('password_strength', $form_state['values']['user_password_strength'])
640 641
    ->set('verify_mail', $form_state['values']['user_email_verification'])
    ->set('signatures', $form_state['values']['user_signatures'])
642
    ->set('cancel_method', $form_state['values']['user_cancel_method'])
643 644 645 646
    ->set('notify.status_activated', $form_state['values']['user_mail_status_activated_notify'])
    ->set('notify.status_blocked', $form_state['values']['user_mail_status_blocked_notify'])
    ->set('notify.status_canceled', $form_state['values']['user_mail_status_canceled_notify'])
    ->save();
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
  config('user.mail')
    ->set('cancel_confirm.body', $form_state['values']['user_mail_cancel_confirm_body'])
    ->set('cancel_confirm.subject', $form_state['values']['user_mail_cancel_confirm_subject'])
    ->set('password_reset.body', $form_state['values']['user_mail_password_reset_body'])
    ->set('password_reset.subject', $form_state['values']['user_mail_password_reset_subject'])
    ->set('register_admin_created.body', $form_state['values']['user_mail_register_admin_created_body'])
    ->set('register_admin_created.subject', $form_state['values']['user_mail_register_admin_created_subject'])
    ->set('register_no_approval_required.body', $form_state['values']['user_mail_register_no_approval_required_body'])
    ->set('register_no_approval_required.subject', $form_state['values']['user_mail_register_no_approval_required_subject'])
    ->set('register_pending_approval.body', $form_state['values']['user_mail_register_pending_approval_body'])
    ->set('register_pending_approval.subject', $form_state['values']['user_mail_register_pending_approval_subject'])
    ->set('status_activated.body', $form_state['values']['user_mail_status_activated_body'])
    ->set('status_activated.subject', $form_state['values']['user_mail_status_activated_subject'])
    ->set('status_blocked.body', $form_state['values']['user_mail_status_blocked_body'])
    ->set('status_blocked.subject', $form_state['values']['user_mail_status_blocked_subject'])
    ->set('status_canceled.body', $form_state['values']['user_mail_status_canceled_body'])
    ->set('status_canceled.subject', $form_state['values']['user_mail_status_canceled_subject'])
    ->save();
665 666 667 668
}

/**
 * Menu callback: administer permissions.
669
 *
670
 * @ingroup forms
671 672
 * @see user_admin_permissions_submit()
 * @see theme_user_admin_permissions()
673
 */
674
function user_admin_permissions($form, $form_state, $rid = NULL) {
675

676
  // Retrieve role names for columns.
677
  $role_names = user_role_names();
678
  if (isset($rid)) {
679
    $role_names = array($rid => $role_names[$rid]);
680
  }
681 682
  // Fetch permissions for all roles or the one selected role.
  $role_permissions = user_role_permissions($role_names);
683

684 685 686 687 688
  // Store $role_names for use when saving the data.
  $form['role_names'] = array(
    '#type' => 'value',
    '#value' => $role_names,
  );
689 690
  // Render role/permission overview:
  $options = array();
691
  $module_info = system_get_info('module');
692
  $hide_descriptions = system_admin_compact_mode();
693 694 695 696

  // Get a list of all the modules implementing a hook_permission() and sort by
  // display name.
  $modules = array();
697
  foreach (module_implements('permission') as $module) {
698
    $modules[$module] = $module_info[$module]['name'];
699
  }
700
  asort($modules);
701

702
  foreach ($modules as $module => $display_name) {
703
    if ($permissions = module_invoke($module, 'permission')) {
704
      $form['permission'][] = array(
705
        '#markup' => $module_info[$module]['name'],
706
        '#id' => $module,
707
      );
708
      foreach ($permissions as $perm => $perm_item) {
709 710 711 712 713 714
        // Fill in default values for the permission.
        $perm_item += array(
          'description' => '',
          'restrict access' => FALSE,
          'warning' => !empty($perm_item['restrict access']) ? t('Warning: Give to trusted roles only; this permission has security implications.') : '',
        );
715
        $options[$perm] = '';
716 717
        $form['permission'][$perm] = array(
          '#type' => 'item',
718
          '#markup' => $perm_item['title'],
719
          '#description' => theme('user_permission_description', array('permission_item' => $perm_item, 'hide' => $hide_descriptions)),
720
        );
721 722
        foreach ($role_names as $rid => $name) {
          // Builds arrays for checked boxes for each role
723
          if (isset($role_permissions[$rid][$perm])) {
724 725 726 727 728 729 730 731 732
            $status[$rid][] = $perm;
          }
        }
      }
    }
  }

  // Have to build checkboxes here after checkbox arrays are built
  foreach ($role_names as $rid => $name) {
733 734 735 736 737 738
    $form['checkboxes'][$rid] = array(
      '#type' => 'checkboxes',
      '#options' => $options,
      '#default_value' => isset($status[$rid]) ? $status[$rid] : array(),
      '#attributes' => array('class' => array('rid-' . $rid)),
    );
739
    $form['role_names'][$rid] = array('#markup' => check_plain($name), '#tree' => TRUE);
740
  }
741

742
  $form['actions'] = array('#type' => 'actions');
743
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save permissions'));
744

745
  $form['#attached']['library'][] = array('user', 'drupal.user.permissions');
746

747 748 749
  return $form;
}

750 751 752
/**
 * Save permissions selected on the administer permissions page.
 *
753
 * @see user_admin_permissions()
754
 */
755
function user_admin_permissions_submit($form, &$form_state) {
756
  foreach ($form_state['values']['role_names'] as $rid => $name) {
757
    user_role_change_permissions($rid, $form_state['values'][$rid]);
758 759 760 761
  }

  drupal_set_message(t('The changes have been saved.'));

762
  // Clear the cached pages and blocks.
763
  cache_invalidate_tags(array('content' => TRUE));
764 765 766
}

/**
767 768 769 770 771
 * Returns HTML for the administer permissions page.
 *
 * @param $variables
 *   An associative array containing:
 *   - form: A render element representing the form.
772 773 774
 *
 * @ingroup themeable
 */
775 776 777
function theme_user_admin_permissions($variables) {
  $form = $variables['form'];

778
  $roles = user_role_names();
779
  foreach (element_children($form['permission']) as $key) {
780 781 782
    $row = array();
    // Module name
    if (is_numeric($key)) {
783
      $row[] = array('data' => drupal_render($form['permission'][$key]), 'class' => array('module'), 'id' => 'module-' . $form['permission'][$key]['#id'], 'colspan' => count($form['role_names']['#value']) + 1);
784 785 786 787 788
    }
    else {
      // Permission row.
      $row[] = array(
        'data' => drupal_render($form['permission'][$key]),
789
        'class' => array('permission'),
790 791
      );
      foreach (element_children($form['checkboxes']) as $rid) {
792
        $form['checkboxes'][$rid][$key]['#title'] = $roles[$rid] . ': ' . $form['permission'][$key]['#markup'];
793 794
        $form['checkboxes'][$rid][$key]['#title_display'] = 'invisible';
        $row[] = array('data' => drupal_render($form['checkboxes'][$rid][$key]), 'class' => array('checkbox'));
795 796
      }
    }
797
    $rows[] = $row;
798 799 800
  }
  $header[] = (t('Permission'));
  foreach (element_children($form['role_names']) as $rid) {
801
    $header[] = array('data' => drupal_render($form['role_names'][$rid]), 'class' => array('checkbox'));
802
  }
803
  $output = theme('system_compact_link');
804
  $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'permissions')));
805
  $output .= drupal_render_children($form);
806 807 808
  return $output;
}

809
/**
810
 * Returns HTML for an individual permission description.
811 812 813 814 815 816 817 818 819 820 821 822 823 824