Code coverage for /20080809/modules/user/user.admin.inc

Line #Times calledCode
1
<?php
2
// $Id: user.admin.inc,v 1.24 2008/07/24 16:28:52 dries Exp $
3
4
/**
5
 * @file
6
 * Admin page callback file for the user module.
7
 */
8
9119
function user_admin($callback_arg = '') {
104
  $op = isset($_POST['op']) ? $_POST['op'] : $callback_arg;
11
12
  switch ($op) {
134
    case t('Create new account'):
144
    case 'create':
153
      $output = drupal_get_form('user_register');
162
      break;
171
    default:
181
      if (!empty($_POST['accounts']) && isset($_POST['operation']) &&
($_POST['operation'] == 'delete')) {
190
        $output = drupal_get_form('user_multiple_delete_confirm');
200
      }
21
      else {
221
        $output = drupal_get_form('user_filter_form');
231
        $output .= drupal_get_form('user_admin_account');
24
      }
251
  }
263
  return $output;
270
}
28
29
/**
30
 * Form builder; Return form for user administration filters.
31
 *
32
 * @ingroup forms
33
 * @see user_filter_form_submit()
34
 */
35119
function user_filter_form() {
361
  $session = &$_SESSION['user_overview_filter'];
371
  $session = is_array($session) ? $session : array();
381
  $filters = user_filters();
39
401
  $i = 0;
411
  $form['filters'] = array(
421
    '#type' => 'fieldset',
431
    '#title' => t('Show only users where'),
441
    '#theme' => 'user_filters',
45
  );
461
  foreach ($session as $filter) {
470
    list($type, $value) = $filter;
48
    // Merge an array of arrays into one if necessary.
490
    $options = $type == 'permission' ? call_user_func_array('array_merge',
$filters[$type]['options']) : $filters[$type]['options'];
500
    $params = array('%property' => $filters[$type]['title'] , '%value' =>
$options[$value]);
510
    if ($i++ > 0) {
520
      $form['filters']['current'][] = array('#markup' => t('<em>and</em>
where <strong>%property</strong> is <strong>%value</strong>', $params));
530
    }
54
    else {
550
      $form['filters']['current'][] = array('#markup' =>
t('<strong>%property</strong> is <strong>%value</strong>', $params));
56
    }
570
  }
58
591
  foreach ($filters as $key => $filter) {
601
    $names[$key] = $filter['title'];
611
    $form['filters']['status'][$key] = array(
621
      '#type' => 'select',
631
      '#options' => $filter['options'],
64
    );
651
  }
66
671
  $form['filters']['filter'] = array(
681
    '#type' => 'radios',
691
    '#options' => $names,
70
  );
711
  $form['filters']['buttons']['submit'] = array(
721
    '#type' => 'submit',
731
    '#value' => (count($session) ? t('Refine') : t('Filter')),
74
  );
751
  if (count($session)) {
760
    $form['filters']['buttons']['undo'] = array(
770
      '#type' => 'submit',
780
      '#value' => t('Undo'),
79
    );
800
    $form['filters']['buttons']['reset'] = array(
810
      '#type' => 'submit',
820
      '#value' => t('Reset'),
83
    );
840
  }
85
861
  drupal_add_js('misc/form.js', 'core');
87
881
  return $form;
890
}
90
91
/**
92
 * Process result from user administration filter form.
93
 */
94119
function user_filter_form_submit($form, &$form_state) {
950
  $op = $form_state['values']['op'];
960
  $filters = user_filters();
97
  switch ($op) {
980
    case t('Filter'): case t('Refine'):
990
      if (isset($form_state['values']['filter'])) {
1000
        $filter = $form_state['values']['filter'];
101
        // Merge an array of arrays into one if necessary.
1020
        $options = $filter == 'permission' ?
call_user_func_array('array_merge', $filters[$filter]['options']) :
$filters[$filter]['options'];
1030
        if (isset($options[$form_state['values'][$filter]])) {
1040
          $_SESSION['user_overview_filter'][] = array($filter,
$form_state['values'][$filter]);
1050
        }
1060
      }
1070
      break;
1080
    case t('Undo'):
1090
      array_pop($_SESSION['user_overview_filter']);
1100
      break;
1110
    case t('Reset'):
1120
      $_SESSION['user_overview_filter'] = array();
1130
      break;
1140
    case t('Update'):
1150
      return;
1160
  }
117
1180
  $form_state['redirect'] = 'admin/user/user';
1190
  return;
1200
}
121
122
/**
123
 * Form builder; User administration page.
124
 *
125
 * @ingroup forms
126
 * @see user_admin_account_validate()
127
 * @see user_admin_account_submit()
128
 */
129119
function user_admin_account() {
1301
  $filter = user_build_filter_query();
131
132
  $header = array(
1331
    array(),
1341
    array('data' => t('Username'), 'field' => 'u.name'),
1351
    array('data' => t('Status'), 'field' => 'u.status'),
1361
    t('Roles'),
1371
    array('data' => t('Member for'), 'field' => 'u.created', 'sort' =>
'desc'),
1381
    array('data' => t('Last access'), 'field' => 'u.access'),
1391
    t('Operations')
1401
  );
141
1421
  $sql = 'SELECT DISTINCT u.uid, u.name, u.status, u.created, u.access FROM
{users} u LEFT JOIN {users_roles} ur ON u.uid = ur.uid ' . $filter['join']
. ' WHERE u.uid != 0 ' . $filter['where'];
1431
  $sql .= tablesort_sql($header);
1441
  $query_count = 'SELECT COUNT(DISTINCT u.uid) FROM {users} u LEFT JOIN
{users_roles} ur ON u.uid = ur.uid ' . $filter['join'] . ' WHERE u.uid != 0
' . $filter['where'];
1451
  $result = pager_query($sql, 50, 0, $query_count, $filter['args']);
146
1471
  $form['options'] = array(
1481
    '#type' => 'fieldset',
1491
    '#title' => t('Update options'),
1501
    '#prefix' => '<div class="container-inline">',
1511
    '#suffix' => '</div>',
152
  );
1531
  $options = array();
1541
  foreach (module_invoke_all('user_operations') as $operation => $array) {
1551
    $options[$operation] = $array['label'];
1561
  }
1571
  $form['options']['operation'] = array(
1581
    '#type' => 'select',
1591
    '#options' => $options,
1601
    '#default_value' => 'unblock',
161
  );
1621
  $form['options']['submit'] = array(
1631
    '#type' => 'submit',
1641
    '#value' => t('Update'),
165
  );
166
1671
  $destination = drupal_get_destination();
168
1691
  $status = array(t('blocked'), t('active'));
1701
  $roles = user_roles(TRUE);
1711
  $accounts = array();
1721
  while ($account = db_fetch_object($result)) {
1731
    $accounts[$account->uid] = '';
1741
    $form['name'][$account->uid] = array('#markup' => theme('username',
$account));
1751
    $form['status'][$account->uid] =  array('#markup' =>
$status[$account->status]);
1761
    $users_roles = array();
1771
    $roles_result = db_query('SELECT rid FROM {users_roles} WHERE uid =
%d', $account->uid);
1781
    while ($user_role = db_fetch_object($roles_result)) {
1791
      $users_roles[] = $roles[$user_role->rid];
1801
    }
1811
    asort($users_roles);
1821
    $form['roles'][$account->uid][0] = array('#markup' =>
theme('item_list', $users_roles));
1831
    $form['member_for'][$account->uid] = array('#markup' =>
format_interval(time() - $account->created));
1841
    $form['last_access'][$account->uid] =  array('#markup' =>
$account->access ? t('@time ago', array('@time' => format_interval(time() -
$account->access))) : t('never'));
1851
    $form['operations'][$account->uid] = array('#markup' => l(t('edit'),
"user/$account->uid/edit", array('query' => $destination)));
1861
  }
1871
  $form['accounts'] = array(
1881
    '#type' => 'checkboxes',
189
    '#options' => $accounts
1901
  );
1911
  $form['pager'] = array('#markup' => theme('pager', NULL, 50, 0));
192
1931
  return $form;
1940
}
195
196
/**
197
 * Submit the user administration update form.
198
 */
199119
function user_admin_account_submit($form, &$form_state) {
2000
  $operations = module_invoke_all('user_operations', $form_state);
2010
  $operation = $operations[$form_state['values']['operation']];
202
  // Filter out unchecked accounts.
2030
  $accounts = array_filter($form_state['values']['accounts']);
2040
  if ($function = $operation['callback']) {
205
    // Add in callback arguments if present.
2060
    if (isset($operation['callback arguments'])) {
2070
      $args = array_merge(array($accounts), $operation['callback
arguments']);
2080
    }
209
    else {
2100
      $args = array($accounts);
211
    }
2120
    call_user_func_array($function, $args);
213
2140
    drupal_set_message(t('The update has been performed.'));
2150
  }
2160
}
217
218119
function user_admin_account_validate($form, &$form_state) {
2190
  $form_state['values']['accounts'] =
array_filter($form_state['values']['accounts']);
2200
  if (count($form_state['values']['accounts']) == 0) {
2210
    form_set_error('', t('No users selected.'));
2220
  }
2230
}
224
225
/**
226
 * Form builder; Configure user settings for this site.
227
 *
228
 * @ingroup forms
229
 * @see system_settings_form()
230
 */
231119
function user_admin_settings() {
232
  // User registration settings.
2330
  $form['registration'] = array('#type' => 'fieldset', '#title' => t('User
registration settings'));
2340
  $form['registration']['user_register'] = array('#type' => 'radios',
'#title' => t('Public registrations'), '#default_value' =>
variable_get('user_register', 1), '#options' => array(t('Only site
administrators can create new user accounts.'), t('Visitors can create
accounts and no administrator approval is required.'), t('Visitors can
create accounts but administrator approval is required.')));
2350
  $form['registration']['user_email_verification'] = array('#type' =>
'checkbox', '#title' => t('Require e-mail verification when a visitor
creates an account'), '#default_value' =>
variable_get('user_email_verification', TRUE), '#description' => t('If this
box is checked, 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 it unchecked, users will be logged in immediately upon
registering, and may select their own passwords during registration.'));
2360
  $form['registration']['user_registration_help'] = array('#type' =>
'textarea', '#title' => t('User registration guidelines'), '#default_value'
=> variable_get('user_registration_help', ''), '#description' => t('This
text is displayed at the top of the user registration form and is useful
for helping or instructing your users.'));
237
238
  // User e-mail settings.
2390
  $form['email'] = array(
2400
    '#type' => 'fieldset',
2410
    '#title' => t('User e-mail settings'),
2420
    '#description' => t('Drupal sends emails whenever new users register on
your site, and optionally, may also notify users after other account
actions. Using a simple set of content templates, notification e-mails can
be customized to fit the specific needs of your site.'),
243
  );
244
  // These email tokens are shared for all settings, so just define
245
  // the list once to help ensure they stay in sync.
2460
  $email_token_help = t('Available variables are:') . ' !username, !site,
!password, !uri, !uri_brief, !mailto, !date, !login_uri, !edit_uri,
!login_url.';
247
2480
  $form['email']['admin_created'] = array(
2490
    '#type' => 'fieldset',
2500
    '#title' => t('Welcome, new user created by administrator'),
2510
    '#collapsible' => TRUE,
2520
    '#collapsed' => (variable_get('user_register', 1) != 0),
2530
    '#description' => t('Customize welcome e-mail messages sent to new
member accounts created by an administrator.') . ' ' . $email_token_help,
254
  );
2550
 
$form['email']['admin_created']['user_mail_register_admin_created_subject']
= array(
2560
    '#type' => 'textfield',
2570
    '#title' => t('Subject'),
2580
    '#default_value' => _user_mail_text('register_admin_created_subject'),
2590
    '#maxlength' => 180,
260
  );
2610
  $form['email']['admin_created']['user_mail_register_admin_created_body']
= array(
2620
    '#type' => 'textarea',
2630
    '#title' => t('Body'),
2640
    '#default_value' => _user_mail_text('register_admin_created_body'),
2650
    '#rows' => 15,
266
  );
267
2680
  $form['email']['no_approval_required'] = array(
2690
    '#type' => 'fieldset',
2700
    '#title' => t('Welcome, no approval required'),
2710
    '#collapsible' => TRUE,
2720
    '#collapsed' => (variable_get('user_register', 1) != 1),
2730
    '#description' => t('Customize welcome e-mail messages sent to new
members upon registering, when no administrator approval is required.') . '
' . $email_token_help
2740
  );
2750
 
$form['email']['no_approval_required']['user_mail_register_no_approval_required_subject']
= array(
2760
    '#type' => 'textfield',
2770
    '#title' => t('Subject'),
2780
    '#default_value' =>
_user_mail_text('register_no_approval_required_subject'),
2790
    '#maxlength' => 180,
280
  );
2810
 
$form['email']['no_approval_required']['user_mail_register_no_approval_required_body']
= array(
2820
    '#type' => 'textarea',
2830
    '#title' => t('Body'),
2840
    '#default_value' =>
_user_mail_text('register_no_approval_required_body'),
2850
    '#rows' => 15,
286
  );
287
2880
  $form['email']['pending_approval'] = array(
2890
    '#type' => 'fieldset',
2900
    '#title' => t('Welcome, awaiting administrator approval'),
2910
    '#collapsible' => TRUE,
2920
    '#collapsed' => (variable_get('user_register', 1) != 2),
2930
    '#description' => t('Customize welcome e-mail messages sent to new
members upon registering, when administrative approval is required.') . ' '
. $email_token_help,
294
  );
2950
 
$form['email']['pending_approval']['user_mail_register_pending_approval_subject']
= array(
2960
    '#type' => 'textfield',
2970
    '#title' => t('Subject'),
2980
    '#default_value' =>
_user_mail_text('register_pending_approval_subject'),
2990
    '#maxlength' => 180,
300
  );
3010
 
$form['email']['pending_approval']['user_mail_register_pending_approval_body']
= array(
3020
    '#type' => 'textarea',
3030
    '#title' => t('Body'),
3040
    '#default_value' => _user_mail_text('register_pending_approval_body'),
3050
    '#rows' => 8,
306
  );
307
3080
  $form['email']['password_reset'] = array(
3090
    '#type' => 'fieldset',
3100
    '#title' => t('Password recovery email'),
3110
    '#collapsible' => TRUE,
3120
    '#collapsed' => TRUE,
3130
    '#description' => t('Customize e-mail messages sent to users who
request a new password.') . ' ' . $email_token_help,
314
  );
3150
  $form['email']['password_reset']['user_mail_password_reset_subject'] =
array(
3160
    '#type' => 'textfield',
3170
    '#title' => t('Subject'),
3180
    '#default_value' => _user_mail_text('password_reset_subject'),
3190
    '#maxlength' => 180,
320
  );
3210
  $form['email']['password_reset']['user_mail_password_reset_body'] =
array(
3220
    '#type' => 'textarea',
3230
    '#title' => t('Body'),
3240
    '#default_value' => _user_mail_text('password_reset_body'),
3250
    '#rows' => 12,
326
  );
327
3280
  $form['email']['activated'] = array(
3290
    '#type' => 'fieldset',
3300
    '#title' => t('Account activation email'),
3310
    '#collapsible' => TRUE,
3320
    '#collapsed' => TRUE,
3330
    '#description' => t('Enable and customize 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,
334
  );
3350
  $form['email']['activated']['user_mail_status_activated_notify'] =
array(
3360
    '#type' => 'checkbox',
3370
    '#title' => t('Notify user when account is activated.'),
3380
    '#default_value' => variable_get('user_mail_status_activated_notify',
TRUE),
339
  );
3400
  $form['email']['activated']['user_mail_status_activated_subject'] =
array(
3410
    '#type' => 'textfield',
3420
    '#title' => t('Subject'),
3430
    '#default_value' => _user_mail_text('status_activated_subject'),
3440
    '#maxlength' => 180,
345
  );
3460
  $form['email']['activated']['user_mail_status_activated_body'] = array(
3470
    '#type' => 'textarea',
3480
    '#title' => t('Body'),
3490
    '#default_value' => _user_mail_text('status_activated_body'),
3500
    '#rows' => 15,
351
  );
352
3530
  $form['email']['blocked'] = array(
3540
    '#type' => 'fieldset',
3550
    '#title' => t('Account blocked email'),
3560
    '#collapsible' => TRUE,
3570
    '#collapsed' => TRUE,
3580
    '#description' => t('Enable and customize e-mail messages sent to users
when their accounts are blocked.') . ' ' . $email_token_help,
359
  );
3600
  $form['email']['blocked']['user_mail_status_blocked_notify'] = array(
3610
    '#type' => 'checkbox',
3620
    '#title' => t('Notify user when account is blocked.'),
3630
    '#default_value' => variable_get('user_mail_status_blocked_notify',
FALSE),
364
  );
3650
  $form['email']['blocked']['user_mail_status_blocked_subject'] = array(
3660
    '#type' => 'textfield',
3670
    '#title' => t('Subject'),
3680
    '#default_value' => _user_mail_text('status_blocked_subject'),
3690
    '#maxlength' => 180,
370
  );
3710
  $form['email']['blocked']['user_mail_status_blocked_body'] = array(
3720
    '#type' => 'textarea',
3730
    '#title' => t('Body'),
3740
    '#default_value' => _user_mail_text('status_blocked_body'),
3750
    '#rows' => 3,
376
  );
377
3780
  $form['email']['deleted'] = array(
3790
    '#type' => 'fieldset',
3800
    '#title' => t('Account deleted email'),
3810
    '#collapsible' => TRUE,
3820
    '#collapsed' => TRUE,
3830
    '#description' => t('Enable and customize e-mail messages sent to users
when their accounts are deleted.') . ' ' . $email_token_help,
384
  );
3850
  $form['email']['deleted']['user_mail_status_deleted_notify'] = array(
3860
    '#type' => 'checkbox',
3870
    '#title' => t('Notify user when account is deleted.'),
3880
    '#default_value' => variable_get('user_mail_status_deleted_notify',
FALSE),
389
  );
3900
  $form['email']['deleted']['user_mail_status_deleted_subject'] = array(
3910
    '#type' => 'textfield',
3920
    '#title' => t('Subject'),
3930
    '#default_value' => _user_mail_text('status_deleted_subject'),
3940
    '#maxlength' => 180,
395
  );
3960
  $form['email']['deleted']['user_mail_status_deleted_body'] = array(
3970
    '#type' => 'textarea',
3980
    '#title' => t('Body'),
3990
    '#default_value' => _user_mail_text('status_deleted_body'),
4000
    '#rows' => 3,
401
  );
402
403
  // User signatures.
4040
  $form['signatures'] = array(
4050
    '#type' => 'fieldset',
4060
    '#title' => t('Signatures'),
407
  );
4080
  $form['signatures']['user_signatures'] = array(
4090
    '#type' => 'radios',
4100
    '#title' => t('Signature support'),
4110
    '#default_value' => variable_get('user_signatures', 0),
4120
    '#options' => array(t('Disabled'), t('Enabled')),
413
  );
414
415
  // If picture support is enabled, check whether the picture directory
exists:
4160
  if (variable_get('user_pictures', 0)) {
4170
    $picture_path = file_create_path(variable_get('user_picture_path',
'pictures'));
4180
    file_check_directory($picture_path, 1, 'user_picture_path');
4190
  }
420
4210
  $form['pictures'] = array(
4220
    '#type' => 'fieldset',
4230
    '#title' => t('Pictures'),
424
  );
4250
  $picture_support = variable_get('user_pictures', 0);
4260
  $form['pictures']['user_pictures'] = array(
4270
    '#type' => 'radios',
4280
    '#title' => t('Picture support'),
4290
    '#default_value' => $picture_support,
4300
    '#options' => array(t('Disabled'), t('Enabled')),
4310
    '#prefix' => '<div class="user-admin-picture-radios">',
4320
    '#suffix' => '</div>',
433
  );
4340
  drupal_add_js(drupal_get_path('module', 'user') . '/user.js');
435
  // If JS is enabled, and the radio is defaulting to off, hide all
436
  // the settings on page load via .css using the js-hide class so
437
  // that there's no flicker.
4380
  $css_class = 'user-admin-picture-settings';
4390
  if (!$picture_support) {
4400
    $css_class .= ' js-hide';
4410
  }
4420
  $form['pictures']['settings'] = array(
4430
    '#prefix' => '<div class="' . $css_class . '">',
4440
    '#suffix' => '</div>',
445
  );
4460
  $form['pictures']['settings']['user_picture_path'] = array(
4470
    '#type' => 'textfield',
4480
    '#title' => t('Picture image path'),
4490
    '#default_value' => variable_get('user_picture_path', 'pictures'),
4500
    '#size' => 30,
4510
    '#maxlength' => 255,
4520
    '#description' => t('Subdirectory in the directory %dir where pictures
will be stored.', array('%dir' => file_directory_path() . '/')),
453
  );
4540
  $form['pictures']['settings']['user_picture_default'] = array(
4550
    '#type' => 'textfield',
4560
    '#title' => t('Default picture'),
4570
    '#default_value' => variable_get('user_picture_default', ''),
4580
    '#size' => 30,
4590
    '#maxlength' => 255,
4600
    '#description' => t('URL of picture to display for users with no custom
picture selected. Leave blank for none.'),
461
  );
4620
  $form['pictures']['settings']['user_picture_dimensions'] = array(
4630
    '#type' => 'textfield',
4640
    '#title' => t('Picture maximum dimensions'),
4650
    '#default_value' => variable_get('user_picture_dimensions', '85x85'),
4660
    '#size' => 15,
4670
    '#maxlength' => 10,
4680
    '#description' => t('Maximum dimensions for pictures, in pixels.'),
469
  );
4700
  $form['pictures']['settings']['user_picture_file_size'] = array(
4710
    '#type' => 'textfield',
4720
    '#title' => t('Picture maximum file size'),
4730
    '#default_value' => variable_get('user_picture_file_size', '30'),
4740
    '#size' => 15,
4750
    '#maxlength' => 10,
4760
    '#description' => t('Maximum file size for pictures, in kB.'),
477
  );
4780
  $form['pictures']['settings']['user_picture_guidelines'] = array(
4790
    '#type' => 'textarea',
4800
    '#title' => t('Picture guidelines'),
4810
    '#default_value' => variable_get('user_picture_guidelines', ''),
4820
    '#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."),
483
  );
484
4850
  return system_settings_form($form);
4860
}
487
488
/**
489
 * Menu callback: administer permissions.
490
 *
491
 * @ingroup forms
492
 * @see user_admin_perm_submit()
493
 * @see theme_user_admin_perm()
494
 */
495119
function user_admin_perm($form_state, $rid = NULL) {
496
497
  // Retrieve role names for columns.
49824
  $role_names = user_roles();
49924
  if (is_numeric($rid)) {
5000
    $role_names = array($rid => $role_names[$rid]);
5010
  }
502
  // Fetch permissions for all roles or the one selected role.
50324
  $role_permissions = user_role_permissions($role_names);
504
505
  // Store $role_names for use when saving the data.
50624
  $form['role_names'] = array(
50724
    '#type' => 'value',
50824
    '#value' => $role_names,
509
  );
510
  // Render role/permission overview:
51124
  $options = array();
51224
  $hide_descriptions = !system_admin_compact_mode();
51324
  foreach (module_list(FALSE, FALSE, TRUE) as $module) {
51424
    if ($permissions = module_invoke($module, 'perm')) {
51524
      $form['permission'][] = array(
51624
        '#markup' => $module,
517
      );
51824
      ksort($permissions);
51924
      foreach ($permissions as $perm => $description) {
520
        // Account for permissions lacking a description.
52124
        if (is_int($perm)) {
5220
          $perm = $description;
5230
          $description = NULL;
5240
        }
52524
        $options[$perm] = '';
52624
        $form['permission'][$perm] = array(
52724
          '#type' => 'item',
52824
          '#markup' => t($perm),
52924
          '#description' => $hide_descriptions ? $description : NULL,
530
        );
53124
        foreach ($role_names as $rid => $name) {
532
          // Builds arrays for checked boxes for each role
53324
          if (isset($role_permissions[$rid][$perm])) {
53424
            $status[$rid][] = $perm;
53524
          }
53624
        }
53724
      }
53824
    }
53924
  }
540
541
  // Have to build checkboxes here after checkbox arrays are built
54224
  foreach ($role_names as $rid => $name) {
54324
    $form['checkboxes'][$rid] = array('#type' => 'checkboxes', '#options'
=> $options, '#default_value' => isset($status[$rid]) ? $status[$rid] :
array());
54424
    $form['role_names'][$rid] = array('#markup' => $name, '#tree' =>
TRUE);
54524
  }
54624
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save
permissions'));
547
54824
  return $form;
5490
}
550
551
/**
552
 * Save permissions selected on the administer permissions page.
553
 *
554
 * @see user_admin_perm
555
 */
556119
function user_admin_perm_submit($form, &$form_state) {
5578
  foreach ($form_state['values']['role_names'] as $rid => $name) {
5588
    $checked = array_filter($form_state['values'][$rid]);
559
    // Delete existing permissions for the role. This handles "unchecking"
checkboxes.
5608
    db_query("DELETE FROM {role_permission} WHERE rid = %d", $rid);
5618
    foreach ($checked as $permission) {
5628
      db_query("INSERT INTO {role_permission} (rid, permission) VALUES (%d,
'%s')", $rid, $permission);
5638
    }
5648
  }
565
5668
  drupal_set_message(t('The changes have been saved.'));
567
568
  // Clear the cached pages and blocks.
5698
  cache_clear_all();
5708
}
571
572
/**
573
 * Theme the administer permissions page.
574
 *
575
 * @ingroup themeable
576
 */
577119
function theme_user_admin_perm($form) {
57816
  $roles = user_roles();
57916
  foreach (element_children($form['permission']) as $key) {
580
    // Don't take form control structures
58116
    if (is_array($form['permission'][$key])) {
58216
      $row = array();
583
      // Module name
58416
      if (is_numeric($key)) {
58516
        $row[] = array('data' => t('@module module', array('@module' =>
drupal_render($form['permission'][$key]))), 'class' => 'module', 'id' =>
'module-' . $form['permission'][$key]['#markup'], 'colspan' =>
count($form['role_names']) + 1);
58616
      }
587
      else {
588
        // Permission row.
58916
        $row[] = array(
59016
          'data' => drupal_render($form['permission'][$key]),
59116
          'class' => 'permission',
592
        );
59316
        foreach (element_children($form['checkboxes']) as $rid) {
59416
          if (is_array($form['checkboxes'][$rid])) {
59516
            $row[] = array('data' =>
drupal_render($form['checkboxes'][$rid][$key]), 'class' => 'checkbox',
'title' => $roles[$rid] . ' : ' . t($key));
59616
          }
59716
        }
598
      }
59916
      $rows[] = $row;
60016
    }
60116
  }
60216
  $header[] = (t('Permission'));
60316
  foreach (element_children($form['role_names']) as $rid) {
60416
    if (is_array($form['role_names'][$rid])) {
60516
      $header[] = array('data' => drupal_render($form['role_names'][$rid]),
'class' => 'checkbox');
60616
    }
60716
  }
60816
  $output = theme('system_compact_link');
60916
  $output .= theme('table', $header, $rows, array('id' => 'permissions'));
61016
  $output .= drupal_render($form);
61116
  return $output;
6120
}
613
614
/**
615
 * Menu callback: administer roles.
616
 *
617
 * @ingroup forms
618
 * @see user_admin_role_validate()
619
 * @see user_admin_role_submit()
620
 * @see theme_user_admin_new_role()
621
 */
622119
function user_admin_role() {
6230
  $rid = arg(4);
6240
  if ($rid) {
6250
    if ($rid == DRUPAL_ANONYMOUS_RID || $rid == DRUPAL_AUTHENTICATED_RID)
{
6260
      drupal_goto('admin/user/roles');
6270
    }
628
    // Display the edit role form.
6290
    $role = db_fetch_object(db_query('SELECT * FROM {role} WHERE rid = %d',
$rid));
6300
    $form['name'] = array(
6310
      '#type' => 'textfield',
6320
      '#title' => t('Role name'),
6330
      '#default_value' => $role->name,
6340
      '#size' => 30,
6350
      '#required' => TRUE,
6360
      '#maxlength' => 64,
6370
      '#description' => t('The name for this role. Example: "moderator",
"editorial board", "site architect".'),
638
    );
6390
    $form['rid'] = array(
6400
      '#type' => 'value',
6410
      '#value' => $rid,
642
    );
6430
    $form['submit'] = array(
6440
      '#type' => 'submit',
6450
      '#value' => t('Save role'),
646
    );
6470
    $form['delete'] = array(
6480
      '#type' => 'submit',
6490
      '#value' => t('Delete role'),
650
    );
6510
  }
652
  else {
6530
    $form['name'] = array(
6540
      '#type' => 'textfield',
6550
      '#size' => 32,
6560
      '#maxlength' => 64,
657
    );
6580
    $form['submit'] = array(
6590
      '#type' => 'submit',
6600
      '#value' => t('Add role'),
661
    );
6620
    $form['#submit'][] = 'user_admin_role_submit';
6630
    $form['#validate'][] = 'user_admin_role_validate';
664
  }
6650
  return $form;
6660
}
667
668119
function user_admin_role_validate($form, &$form_state) {
6690
  if ($form_state['values']['name']) {
6700
    if ($form_state['values']['op'] == t('Save role')) {
6710
      if (db_result(db_query("SELECT COUNT(*) FROM {role} WHERE name = '%s'
AND rid != %d", $form_state['values']['name'],
$form_state['values']['rid']))) {
6720
        form_set_error('name', t('The role name %name already exists.
Please choose another role name.', array('%name' =>
$form_state['values']['name'])));
6730
      }
6740
    }
6750
    else if ($form_state['values']['op'] == t('Add role')) {
6760
      if (db_result(db_query("SELECT COUNT(*) FROM {role} WHERE name =
'%s'", $form_state['values']['name']))) {
6770
        form_set_error('name', t('The role name %name already exists.
Please choose another role name.', array('%name' =>
$form_state['values']['name'])));
6780
      }
6790
    }
6800
  }
681
  else {
6820
    form_set_error('name', t('You must specify a valid role name.'));
683
  }
6840
}
685
686119
function user_admin_role_submit($form, &$form_state) {
6870
  if ($form_state['values']['op'] == t('Save role')) {
6880
    db_query("UPDATE {role} SET name = '%s' WHERE rid = %d",
$form_state['values']['name'], $form_state['values']['rid']);
6890
    drupal_set_message(t('The role has been renamed.'));
6900
  }
6910
  else if ($form_state['values']['op'] == t('Delete role')) {
6920
    db_query('DELETE FROM {role} WHERE rid = %d',
$form_state['values']['rid']);
6930
    db_query('DELETE FROM {role_permission} WHERE rid = %d',
$form_state['values']['rid']);
694
    // Update the users who have this role set:
6950
    db_query('DELETE FROM {users_roles} WHERE rid = %d',
$form_state['values']['rid']);
696
6970
    drupal_set_message(t('The role has been deleted.'));
6980
  }
6990
  else if ($form_state['values']['op'] == t('Add role')) {
7000
    db_query("INSERT INTO {role} (name) VALUES ('%s')",
$form_state['values']['name']);
7010
    drupal_set_message(t('The role has been added.'));
7020
  }
7030
  $form_state['redirect'] = 'admin/user/roles';
7040
  return;
7050
}
706
707
/**
708
 * Theme user administration overview.
709
 *
710
 * @ingroup themeable
711
 */
712119
function theme_user_admin_account($form) {
713
  // Overview table:
714
  $header = array(
7151
    theme('table_select_header_cell'),
7161
    array('data' => t('Username'), 'field' => 'u.name'),
7171
    array('data' => t('Status'), 'field' => 'u.status'),
7181
    t('Roles'),
7191
    array('data' => t('Member for'), 'field' => 'u.created', 'sort' =>
'desc'),
7201
    array('data' => t('Last access'), 'field' => 'u.access'),
7211
    t('Operations')
7221
  );
723
7241
  $output = drupal_render($form['options']);
7251
  if (isset($form['name']) && is_array($form['name'])) {
7261
    foreach (element_children($form['name']) as $key) {
7271
      $rows[] = array(
7281
        drupal_render($form['accounts'][$key]),
7291
        drupal_render($form['name'][$key]),
7301
        drupal_render($form['status'][$key]),
7311
        drupal_render($form['roles'][$key]),
7321
        drupal_render($form['member_for'][$key]),
7331
        drupal_render($form['last_access'][$key]),
7341
        drupal_render($form['operations'][$key]),
735
      );
7361
    }
7371
  }
738
  else {
7390
    $rows[] = array(array('data' => t('No users available.'), 'colspan' =>
'7'));
740
  }
741
7421
  $output .= theme('table', $header, $rows);
7431
  if ($form['pager']['#markup']) {
7440
    $output .= drupal_render($form['pager']);
7450
  }
746
7471
  $output .= drupal_render($form);
748
7491
  return $output;
7500
}
751
752
/**
753
 * Theme the new-role form.
754
 *
755
 * @ingroup themeable
756
 */
757119
function theme_user_admin_new_role($form) {
7580
  $header = array(t('Name'), array('data' => t('Operations'), 'colspan' =>
2));
7590
  foreach (user_roles() as $rid => $name) {
7600
    $edit_permissions = l(t('edit permissions'), 'admin/user/permissions/'
. $rid);
7610
    if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID,
DRUPAL_AUTHENTICATED_RID))) {
7620
      $rows[] = array($name, l(t('edit role'), 'admin/user/roles/edit/' .
$rid), $edit_permissions);
7630
    }
764
    else {
7650
      $rows[] = array($name, t('locked'), $edit_permissions);
766
    }
7670
  }
7680
  $rows[] = array(drupal_render($form['name']), array('data' =>
drupal_render($form['submit']), 'colspan' => 2));
769
7700
  $output = drupal_render($form);
7710
  $output .= theme('table', $header, $rows);
772
7730
  return $output;
7740
}
775
776
/**
777
 * Theme user administration filter form.
778
 *
779
 * @ingroup themeable
780
 */
781119
function theme_user_filter_form($form) {
7821
  $output = '<div id="user-admin-filter">';
7831
  $output .= drupal_render($form['filters']);
7841
  $output .= '</div>';
7851
  $output .= drupal_render($form);
7861
  return $output;
7870
}
788
789
/**
790
 * Theme user administration filter selector.
791
 *
792
 * @ingroup themeable
793
 */
794119
function theme_user_filters($form) {
7951
  $output = '<ul class="clear-block">';
7961
  if (!empty($form['current'])) {
7970
    foreach (element_children($form['current']) as $key) {
7980
      $output .= '<li>' . drupal_render($form['current'][$key]) . '</li>';
7990
    }
8000
  }
801
8021
  $output .= '<li><dl class="multiselect">' . (!empty($form['current']) ?
'<dt><em>' . t('and') . '</em> ' . t('where') . '</dt>' : '') . '<dd
class="a">';
8031
  foreach (element_children($form['filter']) as $key) {
8041
    $output .= drupal_render($form['filter'][$key]);
8051
  }
8061
  $output .= '</dd>';
807
8081
  $output .= '<dt>' . t('is') . '</dt><dd class="b">';
809
8101
  foreach (element_children($form['status']) as $key) {
8111
    $output .= drupal_render($form['status'][$key]);
8121
  }
8131
  $output .= '</dd>';
814
8151
  $output .= '</dl>';
8161
  $output .= '<div class="container-inline" id="user-admin-buttons">' .
drupal_render($form['buttons']) . '</div>';
8171
  $output .= '</li></ul>';
818
8191
  return $output;
8200
}
821119