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

Line #Times calledCode
1
<?php
2
// $Id: user.admin.inc,v 1.30 2008/10/19 21:19:02 dries Exp $
3
4
/**
5
 * @file
6
 * Admin page callback file for the user module.
7
 */
8
9219
function user_admin($callback_arg = '') {
1011
  $op = isset($_POST['op']) ? $_POST['op'] : $callback_arg;
11
12
  switch ($op) {
1311
    case t('Create new account'):
1411
    case 'create':
153
      $output = drupal_get_form('user_register');
162
      break;
178
    default:
188
      if (!empty($_POST['accounts']) && isset($_POST['operation']) &&
($_POST['operation'] == 'delete')) {
190
        $output = drupal_get_form('user_multiple_delete_confirm');
200
      }
21
      else {
228
        $output = drupal_get_form('user_filter_form');
237
        $output .= drupal_get_form('user_admin_account');
24
      }
258
  }
268
  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
 */
35219
function user_filter_form() {
368
  $session = &$_SESSION['user_overview_filter'];
378
  $session = is_array($session) ? $session : array();
388
  $filters = user_filters();
39
408
  $i = 0;
418
  $form['filters'] = array(
428
    '#type' => 'fieldset',
438
    '#title' => t('Show only users where'),
448
    '#theme' => 'user_filters',
45
  );
468
  foreach ($session as $filter) {
474
    list($type, $value) = $filter;
48
    // Merge an array of arrays into one if necessary.
494
    $options = $type == 'permission' ? call_user_func_array('array_merge',
$filters[$type]['options']) : $filters[$type]['options'];
504
    $params = array('%property' => $filters[$type]['title'] , '%value' =>
$options[$value]);
514
    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 {
554
      $form['filters']['current'][] = array('#markup' =>
t('<strong>%property</strong> is <strong>%value</strong>', $params));
56
    }
574
  }
58
598
  foreach ($filters as $key => $filter) {
608
    $names[$key] = $filter['title'];
618
    $form['filters']['status'][$key] = array(
628
      '#type' => 'select',
638
      '#options' => $filter['options'],
64
    );
658
  }
66
678
  $form['filters']['filter'] = array(
688
    '#type' => 'radios',
698
    '#options' => $names,
70
  );
718
  $form['filters']['buttons']['submit'] = array(
728
    '#type' => 'submit',
738
    '#value' => (count($session) ? t('Refine') : t('Filter')),
74
  );
758
  if (count($session)) {
764
    $form['filters']['buttons']['undo'] = array(
774
      '#type' => 'submit',
784
      '#value' => t('Undo'),
79
    );
804
    $form['filters']['buttons']['reset'] = array(
814
      '#type' => 'submit',
824
      '#value' => t('Reset'),
83
    );
844
  }
85
868
  drupal_add_js('misc/form.js', 'core');
87
888
  return $form;
890
}
90
91
/**
92
 * Process result from user administration filter form.
93
 */
94219
function user_filter_form_submit($form, &$form_state) {
951
  $op = $form_state['values']['op'];
961
  $filters = user_filters();
97
  switch ($op) {
981
    case t('Filter'): case t('Refine'):
991
      if (isset($form_state['values']['filter'])) {
1001
        $filter = $form_state['values']['filter'];
101
        // Merge an array of arrays into one if necessary.
1021
        $options = $filter == 'permission' ?
call_user_func_array('array_merge', $filters[$filter]['options']) :
$filters[$filter]['options'];
1031
        if (isset($options[$form_state['values'][$filter]])) {
1041
          $_SESSION['user_overview_filter'][] = array($filter,
$form_state['values'][$filter]);
1051
        }
1061
      }
1071
      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
1181
  $form_state['redirect'] = 'admin/user/user';
1191
  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
 */
129219
function user_admin_account() {
1307
  $filter = user_build_filter_query();
131
132
  $header = array(
1337
    array(),
1347
    array('data' => t('Username'), 'field' => 'u.name'),
1357
    array('data' => t('Status'), 'field' => 'u.status'),
1367
    t('Roles'),
1377
    array('data' => t('Member for'), 'field' => 'u.created', 'sort' =>
'desc'),
1387
    array('data' => t('Last access'), 'field' => 'u.access'),
1397
    t('Operations')
1407
  );
141
1427
  $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'];
1437
  $sql .= tablesort_sql($header);
1447
  $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'];
1457
  $result = pager_query($sql, 50, 0, $query_count, $filter['args']);
146
1477
  $form['options'] = array(
1487
    '#type' => 'fieldset',
1497
    '#title' => t('Update options'),
1507
    '#prefix' => '<div class="container-inline">',
1517
    '#suffix' => '</div>',
152
  );
1537
  $options = array();
1547
  foreach (module_invoke_all('user_operations') as $operation => $array) {
1557
    $options[$operation] = $array['label'];
1567
  }
1577
  $form['options']['operation'] = array(
1587
    '#type' => 'select',
1597
    '#options' => $options,
1607
    '#default_value' => 'unblock',
161
  );
1627
  $form['options']['submit'] = array(
1637
    '#type' => 'submit',
1647
    '#value' => t('Update'),
165
  );
166
1677
  $destination = drupal_get_destination();
168
1697
  $status = array(t('blocked'), t('active'));
1707
  $roles = user_roles(TRUE);
1717
  $accounts = array();
1727
  while ($account = db_fetch_object($result)) {
1737
    $accounts[$account->uid] = '';
1747
    $form['name'][$account->uid] = array('#markup' => theme('username',
$account));
1757
    $form['status'][$account->uid] =  array('#markup' =>
$status[$account->status]);
1767
    $users_roles = array();
1777
    $roles_result = db_query('SELECT rid FROM {users_roles} WHERE uid =
%d', $account->uid);
1787
    while ($user_role = db_fetch_object($roles_result)) {
1797
      $users_roles[] = $roles[$user_role->rid];
1807
    }
1817
    asort($users_roles);
1827
    $form['roles'][$account->uid][0] = array('#markup' =>
theme('item_list', $users_roles));
1837
    $form['member_for'][$account->uid] = array('#markup' =>
format_interval(REQUEST_TIME - $account->created));
1847
    $form['last_access'][$account->uid] =  array('#markup' =>
$account->access ? t('@time ago', array('@time' =>
format_interval(REQUEST_TIME - $account->access))) : t('never'));
1857
    $form['operations'][$account->uid] = array('#markup' => l(t('edit'),
"user/$account->uid/edit", array('query' => $destination)));
1867
  }
1877
  $form['accounts'] = array(
1887
    '#type' => 'checkboxes',
189
    '#options' => $accounts
1907
  );
1917
  $form['pager'] = array('#markup' => theme('pager', NULL, 50, 0));
192
1937
  return $form;
1940
}
195
196
/**
197
 * Submit the user administration update form.
198
 */
199219
function user_admin_account_submit($form, &$form_state) {
2001
  $operations = module_invoke_all('user_operations', $form_state);
2011
  $operation = $operations[$form_state['values']['operation']];
202
  // Filter out unchecked accounts.
2031
  $accounts = array_filter($form_state['values']['accounts']);
2041
  if ($function = $operation['callback']) {
205
    // Add in callback arguments if present.
2061
    if (isset($operation['callback arguments'])) {
2070
      $args = array_merge(array($accounts), $operation['callback
arguments']);
2080
    }
209
    else {
2101
      $args = array($accounts);
211
    }
2121
    call_user_func_array($function, $args);
213
2141
    drupal_set_message(t('The update has been performed.'));
2151
  }
2161
}
217
218219
function user_admin_account_validate($form, &$form_state) {
2191
  $form_state['values']['accounts'] =
array_filter($form_state['values']['accounts']);
2201
  if (count($form_state['values']['accounts']) == 0) {
2210
    form_set_error('', t('No users selected.'));
2220
  }
2231
}
224
225
/**
226
 * Form builder; Configure user settings for this site.
227
 *
228
 * @ingroup forms
229
 * @see system_settings_form()
230
 */
231219
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, FILE_CREATE_DIRECTORY,
'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
 */
495219
function user_admin_perm($form_state, $rid = NULL) {
496
497
  // Retrieve role names for columns.
49830
  $role_names = user_roles();
49930
  if (is_numeric($rid)) {
5000
    $role_names = array($rid => $role_names[$rid]);
5010
  }
502
  // Fetch permissions for all roles or the one selected role.
50330
  $role_permissions = user_role_permissions($role_names);
504
505
  // Store $role_names for use when saving the data.
50630
  $form['role_names'] = array(
50730
    '#type' => 'value',
50830
    '#value' => $role_names,
509
  );
510
  // Render role/permission overview:
51130
  $options = array();
51230
  $hide_descriptions = !system_admin_compact_mode();
51330
  foreach (module_list(FALSE, FALSE, TRUE) as $module) {
51430
    if ($permissions = module_invoke($module, 'perm')) {
51530
      $form['permission'][] = array(
51630
        '#markup' => $module,
517
        );
51830
      foreach ($permissions as $perm => $perm_item) {
51930
        $options[$perm] = '';
52030
        $form['permission'][$perm] = array(
52130
          '#type' => 'item',
52230
          '#markup' => $perm_item['title'],
52330
          '#description' => $hide_descriptions ? $perm_item['description']
: NULL,
524
        );
52530
        foreach ($role_names as $rid => $name) {
526
          // Builds arrays for checked boxes for each role
52730
          if (isset($role_permissions[$rid][$perm])) {
52830
            $status[$rid][] = $perm;
52930
          }
53030
        }
53130
      }
53230
    }
53330
  }
534
535
  // Have to build checkboxes here after checkbox arrays are built
53630
  foreach ($role_names as $rid => $name) {
53730
    $form['checkboxes'][$rid] = array('#type' => 'checkboxes', '#options'
=> $options, '#default_value' => isset($status[$rid]) ? $status[$rid] :
array());
53830
    $form['role_names'][$rid] = array('#markup' => $name, '#tree' => TRUE);
53930
  }
54030
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save
permissions'));
541
54230
  return $form;
5430
}
544
545
/**
546
 * Save permissions selected on the administer permissions page.
547
 *
548
 * @see user_admin_perm
549
 */
550219
function user_admin_perm_submit($form, &$form_state) {
55110
  foreach ($form_state['values']['role_names'] as $rid => $name) {
55210
    $checked = array_filter($form_state['values'][$rid]);
553
    // Delete existing permissions for the role. This handles "unchecking"
checkboxes.
55410
    db_query("DELETE FROM {role_permission} WHERE rid = %d", $rid);
55510
    foreach ($checked as $permission) {
55610
      db_query("INSERT INTO {role_permission} (rid, permission) VALUES (%d,
'%s')", $rid, $permission);
55710
    }
55810
  }
559
56010
  drupal_set_message(t('The changes have been saved.'));
561
562
  // Clear the cached pages and blocks.
56310
  cache_clear_all();
56410
}
565
566
/**
567
 * Theme the administer permissions page.
568
 *
569
 * @ingroup themeable
570
 */
571219
function theme_user_admin_perm($form) {
57220
  $roles = user_roles();
57320
  foreach (element_children($form['permission']) as $key) {
574
    // Don't take form control structures
57520
    if (is_array($form['permission'][$key])) {
57620
      $row = array();
577
      // Module name
57820
      if (is_numeric($key)) {
57920
        $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);
58020
      }
581
      else {
582
        // Permission row.
58320
        $row[] = array(
58420
          'data' => drupal_render($form['permission'][$key]),
58520
          'class' => 'permission',
586
        );
58720
        foreach (element_children($form['checkboxes']) as $rid) {
58820
          if (is_array($form['checkboxes'][$rid])) {
58920
            $row[] = array('data' =>
drupal_render($form['checkboxes'][$rid][$key]), 'class' => 'checkbox',
'title' => $roles[$rid] . ' : ' . t($key));
59020
          }
59120
        }
592
      }
59320
      $rows[] = $row;
59420
    }
59520
  }
59620
  $header[] = (t('Permission'));
59720
  foreach (element_children($form['role_names']) as $rid) {
59820
    if (is_array($form['role_names'][$rid])) {
59920
      $header[] = array('data' => drupal_render($form['role_names'][$rid]),
'class' => 'checkbox');
60020
    }
60120
  }
60220
  $output = theme('system_compact_link');
60320
  $output .= theme('table', $header, $rows, array('id' => 'permissions'));
60420
  $output .= drupal_render($form);
60520
  return $output;
6060
}
607
608
/**
609
 * Menu callback: administer roles.
610
 *
611
 * @ingroup forms
612
 * @see user_admin_role_validate()
613
 * @see user_admin_role_submit()
614
 * @see theme_user_admin_new_role()
615
 */
616219
function user_admin_role() {
6170
  $rid = arg(4);
6180
  if ($rid) {
6190
    if ($rid == DRUPAL_ANONYMOUS_RID || $rid == DRUPAL_AUTHENTICATED_RID) {
6200
      drupal_goto('admin/user/roles');
6210
    }
622
    // Display the edit role form.
6230
    $role = db_fetch_object(db_query('SELECT * FROM {role} WHERE rid = %d',
$rid));
6240
    $form['name'] = array(
6250
      '#type' => 'textfield',
6260
      '#title' => t('Role name'),
6270
      '#default_value' => $role->name,
6280
      '#size' => 30,
6290
      '#required' => TRUE,
6300
      '#maxlength' => 64,
6310
      '#description' => t('The name for this role. Example: "moderator",
"editorial board", "site architect".'),
632
    );
6330
    $form['rid'] = array(
6340
      '#type' => 'value',
6350
      '#value' => $rid,
636
    );
6370
    $form['submit'] = array(
6380
      '#type' => 'submit',
6390
      '#value' => t('Save role'),
640
    );
6410
    $form['delete'] = array(
6420
      '#type' => 'submit',
6430
      '#value' => t('Delete role'),
644
    );
6450
  }
646
  else {
6470
    $form['name'] = array(
6480
      '#type' => 'textfield',
6490
      '#size' => 32,
6500
      '#maxlength' => 64,
651
    );
6520
    $form['submit'] = array(
6530
      '#type' => 'submit',
6540
      '#value' => t('Add role'),
655
    );
6560
    $form['#submit'][] = 'user_admin_role_submit';
6570
    $form['#validate'][] = 'user_admin_role_validate';
658
  }
6590
  return $form;
6600
}
661
662219
function user_admin_role_validate($form, &$form_state) {
6630
  if ($form_state['values']['name']) {
6640
    if ($form_state['values']['op'] == t('Save role')) {
6650
      if (db_result(db_query("SELECT COUNT(*) FROM {role} WHERE name = '%s'
AND rid != %d", $form_state['values']['name'],
$form_state['values']['rid']))) {
6660
        form_set_error('name', t('The role name %name already exists.
Please choose another role name.', array('%name' =>
$form_state['values']['name'])));
6670
      }
6680
    }
6690
    elseif ($form_state['values']['op'] == t('Add role')) {
6700
      if (db_result(db_query("SELECT COUNT(*) FROM {role} WHERE name =
'%s'", $form_state['values']['name']))) {
6710
        form_set_error('name', t('The role name %name already exists.
Please choose another role name.', array('%name' =>
$form_state['values']['name'])));
6720
      }
6730
    }
6740
  }
675
  else {
6760
    form_set_error('name', t('You must specify a valid role name.'));
677
  }
6780
}
679
680219
function user_admin_role_submit($form, &$form_state) {
6810
  if ($form_state['values']['op'] == t('Save role')) {
6820
    db_query("UPDATE {role} SET name = '%s' WHERE rid = %d",
$form_state['values']['name'], $form_state['values']['rid']);
6830
    drupal_set_message(t('The role has been renamed.'));
6840
  }
6850
  elseif ($form_state['values']['op'] == t('Delete role')) {
6860
    db_query('DELETE FROM {role} WHERE rid = %d',
$form_state['values']['rid']);
6870
    db_query('DELETE FROM {role_permission} WHERE rid = %d',
$form_state['values']['rid']);
688
    // Update the users who have this role set:
6890
    db_query('DELETE FROM {users_roles} WHERE rid = %d',
$form_state['values']['rid']);
690
6910
    drupal_set_message(t('The role has been deleted.'));
6920
  }
6930
  elseif ($form_state['values']['op'] == t('Add role')) {
6940
    db_query("INSERT INTO {role} (name) VALUES ('%s')",
$form_state['values']['name']);
6950
    drupal_set_message(t('The role has been added.'));
6960
  }
6970
  $form_state['redirect'] = 'admin/user/roles';
6980
  return;
6990
}
700
701
/**
702
 * Theme user administration overview.
703
 *
704
 * @ingroup themeable
705
 */
706219
function theme_user_admin_account($form) {
707
  // Overview table:
708
  $header = array(
7096
    theme('table_select_header_cell'),
7106
    array('data' => t('Username'), 'field' => 'u.name'),
7116
    array('data' => t('Status'), 'field' => 'u.status'),
7126
    t('Roles'),
7136
    array('data' => t('Member for'), 'field' => 'u.created', 'sort' =>
'desc'),
7146
    array('data' => t('Last access'), 'field' => 'u.access'),
7156
    t('Operations')
7166
  );
717
7186
  $output = drupal_render($form['options']);
7196
  if (isset($form['name']) && is_array($form['name'])) {
7206
    foreach (element_children($form['name']) as $key) {
7216
      $rows[] = array(
7226
        drupal_render($form['accounts'][$key]),
7236
        drupal_render($form['name'][$key]),
7246
        drupal_render($form['status'][$key]),
7256
        drupal_render($form['roles'][$key]),
7266
        drupal_render($form['member_for'][$key]),
7276
        drupal_render($form['last_access'][$key]),
7286
        drupal_render($form['operations'][$key]),
729
      );
7306
    }
7316
  }
732
  else {
7330
    $rows[] = array(array('data' => t('No users available.'), 'colspan' =>
'7'));
734
  }
735
7366
  $output .= theme('table', $header, $rows);
7376
  if ($form['pager']['#markup']) {
7380
    $output .= drupal_render($form['pager']);
7390
  }
740
7416
  $output .= drupal_render($form);
742
7436
  return $output;
7440
}
745
746
/**
747
 * Theme the new-role form.
748
 *
749
 * @ingroup themeable
750
 */
751219
function theme_user_admin_new_role($form) {
7520
  $header = array(t('Name'), array('data' => t('Operations'), 'colspan' =>
2));
7530
  foreach (user_roles() as $rid => $name) {
7540
    $edit_permissions = l(t('edit permissions'), 'admin/user/permissions/'
. $rid);
7550
    if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID,
DRUPAL_AUTHENTICATED_RID))) {
7560
      $rows[] = array($name, l(t('edit role'), 'admin/user/roles/edit/' .
$rid), $edit_permissions);
7570
    }
758
    else {
7590
      $rows[] = array($name, t('locked'), $edit_permissions);
760
    }
7610
  }
7620
  $rows[] = array(drupal_render($form['name']), array('data' =>
drupal_render($form['submit']), 'colspan' => 2));
763
7640
  $output = drupal_render($form);
7650
  $output .= theme('table', $header, $rows);
766
7670
  return $output;
7680
}
769
770
/**
771
 * Theme user administration filter form.
772
 *
773
 * @ingroup themeable
774
 */
775219
function theme_user_filter_form($form) {
7767
  $output = '<div id="user-admin-filter">';
7777
  $output .= drupal_render($form['filters']);
7787
  $output .= '</div>';
7797
  $output .= drupal_render($form);
7807
  return $output;
7810
}
782
783
/**
784
 * Theme user administration filter selector.
785
 *
786
 * @ingroup themeable
787
 */
788219
function theme_user_filters($form) {
7897
  $output = '<ul class="clear-block">';
7907
  if (!empty($form['current'])) {
7914
    foreach (element_children($form['current']) as $key) {
7924
      $output .= '<li>' . drupal_render($form['current'][$key]) . '</li>';
7934
    }
7944
  }
795
7967
  $output .= '<li><dl class="multiselect">' . (!empty($form['current']) ?
'<dt><em>' . t('and') . '</em> ' . t('where') . '</dt>' : '') . '<dd
class="a">';
7977
  foreach (element_children($form['filter']) as $key) {
7987
    $output .= drupal_render($form['filter'][$key]);
7997
  }
8007
  $output .= '</dd>';
801
8027
  $output .= '<dt>' . t('is') . '</dt><dd class="b">';
803
8047
  foreach (element_children($form['status']) as $key) {
8057
    $output .= drupal_render($form['status'][$key]);
8067
  }
8077
  $output .= '</dd>';
808
8097
  $output .= '</dl>';
8107
  $output .= '<div class="container-inline" id="user-admin-buttons">' .
drupal_render($form['buttons']) . '</div>';
8117
  $output .= '</li></ul>';
812
8137
  return $output;
8140
}
815219