user.admin.inc 39.8 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 41 42
  $form['filters'] = array(
    '#type' => 'fieldset',
    '#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 173 174

  $form['options'] = array(
    '#type' => 'fieldset',
    '#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 196
  $form['options']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update'),
  );

  $destination = drupal_get_destination();

  $status = array(t('blocked'), t('active'));
197
  $roles = array_map('check_plain', user_roles(TRUE));
198
  $accounts = array();
199
  foreach ($result as $account) {
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 217 218 219 220 221 222
    );
    $links = array();
    $links['edit'] = array(
      'title' => t('edit'),
      'href' => "user/$account->uid/edit",
      'query' => $destination,
    );
    $options[$account->uid]['operations']['data'] = array(
      '#type' => 'operations',
      '#links' => $links,
223
    );
224
  }
225

226
  $form['accounts'] = array(
227 228 229 230
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('No people available.'),
231
  );
232
  $form['pager'] = array('#markup' => theme('pager'));
233 234 235 236 237 238 239 240

  return $form;
}

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

276 277 278 279 280 281 282 283
  // Settings for anonymous users.
  $form['anonymous_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Anonymous users'),
  );
  $form['anonymous_settings']['anonymous'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
284
    '#default_value' => $config->get('anonymous'),
285 286 287
    '#description' => t('The name used to indicate anonymous users.'),
    '#required' => TRUE,
  );
288

289 290 291
  // Administrative role option.
  $form['admin_role'] = array(
    '#type' => 'fieldset',
292
    '#title' => t('Administrator role'),
293 294
  );

295
  // Do not allow users to set the anonymous or authenticated user roles as the
296 297
  // administrator role.
  $roles = user_roles();
298 299
  unset($roles[DRUPAL_ANONYMOUS_RID]);
  unset($roles[DRUPAL_AUTHENTICATED_RID]);
300 301 302 303 304
  $roles[0] = t('disabled');

  $form['admin_role']['user_admin_role'] = array(
    '#type' => 'select',
    '#title' => t('Administrator role'),
305
    '#default_value' => $config->get('admin_role'),
306 307 308 309
    '#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.'),
  );

310 311
  // User registration settings.
  $form['registration_cancellation'] = array(
312
    '#type' => 'fieldset',
313 314 315 316 317
    '#title' => t('Registration and cancellation'),
  );
  $form['registration_cancellation']['user_register'] = array(
    '#type' => 'radios',
    '#title' => t('Who can register accounts?'),
318
    '#default_value' => $config->get('register'),
319
    '#options' => array(
320 321 322
      USER_REGISTER_ADMINISTRATORS_ONLY => t('Administrators only'),
      USER_REGISTER_VISITORS => t('Visitors'),
      USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL => t('Visitors, but administrator approval is required'),
323 324 325 326 327
    )
  );
  $form['registration_cancellation']['user_email_verification'] = array(
    '#type' => 'checkbox',
    '#title' => t('Require e-mail verification when a visitor creates an account.'),
328
    '#default_value' => $config->get('verify_mail'),
329
    '#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.')
330
  );
331 332
  module_load_include('inc', 'user', 'user.pages');
  $form['registration_cancellation']['user_cancel_method'] = array(
333 334
    '#type' => 'item',
    '#title' => t('When cancelling a user account'),
335
    '#default_value' => $config->get('cancel_method'),
336
    '#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'))),
337
  );
338 339
  $form['registration_cancellation']['user_cancel_method'] += user_cancel_methods();
  foreach (element_children($form['registration_cancellation']['user_cancel_method']) as $element) {
340 341
    // Remove all account cancellation methods that have #access defined, as
    // those cannot be configured as default method.
342 343
    if (isset($form['registration_cancellation']['user_cancel_method'][$element]['#access'])) {
      $form['registration_cancellation']['user_cancel_method'][$element]['#access'] = FALSE;
344 345 346
    }
    // Remove the description (only displayed on the confirmation form).
    else {
347
      unset($form['registration_cancellation']['user_cancel_method'][$element]['#description']);
348 349 350
    }
  }

351 352
  // Account settings.
  $form['personalization'] = array(
353
    '#type' => 'fieldset',
354 355 356 357 358
    '#title' => t('Personalization'),
  );
  $form['personalization']['user_signatures'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable signatures.'),
359
    '#default_value' => $config->get('signatures'),
360 361 362
  );
  // If picture support is enabled, check whether the picture directory exists.
  if (variable_get('user_pictures', 0)) {
363
    $picture_path =  file_default_scheme() . '://' . variable_get('user_picture_path', 'pictures');
364 365
    if (!file_prepare_directory($picture_path, FILE_CREATE_DIRECTORY)) {
      form_set_error('user_picture_path', t('The directory %directory does not exist or is not writable.', array('%directory' => $picture_path)));
366
      watchdog('file system', 'The directory %directory does not exist or is not writable.', array('%directory' => $picture_path), WATCHDOG_ERROR);
367
    }
368 369 370 371 372 373 374
  }
  $picture_support = variable_get('user_pictures', 0);
  $form['personalization']['user_pictures'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable user pictures.'),
    '#default_value' => $picture_support,
  );
375
  $form['#attached']['library'][] = array('user', 'drupal.user');
376
  $form['personalization']['pictures'] = array(
377 378 379 380 381 382 383
    '#type' => 'container',
    '#states' => array(
      // Hide the additional picture settings when user pictures are disabled.
      'invisible' => array(
        'input[name="user_pictures"]' => array('checked' => FALSE),
      ),
    ),
384 385 386 387 388 389 390
  );
  $form['personalization']['pictures']['user_picture_path'] = array(
    '#type' => 'textfield',
    '#title' => t('Picture directory'),
    '#default_value' => variable_get('user_picture_path', 'pictures'),
    '#size' => 30,
    '#maxlength' => 255,
391
    '#description' => t('Subdirectory in the file upload directory where pictures will be stored.'),
392 393 394 395 396 397 398 399 400
  );
  $form['personalization']['pictures']['user_picture_default'] = array(
    '#type' => 'textfield',
    '#title' => t('Default picture'),
    '#default_value' => variable_get('user_picture_default', ''),
    '#size' => 30,
    '#maxlength' => 255,
    '#description' => t('URL of picture to display for users with no custom picture selected. Leave blank for none.'),
  );
401 402 403
  if (module_exists('image')) {
    $form['personalization']['pictures']['settings']['user_picture_style'] = array(
      '#type' => 'select',
404
      '#title' => t('Picture display style'),
405 406
      '#options' => image_style_options(TRUE),
      '#default_value' => variable_get('user_picture_style', ''),
407
      '#description' => t('The style selected will be used on display, while the original image is retained. Styles may be configured in the <a href="!url">Image styles</a> administration area.', array('!url' => url('admin/config/media/image-styles'))),
408 409
    );
  }
410 411
  $form['personalization']['pictures']['user_picture_dimensions'] = array(
    '#type' => 'textfield',
412
    '#title' => t('Picture upload dimensions'),
413
    '#default_value' => variable_get('user_picture_dimensions', '85x85'),
414
    '#size' => 10,
415
    '#maxlength' => 10,
416
    '#field_suffix' => ' ' . t('pixels'),
417
    '#description' => t('Pictures larger than this will be scaled down to this size.'),
418 419
  );
  $form['personalization']['pictures']['user_picture_file_size'] = array(
420
    '#type' => 'number',
421
    '#title' => t('Picture upload file size'),
422
    '#default_value' => variable_get('user_picture_file_size', '30'),
423
    '#min' => 0,
424
    '#field_suffix' => ' ' . t('KB'),
425
    '#description' => t('Maximum allowed file size for uploaded pictures. Upload size is normally limited only by the PHP maximum post and file upload settings, and images are automatically scaled down to the dimensions specified above.'),
426 427 428 429 430 431 432 433 434 435 436 437 438 439
  );
  $form['personalization']['pictures']['user_picture_guidelines'] = array(
    '#type' => 'textarea',
    '#title' => t('Picture guidelines'),
    '#default_value' => variable_get('user_picture_guidelines', ''),
    '#description' => t("This text is displayed at the picture upload form in addition to the default guidelines. It's useful for helping or instructing your users."),
  );

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

445
  $form['email_admin_created'] = array(
446
    '#type' => 'fieldset',
447
    '#title' => t('Welcome (new user created by administrator)'),
448
    '#collapsible' => TRUE,
449
    '#collapsed' => ($config->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY),
450
    '#description' => t('Edit the welcome e-mail messages sent to new member accounts created by an administrator.') . ' ' . $email_token_help,
451
    '#group' => 'email',
452
  );
453
  $form['email_admin_created']['user_mail_register_admin_created_subject'] = array(
454 455
    '#type' => 'textfield',
    '#title' => t('Subject'),
456
    '#default_value' => $mail_config->get('register_admin_created.subject'),
457 458
    '#maxlength' => 180,
  );
459
  $form['email_admin_created']['user_mail_register_admin_created_body'] = array(
460 461
    '#type' => 'textarea',
    '#title' => t('Body'),
462
    '#default_value' =>  $mail_config->get('register_admin_created.body'),
463 464 465
    '#rows' => 15,
  );

466
  $form['email_pending_approval'] = array(
467
    '#type' => 'fieldset',
468
    '#title' => t('Welcome (awaiting approval)'),
469
    '#collapsible' => TRUE,
470
    '#collapsed' => ($config->get('register') != USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL),
471
    '#description' => t('Edit the welcome e-mail messages sent to new members upon registering, when administrative approval is required.') . ' ' . $email_token_help,
472
    '#group' => 'email',
473
  );
474
  $form['email_pending_approval']['user_mail_register_pending_approval_subject'] = array(
475 476
    '#type' => 'textfield',
    '#title' => t('Subject'),
477
    '#default_value' => $mail_config->get('register_pending_approval.subject'),
478 479
    '#maxlength' => 180,
  );
480
  $form['email_pending_approval']['user_mail_register_pending_approval_body'] = array(
481 482
    '#type' => 'textarea',
    '#title' => t('Body'),
483
    '#default_value' => $mail_config->get('register_pending_approval.body'),
484
    '#rows' => 8,
485 486
  );

487
  $form['email_no_approval_required'] = array(
488
    '#type' => 'fieldset',
489
    '#title' => t('Welcome (no approval required)'),
490
    '#collapsible' => TRUE,
491
    '#collapsed' => ($config->get('register') != USER_REGISTER_VISITORS),
492
    '#description' => t('Edit the welcome e-mail messages sent to new members upon registering, when no administrator approval is required.') . ' ' . $email_token_help,
493
    '#group' => 'email',
494
  );
495
  $form['email_no_approval_required']['user_mail_register_no_approval_required_subject'] = array(
496 497
    '#type' => 'textfield',
    '#title' => t('Subject'),
498
    '#default_value' => $mail_config->get('register_no_approval_required.subject'),
499 500
    '#maxlength' => 180,
  );
501
  $form['email_no_approval_required']['user_mail_register_no_approval_required_body'] = array(
502 503
    '#type' => 'textarea',
    '#title' => t('Body'),
504
    '#default_value' => $mail_config->get('register_no_approval_required.body'),
505
    '#rows' => 15,
506
  );
507

508
  $form['email_password_reset'] = array(
509
    '#type' => 'fieldset',
510
    '#title' => t('Password recovery'),
511 512
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
513
    '#description' => t('Edit the e-mail messages sent to users who request a new password.') . ' ' . $email_token_help,
514 515
    '#group' => 'email',
    '#weight' => 10,
516
  );
517
  $form['email_password_reset']['user_mail_password_reset_subject'] = array(
518 519
    '#type' => 'textfield',
    '#title' => t('Subject'),
520
    '#default_value' => $mail_config->get('password_reset.subject'),
521 522
    '#maxlength' => 180,
  );
523
  $form['email_password_reset']['user_mail_password_reset_body'] = array(
524 525
    '#type' => 'textarea',
    '#title' => t('Body'),
526
    '#default_value' => $mail_config->get('password_reset.body'),
527 528 529
    '#rows' => 12,
  );

530
  $form['email_activated'] = array(
531
    '#type' => 'fieldset',
532
    '#title' => t('Account activation'),
533 534
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
535
    '#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,
536
    '#group' => 'email',
537
  );
538
  $form['email_activated']['user_mail_status_activated_notify'] = array(
539 540
    '#type' => 'checkbox',
    '#title' => t('Notify user when account is activated.'),
541
    '#default_value' => $config->get('notify.status_activated'),
542
  );
543 544 545 546 547 548 549 550 551 552
  $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(
553 554
    '#type' => 'textfield',
    '#title' => t('Subject'),
555
    '#default_value' => $mail_config->get('status_activated.subject'),
556 557
    '#maxlength' => 180,
  );
558
  $form['email_activated']['settings']['user_mail_status_activated_body'] = array(
559 560
    '#type' => 'textarea',
    '#title' => t('Body'),
561
    '#default_value' => $mail_config->get('status_activated.body'),
562 563 564
    '#rows' => 15,
  );

565
  $form['email_blocked'] = array(
566
    '#type' => 'fieldset',
567
    '#title' => t('Account blocked'),
568 569
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
570
    '#description' => t('Enable and edit e-mail messages sent to users when their accounts are blocked.') . ' ' . $email_token_help,
571
    '#group' => 'email',
572
  );
573
  $form['email_blocked']['user_mail_status_blocked_notify'] = array(
574 575
    '#type' => 'checkbox',
    '#title' => t('Notify user when account is blocked.'),
576
    '#default_value' => $config->get('notify.status_blocked'),
577
  );
578 579 580 581 582 583 584 585 586 587
  $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(
588 589
    '#type' => 'textfield',
    '#title' => t('Subject'),
590
    '#default_value' => $mail_config->get('status_blocked.subject'),
591 592
    '#maxlength' => 180,
  );
593
  $form['email_blocked']['settings']['user_mail_status_blocked_body'] = array(
594 595
    '#type' => 'textarea',
    '#title' => t('Body'),
596
    '#default_value' => $mail_config->get('status_blocked.body'),
597 598 599
    '#rows' => 3,
  );

600
  $form['email_cancel_confirm'] = array(
601
    '#type' => 'fieldset',
602
    '#title' => t('Account cancellation confirmation'),
603 604
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
605
    '#description' => t('Edit the e-mail messages sent to users when they attempt to cancel their accounts.') . ' ' . $email_token_help,
606
    '#group' => 'email',
607
  );
608
  $form['email_cancel_confirm']['user_mail_cancel_confirm_subject'] = array(
609 610
    '#type' => 'textfield',
    '#title' => t('Subject'),
611
    '#default_value' => $mail_config->get('cancel_confirm.subject'),
612 613
    '#maxlength' => 180,
  );
614
  $form['email_cancel_confirm']['user_mail_cancel_confirm_body'] = array(
615 616
    '#type' => 'textarea',
    '#title' => t('Body'),
617
    '#default_value' => $mail_config->get('cancel_confirm.body'),
618 619 620
    '#rows' => 3,
  );

621
  $form['email_canceled'] = array(
622
    '#type' => 'fieldset',
623
    '#title' => t('Account canceled'),
624 625
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
626
    '#description' => t('Enable and edit e-mail messages sent to users when their accounts are canceled.') . ' ' . $email_token_help,
627
    '#group' => 'email',
628
  );
629
  $form['email_canceled']['user_mail_status_canceled_notify'] = array(
630
    '#type' => 'checkbox',
631
    '#title' => t('Notify user when account is canceled.'),
632
    '#default_value' => $config->get('notify.status_canceled'),
633
  );
634 635 636
  $form['email_canceled']['settings'] = array(
    '#type' => 'container',
    '#states' => array(
637
      // Hide the settings when the cancel notify checkbox is disabled.
638 639 640 641 642 643
      'invisible' => array(
        'input[name="user_mail_status_canceled_notify"]' => array('checked' => FALSE),
      ),
    ),
  );
  $form['email_canceled']['settings']['user_mail_status_canceled_subject'] = array(
644 645
    '#type' => 'textfield',
    '#title' => t('Subject'),
646
    '#default_value' => $mail_config->get('status_canceled.subject'),
647 648
    '#maxlength' => 180,
  );
649
  $form['email_canceled']['settings']['user_mail_status_canceled_body'] = array(
650 651
    '#type' => 'textarea',
    '#title' => t('Body'),
652
    '#default_value' => $mail_config->get('status_canceled.body'),
653 654 655
    '#rows' => 3,
  );

656 657 658 659 660 661 662 663 664 665 666 667 668
  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'])
669
    ->set('cancel_method', $form_state['values']['user_cancel_method'])
670 671 672 673
    ->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();
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
  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();
692 693 694 695
}

/**
 * Menu callback: administer permissions.
696
 *
697
 * @ingroup forms
698 699
 * @see user_admin_permissions_submit()
 * @see theme_user_admin_permissions()
700
 */
701
function user_admin_permissions($form, $form_state, $rid = NULL) {
702

703 704
  // Retrieve role names for columns.
  $role_names = user_roles();
705
  if (isset($rid)) {
706
    $role_names = array($rid => $role_names[$rid]);
707
  }
708 709
  // Fetch permissions for all roles or the one selected role.
  $role_permissions = user_role_permissions($role_names);
710

711 712 713 714 715
  // Store $role_names for use when saving the data.
  $form['role_names'] = array(
    '#type' => 'value',
    '#value' => $role_names,
  );
716 717
  // Render role/permission overview:
  $options = array();
718
  $module_info = system_get_info('module');
719
  $hide_descriptions = system_admin_compact_mode();
720 721 722 723

  // Get a list of all the modules implementing a hook_permission() and sort by
  // display name.
  $modules = array();
724
  foreach (module_implements('permission') as $module) {
725
    $modules[$module] = $module_info[$module]['name'];
726
  }
727
  asort($modules);
728

729
  foreach ($modules as $module => $display_name) {
730
    if ($permissions = module_invoke($module, 'permission')) {
731
      $form['permission'][] = array(
732
        '#markup' => $module_info[$module]['name'],
733
        '#id' => $module,
734
      );
735
      foreach ($permissions as $perm => $perm_item) {
736 737 738 739 740 741
        // 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.') : '',
        );
742
        $options[$perm] = '';
743 744
        $form['permission'][$perm] = array(
          '#type' => 'item',
745
          '#markup' => $perm_item['title'],
746
          '#description' => theme('user_permission_description', array('permission_item' => $perm_item, 'hide' => $hide_descriptions)),
747
        );
748 749
        foreach ($role_names as $rid => $name) {
          // Builds arrays for checked boxes for each role
750
          if (isset($role_permissions[$rid][$perm])) {
751 752 753 754 755 756 757 758 759
            $status[$rid][] = $perm;
          }
        }
      }
    }
  }

  // Have to build checkboxes here after checkbox arrays are built
  foreach ($role_names as $rid => $name) {
760 761 762 763 764 765
    $form['checkboxes'][$rid] = array(
      '#type' => 'checkboxes',
      '#options' => $options,
      '#default_value' => isset($status[$rid]) ? $status[$rid] : array(),
      '#attributes' => array('class' => array('rid-' . $rid)),
    );
766
    $form['role_names'][$rid] = array('#markup' => check_plain($name), '#tree' => TRUE);
767
  }
768

769
  $form['actions'] = array('#type' => 'actions');
770
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save permissions'));
771

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

774 775 776
  return $form;
}

777 778 779
/**
 * Save permissions selected on the administer permissions page.
 *
780
 * @see user_admin_permissions()
781
 */
782
function user_admin_permissions_submit($form, &$form_state) {
783
  foreach ($form_state['values']['role_names'] as $rid => $name) {
784
    user_role_change_permissions($rid, $form_state['values'][$rid]);
785 786 787 788
  }

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

789
  // Clear the cached pages and blocks.
790
  cache_invalidate(array('content' => TRUE));