user.admin.inc 36.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

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

function user_admin($callback_arg = '') {
  $op = isset($_POST['op']) ? $_POST['op'] : $callback_arg;

  switch ($op) {
    case t('Create new account'):
    case 'create':
14 15
      $account = entity_create('user', array());
      $build['user_register'] = entity_get_form($account, 'register');
16 17
      break;
    default:
18
      if (!empty($_POST['accounts']) && isset($_POST['operation']) && ($_POST['operation'] == 'cancel')) {
19
        $build['user_multiple_cancel_confirm'] = drupal_get_form('user_multiple_cancel_confirm');
20 21
      }
      else {
22 23
        $build['user_filter_form'] = drupal_get_form('user_filter_form');
        $build['user_admin_account'] = drupal_get_form('user_admin_account');
24 25
      }
  }
26
  return $build;
27 28 29 30
}

/**
 * Form builder; Return form for user administration filters.
31
 *
32
 * @ingroup forms
33
 * @see user_filter_form_submit()
34 35
 */
function user_filter_form() {
36
  $session = isset($_SESSION['user_overview_filter']) ? $_SESSION['user_overview_filter'] : array();
37 38 39
  $filters = user_filters();

  $i = 0;
40
  $form['filters'] = array(
41
    '#type' => 'details',
42
    '#title' => t('Show only users where'),
43
    '#theme' => 'exposed_filters__user',
44
  );
45 46
  foreach ($session as $filter) {
    list($type, $value) = $filter;
47 48 49 50 51 52 53 54 55
    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];
    }
56
    $t_args = array('%property' => $filters[$type]['title'], '%value' => $value);
57
    if ($i++) {
58
      $form['filters']['current'][] = array('#markup' => t('and where %property is %value', $t_args));
59 60
    }
    else {
61
      $form['filters']['current'][] = array('#markup' => t('%property is %value', $t_args));
62
    }
63 64
  }

65 66 67 68 69 70 71 72 73
  $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')),
  );
74
  foreach ($filters as $key => $filter) {
75
    $form['filters']['status']['filters'][$key] = array(
76 77
      '#type' => 'select',
      '#options' => $filter['options'],
78 79
      '#title' => $filter['title'],
      '#default_value' => '[any]',
80
    );
81 82
  }

83
  $form['filters']['status']['actions'] = array(
84 85
    '#type' => 'actions',
    '#attributes' => array('class' => array('container-inline')),
86
  );
87
  $form['filters']['status']['actions']['submit'] = array(
88 89 90
    '#type' => 'submit',
    '#value' => (count($session) ? t('Refine') : t('Filter')),
  );
91
  if (count($session)) {
92
    $form['filters']['status']['actions']['undo'] = array(
93 94 95
      '#type' => 'submit',
      '#value' => t('Undo'),
    );
96
    $form['filters']['status']['actions']['reset'] = array(
97 98 99
      '#type' => 'submit',
      '#value' => t('Reset'),
    );
100 101
  }

102
  drupal_add_library('system', 'drupal.form');
103

104 105 106 107 108 109 110 111 112 113
  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) {
114 115 116 117 118
    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]') {
119
          $_SESSION['user_overview_filter'][] = array($filter, $form_state['values'][$filter]);
120 121 122 123 124 125 126
        }
      }
      break;
    case t('Undo'):
      array_pop($_SESSION['user_overview_filter']);
      break;
    case t('Reset'):
127
      $_SESSION['user_overview_filter'] = array();
128 129 130 131 132
      break;
    case t('Update'):
      return;
  }

133
  $form_state['redirect'] = 'admin/people';
134 135 136 137 138 139 140
  return;
}

/**
 * Form builder; User administration page.
 *
 * @ingroup forms
141 142
 * @see user_admin_account_validate()
 * @see user_admin_account_submit()
143 144 145 146
 */
function user_admin_account() {

  $header = array(
147
    'username' => array('data' => t('Username'), 'field' => 'u.name'),
148 149 150 151
    '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)),
152
    'operations' => t('Operations'),
153 154
  );

155
  $query = db_select('users', 'u');
156 157
  $query->condition('u.uid', 0, '<>');
  user_build_filter_query($query);
158

159
  $count_query = clone $query;
160
  $count_query->addExpression('COUNT(u.uid)');
161

162 163
  $query = $query
    ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
164
    ->extend('Drupal\Core\Database\Query\TableSortExtender');
165 166 167
  $query
    ->fields('u', array('uid', 'name', 'status', 'created', 'access'))
    ->limit(50)
168
    ->orderByHeader($header)
169 170
    ->setCountQuery($count_query);
  $result = $query->execute();
171 172

  $form['options'] = array(
173
    '#type' => 'details',
174
    '#title' => t('Update options'),
175
    '#attributes' => array('class' => array('container-inline')),
176 177 178 179 180 181 182
  );
  $options = array();
  foreach (module_invoke_all('user_operations') as $operation => $array) {
    $options[$operation] = $array['label'];
  }
  $form['options']['operation'] = array(
    '#type' => 'select',
183 184
    '#title' => t('Operation'),
    '#title_display' => 'invisible',
185 186 187
    '#options' => $options,
    '#default_value' => 'unblock',
  );
188
  $options = array();
189 190 191 192 193 194 195
  $form['options']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update'),
  );

  $destination = drupal_get_destination();
  $status = array(t('blocked'), t('active'));
196
  $roles = array_map('check_plain', user_roles(TRUE));
197
  $accounts = array();
198
  foreach ($result as $account) {
199
    $account = user_load($account->uid);
200
    $users_roles = array();
201 202
    $roles_result = db_query('SELECT rid FROM {users_roles} WHERE uid = :uid', array(':uid' => $account->uid));
    foreach ($roles_result as $user_role) {
203
      $users_roles[] = $roles[$user_role->rid];
204
    }
205
    asort($users_roles);
206

207
    $options[$account->uid] = array(
208
      'username' => theme('username', array('account' => $account)),
209
      'status' =>  $status[$account->status],
210
      'roles' => theme('item_list', array('items' => $users_roles)),
211 212
      'member_for' => format_interval(REQUEST_TIME - $account->created),
      'access' =>  $account->access ? t('@time ago', array('@time' => format_interval(REQUEST_TIME - $account->access))) : t('never'),
213 214 215 216
    );
    $links = array();
    $links['edit'] = array(
      'title' => t('edit'),
217
      'href' => 'user/' . $account->uid . '/edit',
218 219
      'query' => $destination,
    );
220 221 222 223 224 225 226
    if (module_invoke('translation_entity', 'translate_access', $account)) {
      $links['translate'] = array(
        'title' => t('translate'),
        'href' => 'user/' . $account->uid . '/translations',
        'query' => $destination,
      );
    }
227 228 229
    $options[$account->uid]['operations']['data'] = array(
      '#type' => 'operations',
      '#links' => $links,
230
    );
231
  }
232

233
  $form['accounts'] = array(
234 235 236 237
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('No people available.'),
238
  );
239
  $form['pager'] = array('#markup' => theme('pager'));
240 241 242 243 244 245 246 247

  return $form;
}

/**
 * Submit the user administration update form.
 */
function user_admin_account_submit($form, &$form_state) {
248
  $operations = module_invoke_all('user_operations', $form, $form_state);
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
  $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
277
 * @see user_admin_settings_submit()
278
 */
279 280
function user_admin_settings($form, &$form_state) {
  $config = config('user.settings');
281 282
  $mail_config = config('user.mail');

283 284
  // Settings for anonymous users.
  $form['anonymous_settings'] = array(
285
    '#type' => 'details',
286 287 288 289 290
    '#title' => t('Anonymous users'),
  );
  $form['anonymous_settings']['anonymous'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
291
    '#default_value' => $config->get('anonymous'),
292 293 294
    '#description' => t('The name used to indicate anonymous users.'),
    '#required' => TRUE,
  );
295

296 297
  // Administrative role option.
  $form['admin_role'] = array(
298
    '#type' => 'details',
299
    '#title' => t('Administrator role'),
300 301
  );

302
  // Do not allow users to set the anonymous or authenticated user roles as the
303 304
  // administrator role.
  $roles = user_roles();
305 306
  unset($roles[DRUPAL_ANONYMOUS_RID]);
  unset($roles[DRUPAL_AUTHENTICATED_RID]);
307 308 309 310 311
  $roles[0] = t('disabled');

  $form['admin_role']['user_admin_role'] = array(
    '#type' => 'select',
    '#title' => t('Administrator role'),
312
    '#default_value' => $config->get('admin_role'),
313 314 315 316
    '#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.'),
  );

317 318 319
  // @todo Remove this check once language settings are generalized.
  if (module_exists('translation_entity')) {
    $form['language'] = array(
320
      '#type' => 'details',
321 322 323 324 325 326 327
      '#title' => t('Language settings'),
      '#tree' => TRUE,
    );
    $form_state['translation_entity']['key'] = 'language';
    $form['language'] += translation_entity_enable_widget('user', 'user', $form, $form_state);
  }

328 329
  // User registration settings.
  $form['registration_cancellation'] = array(
330
    '#type' => 'details',
331 332 333 334 335
    '#title' => t('Registration and cancellation'),
  );
  $form['registration_cancellation']['user_register'] = array(
    '#type' => 'radios',
    '#title' => t('Who can register accounts?'),
336
    '#default_value' => $config->get('register'),
337
    '#options' => array(
338 339 340
      USER_REGISTER_ADMINISTRATORS_ONLY => t('Administrators only'),
      USER_REGISTER_VISITORS => t('Visitors'),
      USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL => t('Visitors, but administrator approval is required'),
341 342 343 344 345
    )
  );
  $form['registration_cancellation']['user_email_verification'] = array(
    '#type' => 'checkbox',
    '#title' => t('Require e-mail verification when a visitor creates an account.'),
346
    '#default_value' => $config->get('verify_mail'),
347
    '#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.')
348
  );
349
  form_load_include($form_state, 'inc', 'user', 'user.pages');
350
  $form['registration_cancellation']['user_cancel_method'] = array(
351
    '#type' => 'radios',
352
    '#title' => t('When cancelling a user account'),
353
    '#default_value' => $config->get('cancel_method'),
354
    '#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'))),
355
  );
356
  $form['registration_cancellation']['user_cancel_method'] += user_cancel_methods();
357 358 359 360 361 362
  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;
363 364 365
    }
  }

366 367
  // Account settings.
  $form['personalization'] = array(
368
    '#type' => 'details',
369 370 371 372 373
    '#title' => t('Personalization'),
  );
  $form['personalization']['user_signatures'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable signatures.'),
374
    '#default_value' => $config->get('signatures'),
375 376 377 378 379 380 381 382
  );

  $form['email_title'] = array(
    '#type' => 'item',
    '#title' => t('E-mails'),
  );
  $form['email'] = array(
    '#type' => 'vertical_tabs',
383 384 385
  );
  // These email tokens are shared for all settings, so just define
  // the list once to help ensure they stay in sync.
386
  $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].');
387

388
  $form['email_admin_created'] = array(
389
    '#type' => 'details',
390
    '#title' => t('Welcome (new user created by administrator)'),
391
    '#collapsible' => TRUE,
392
    '#collapsed' => ($config->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY),
393
    '#description' => t('Edit the welcome e-mail messages sent to new member accounts created by an administrator.') . ' ' . $email_token_help,
394
    '#group' => 'email',
395
  );
396
  $form['email_admin_created']['user_mail_register_admin_created_subject'] = array(
397 398
    '#type' => 'textfield',
    '#title' => t('Subject'),
399
    '#default_value' => $mail_config->get('register_admin_created.subject'),
400 401
    '#maxlength' => 180,
  );
402
  $form['email_admin_created']['user_mail_register_admin_created_body'] = array(
403 404
    '#type' => 'textarea',
    '#title' => t('Body'),
405
    '#default_value' =>  $mail_config->get('register_admin_created.body'),
406 407 408
    '#rows' => 15,
  );

409
  $form['email_pending_approval'] = array(
410
    '#type' => 'details',
411
    '#title' => t('Welcome (awaiting approval)'),
412
    '#collapsible' => TRUE,
413
    '#collapsed' => ($config->get('register') != USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL),
414
    '#description' => t('Edit the welcome e-mail messages sent to new members upon registering, when administrative approval is required.') . ' ' . $email_token_help,
415
    '#group' => 'email',
416
  );
417
  $form['email_pending_approval']['user_mail_register_pending_approval_subject'] = array(
418 419
    '#type' => 'textfield',
    '#title' => t('Subject'),
420
    '#default_value' => $mail_config->get('register_pending_approval.subject'),
421 422
    '#maxlength' => 180,
  );
423
  $form['email_pending_approval']['user_mail_register_pending_approval_body'] = array(
424 425
    '#type' => 'textarea',
    '#title' => t('Body'),
426
    '#default_value' => $mail_config->get('register_pending_approval.body'),
427
    '#rows' => 8,
428 429
  );

430
  $form['email_no_approval_required'] = array(
431
    '#type' => 'details',
432
    '#title' => t('Welcome (no approval required)'),
433
    '#collapsible' => TRUE,
434
    '#collapsed' => ($config->get('register') != USER_REGISTER_VISITORS),
435
    '#description' => t('Edit the welcome e-mail messages sent to new members upon registering, when no administrator approval is required.') . ' ' . $email_token_help,
436
    '#group' => 'email',
437
  );
438
  $form['email_no_approval_required']['user_mail_register_no_approval_required_subject'] = array(
439 440
    '#type' => 'textfield',
    '#title' => t('Subject'),
441
    '#default_value' => $mail_config->get('register_no_approval_required.subject'),
442 443
    '#maxlength' => 180,
  );
444
  $form['email_no_approval_required']['user_mail_register_no_approval_required_body'] = array(
445 446
    '#type' => 'textarea',
    '#title' => t('Body'),
447
    '#default_value' => $mail_config->get('register_no_approval_required.body'),
448
    '#rows' => 15,
449
  );
450

451
  $form['email_password_reset'] = array(
452
    '#type' => 'details',
453
    '#title' => t('Password recovery'),
454 455
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
456
    '#description' => t('Edit the e-mail messages sent to users who request a new password.') . ' ' . $email_token_help,
457 458
    '#group' => 'email',
    '#weight' => 10,
459
  );
460
  $form['email_password_reset']['user_mail_password_reset_subject'] = array(
461 462
    '#type' => 'textfield',
    '#title' => t('Subject'),
463
    '#default_value' => $mail_config->get('password_reset.subject'),
464 465
    '#maxlength' => 180,
  );
466
  $form['email_password_reset']['user_mail_password_reset_body'] = array(
467 468
    '#type' => 'textarea',
    '#title' => t('Body'),
469
    '#default_value' => $mail_config->get('password_reset.body'),
470 471 472
    '#rows' => 12,
  );

473
  $form['email_activated'] = array(
474
    '#type' => 'details',
475
    '#title' => t('Account activation'),
476 477
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
478
    '#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,
479
    '#group' => 'email',
480
  );
481
  $form['email_activated']['user_mail_status_activated_notify'] = array(
482 483
    '#type' => 'checkbox',
    '#title' => t('Notify user when account is activated.'),
484
    '#default_value' => $config->get('notify.status_activated'),
485
  );
486 487 488 489 490 491 492 493 494 495
  $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(
496 497
    '#type' => 'textfield',
    '#title' => t('Subject'),
498
    '#default_value' => $mail_config->get('status_activated.subject'),
499 500
    '#maxlength' => 180,
  );
501
  $form['email_activated']['settings']['user_mail_status_activated_body'] = array(
502 503
    '#type' => 'textarea',
    '#title' => t('Body'),
504
    '#default_value' => $mail_config->get('status_activated.body'),
505 506 507
    '#rows' => 15,
  );

508
  $form['email_blocked'] = array(
509
    '#type' => 'details',
510
    '#title' => t('Account blocked'),
511 512
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
513
    '#description' => t('Enable and edit e-mail messages sent to users when their accounts are blocked.') . ' ' . $email_token_help,
514
    '#group' => 'email',
515
  );
516
  $form['email_blocked']['user_mail_status_blocked_notify'] = array(
517 518
    '#type' => 'checkbox',
    '#title' => t('Notify user when account is blocked.'),
519
    '#default_value' => $config->get('notify.status_blocked'),
520
  );
521 522 523 524 525 526 527 528 529 530
  $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(
531 532
    '#type' => 'textfield',
    '#title' => t('Subject'),
533
    '#default_value' => $mail_config->get('status_blocked.subject'),
534 535
    '#maxlength' => 180,
  );
536
  $form['email_blocked']['settings']['user_mail_status_blocked_body'] = array(
537 538
    '#type' => 'textarea',
    '#title' => t('Body'),
539
    '#default_value' => $mail_config->get('status_blocked.body'),
540 541 542
    '#rows' => 3,
  );

543
  $form['email_cancel_confirm'] = array(
544
    '#type' => 'details',
545
    '#title' => t('Account cancellation confirmation'),
546 547
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
548
    '#description' => t('Edit the e-mail messages sent to users when they attempt to cancel their accounts.') . ' ' . $email_token_help,
549
    '#group' => 'email',
550
  );
551
  $form['email_cancel_confirm']['user_mail_cancel_confirm_subject'] = array(
552 553
    '#type' => 'textfield',
    '#title' => t('Subject'),
554
    '#default_value' => $mail_config->get('cancel_confirm.subject'),
555 556
    '#maxlength' => 180,
  );
557
  $form['email_cancel_confirm']['user_mail_cancel_confirm_body'] = array(
558 559
    '#type' => 'textarea',
    '#title' => t('Body'),
560
    '#default_value' => $mail_config->get('cancel_confirm.body'),
561 562 563
    '#rows' => 3,
  );

564
  $form['email_canceled'] = array(
565
    '#type' => 'details',
566
    '#title' => t('Account canceled'),
567 568
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
569
    '#description' => t('Enable and edit e-mail messages sent to users when their accounts are canceled.') . ' ' . $email_token_help,
570
    '#group' => 'email',
571
  );
572
  $form['email_canceled']['user_mail_status_canceled_notify'] = array(
573
    '#type' => 'checkbox',
574
    '#title' => t('Notify user when account is canceled.'),
575
    '#default_value' => $config->get('notify.status_canceled'),
576
  );
577 578 579
  $form['email_canceled']['settings'] = array(
    '#type' => 'container',
    '#states' => array(
580
      // Hide the settings when the cancel notify checkbox is disabled.
581 582 583 584 585 586
      'invisible' => array(
        'input[name="user_mail_status_canceled_notify"]' => array('checked' => FALSE),
      ),
    ),
  );
  $form['email_canceled']['settings']['user_mail_status_canceled_subject'] = array(
587 588
    '#type' => 'textfield',
    '#title' => t('Subject'),
589
    '#default_value' => $mail_config->get('status_canceled.subject'),
590 591
    '#maxlength' => 180,
  );
592
  $form['email_canceled']['settings']['user_mail_status_canceled_body'] = array(
593 594
    '#type' => 'textarea',
    '#title' => t('Body'),
595
    '#default_value' => $mail_config->get('status_canceled.body'),
596 597 598
    '#rows' => 3,
  );

599 600 601 602 603 604 605 606 607 608 609 610 611
  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'])
    ->set('verify_mail', $form_state['values']['user_email_verification'])
    ->set('signatures', $form_state['values']['user_signatures'])
612
    ->set('cancel_method', $form_state['values']['user_cancel_method'])
613 614 615 616
    ->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();
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
  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();
635 636 637 638
}

/**
 * Menu callback: administer permissions.
639
 *
640
 * @ingroup forms
641 642
 * @see user_admin_permissions_submit()
 * @see theme_user_admin_permissions()
643
 */
644
function user_admin_permissions($form, $form_state, $rid = NULL) {
645

646 647
  // Retrieve role names for columns.
  $role_names = user_roles();
648
  if (isset($rid)) {
649
    $role_names = array($rid => $role_names[$rid]);
650
  }
651 652
  // Fetch permissions for all roles or the one selected role.
  $role_permissions = user_role_permissions($role_names);
653

654 655 656 657 658
  // Store $role_names for use when saving the data.
  $form['role_names'] = array(
    '#type' => 'value',
    '#value' => $role_names,
  );
659 660
  // Render role/permission overview:
  $options = array();
661
  $module_info = system_get_info('module');
662
  $hide_descriptions = system_admin_compact_mode();
663 664 665 666

  // Get a list of all the modules implementing a hook_permission() and sort by
  // display name.
  $modules = array();
667
  foreach (module_implements('permission') as $module) {
668
    $modules[$module] = $module_info[$module]['name'];
669
  }
670
  asort($modules);
671

672
  foreach ($modules as $module => $display_name) {
673
    if ($permissions = module_invoke($module, 'permission')) {
674
      $form['permission'][] = array(
675
        '#markup' => $module_info[$module]['name'],
676
        '#id' => $module,
677
      );
678
      foreach ($permissions as $perm => $perm_item) {
679 680 681 682 683 684
        // 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.') : '',
        );
685
        $options[$perm] = '';
686 687
        $form['permission'][$perm] = array(
          '#type' => 'item',
688
          '#markup' => $perm_item['title'],
689
          '#description' => theme('user_permission_description', array('permission_item' => $perm_item, 'hide' => $hide_descriptions)),
690
        );
691 692
        foreach ($role_names as $rid => $name) {
          // Builds arrays for checked boxes for each role
693
          if (isset($role_permissions[$rid][$perm])) {
694 695 696 697 698 699 700 701 702
            $status[$rid][] = $perm;
          }
        }
      }
    }
  }

  // Have to build checkboxes here after checkbox arrays are built
  foreach ($role_names as $rid => $name) {
703 704 705 706 707 708
    $form['checkboxes'][$rid] = array(
      '#type' => 'checkboxes',
      '#options' => $options,
      '#default_value' => isset($status[$rid]) ? $status[$rid] : array(),
      '#attributes' => array('class' => array('rid-' . $rid)),
    );
709
    $form['role_names'][$rid] = array('#markup' => check_plain($name), '#tree' => TRUE);
710
  }
711

712
  $form['actions'] = array('#type' => 'actions');
713
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save permissions'));
714

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

717 718 719
  return $form;
}

720 721 722
/**
 * Save permissions selected on the administer permissions page.
 *
723
 * @see user_admin_permissions()
724
 */
725
function user_admin_permissions_submit($form, &$form_state) {
726
  foreach ($form_state['values']['role_names'] as $rid => $name) {
727
    user_role_change_permissions($rid, $form_state['values'][$rid]);
728 729 730 731
  }

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

732
  // Clear the cached pages and blocks.
733
  cache_invalidate_tags(array('content' => TRUE));
734 735 736
}

/**
737 738 739 740 741
 * Returns HTML for the administer permissions page.
 *
 * @param $variables
 *   An associative array containing:
 *   - form: A render element representing the form.
742 743 744
 *
 * @ingroup themeable
 */
745 746 747
function theme_user_admin_permissions($variables) {
  $form = $variables['form'];

748
  $roles = user_roles();
749
  foreach (element_children($form['permission']) as $key) {
750 751 752
    $row = array();
    // Module name
    if (is_numeric($key)) {
753
      $row[] = array('data' => drupal_render($form['permission'][$key]), 'class' => array('module'), 'id' => 'module-' . $form['permission'][$key]['#id'], 'colspan' => count($form['role_names']['#value']) + 1);
754 755 756 757 758
    }
    else {
      // Permission row.
      $row[] = array(
        'data' => drupal_render($form['permission'][$key]),
759
        'class' => array('permission'),
760 761
      );
      foreach (element_children($form['checkboxes']) as $rid) {
762
        $form['checkboxes'][$rid][$key]['#title'] = $roles[$rid] . ': ' . $form['permission'][$key]['#markup'];
763 764
        $form['checkboxes'][$rid][$key]['#title_display'] = 'invisible';
        $row[] = array('data' => drupal_render($form['checkboxes'][$rid][$key]), 'class' => array('checkbox'));
765 766
      }
    }
767
    $rows[] = $row;
768 769 770
  }
  $header[] = (t('Permission'));
  foreach (element_children($form['role_names']) as $rid) {
771
    $header[] = array('data' => drupal_render($form['role_names'][$rid]), 'class' => array('checkbox'));
772
  }
773
  $output = theme('system_compact_link');
774
  $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'permissions')));
775
  $output .= drupal_render_children($form);
776 777 778
  return $output;
}

779
/**
780
 * Returns HTML for an individual permission description.
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
 *
 * @param $variables
 *   An associative array containing:
 *   - permission_item: An associative array representing the permission whose
 *     description is being themed. Useful keys include:
 *     - description: The text of the permission description.
 *     - warning: A security-related warning message about the permission (if
 *       there is one).
 *   - hide: A boolean indicating whether or not the permission description was
 *     requested to be hidden rather than shown.
 *
 * @ingroup themeable
 */
function theme_user_permission_description($variables) {
  if (!$variables['hide']) {
    $description = array();
    $permission_item = $variables['permission_item'];
    if (!empty($permission_item['description'])) {
      $description[] = $permission_item['description'];
    }
    if (!empty($permission_item['warning'])) {
      $description[] = '<em class="permission-warning">' . $permission_item['warning'] . '</em>';
    }
    if (!empty($description)) {
      return implode(' ', $description);
    }
  }
}

810
/**
811
 * Form to re-order roles or add a new one.
812
 *
813
 * @ingroup forms
814
 * @see theme_user_admin_roles()
815
 */
816
function user_admin_roles($form, $form_state) {