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

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

8
/**
9
 * Page callback: User administration page.
10 11 12
 */
function user_admin_account() {
  $header = array(
13
    'username' => array('data' => t('Username'), 'field' => 'u.name'),
14 15 16 17
    '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)),
18
    'operations' => t('Operations'),
19 20
  );

21
  $query = db_select('users', 'u');
22 23
  $query->condition('u.uid', 0, '<>');
  user_build_filter_query($query);
24

25
  $count_query = clone $query;
26
  $count_query->addExpression('COUNT(u.uid)');
27

28 29
  $query = $query
    ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
30
    ->extend('Drupal\Core\Database\Query\TableSortExtender');
31 32 33
  $query
    ->fields('u', array('uid', 'name', 'status', 'created', 'access'))
    ->limit(50)
34
    ->orderByHeader($header)
35 36
    ->setCountQuery($count_query);
  $result = $query->execute();
37 38 39

  $destination = drupal_get_destination();
  $status = array(t('blocked'), t('active'));
40
  $roles = array_map('check_plain', user_role_names(TRUE));
41
  $accounts = array();
42
  foreach ($result as $account) {
43
    $account = user_load($account->uid);
44
    $users_roles = array();
45 46
    $roles_result = db_query('SELECT rid FROM {users_roles} WHERE uid = :uid', array(':uid' => $account->uid));
    foreach ($roles_result as $user_role) {
47
      $users_roles[] = $roles[$user_role->rid];
48
    }
49
    asort($users_roles);
50

51
    $options[$account->uid] = array(
52
      'username' => theme('username', array('account' => $account)),
53
      'status' =>  $status[$account->status],
54
      'roles' => theme('item_list', array('items' => $users_roles)),
55 56
      'member_for' => format_interval(REQUEST_TIME - $account->created),
      'access' =>  $account->access ? t('@time ago', array('@time' => format_interval(REQUEST_TIME - $account->access))) : t('never'),
57 58 59
    );
    $links = array();
    $links['edit'] = array(
60
      'title' => t('Edit'),
61
      'href' => 'user/' . $account->uid . '/edit',
62 63
      'query' => $destination,
    );
64 65
    if (module_invoke('translation_entity', 'translate_access', $account)) {
      $links['translate'] = array(
66
        'title' => t('Translate'),
67 68 69 70
        'href' => 'user/' . $account->uid . '/translations',
        'query' => $destination,
      );
    }
71 72 73
    $options[$account->uid]['operations']['data'] = array(
      '#type' => 'operations',
      '#links' => $links,
74
    );
75 76 77

    $options[$account->uid]['title']['data']['#title'] = check_plain($account->name);

78
  }
79

80
  $form['accounts'] = array(
81
    '#theme' => 'table',
82
    '#header' => $header,
83
    '#rows' => $options,
84
    '#empty' => t('No people available.'),
85
  );
86
  $form['pager'] = array('#markup' => theme('pager'));
87 88 89 90 91 92

  return $form;
}

/**
 * Menu callback: administer permissions.
93
 *
94
 * @ingroup forms
95 96
 * @see user_admin_permissions_submit()
 * @see theme_user_admin_permissions()
97
 */
98
function user_admin_permissions($form, $form_state, $rid = NULL) {
99

100
  // Retrieve role names for columns.
101
  $role_names = user_role_names();
102
  if (isset($rid)) {
103
    $role_names = array($rid => $role_names[$rid]);
104
  }
105
  // Fetch permissions for all roles or the one selected role.
106
  $role_permissions = user_role_permissions(array_keys($role_names));
107

108 109 110 111 112
  // Store $role_names for use when saving the data.
  $form['role_names'] = array(
    '#type' => 'value',
    '#value' => $role_names,
  );
113 114
  // Render role/permission overview:
  $options = array();
115
  $module_info = system_get_info('module');
116
  $hide_descriptions = system_admin_compact_mode();
117 118 119 120

  // Get a list of all the modules implementing a hook_permission() and sort by
  // display name.
  $modules = array();
121
  foreach (module_implements('permission') as $module) {
122
    $modules[$module] = $module_info[$module]['name'];
123
  }
124
  asort($modules);
125

126
  foreach ($modules as $module => $display_name) {
127
    if ($permissions = module_invoke($module, 'permission')) {
128
      $form['permission'][] = array(
129
        '#markup' => $module_info[$module]['name'],
130
        '#id' => $module,
131
      );
132
      foreach ($permissions as $perm => $perm_item) {
133 134 135 136 137 138
        // 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.') : '',
        );
139
        $options[$perm] = '';
140 141
        $form['permission'][$perm] = array(
          '#type' => 'item',
142
          '#markup' => $perm_item['title'],
143
          '#description' => theme('user_permission_description', array('permission_item' => $perm_item, 'hide' => $hide_descriptions)),
144
        );
145 146
        foreach ($role_names as $rid => $name) {
          // Builds arrays for checked boxes for each role
147
          if (isset($role_permissions[$rid][$perm])) {
148 149 150 151 152 153 154 155 156
            $status[$rid][] = $perm;
          }
        }
      }
    }
  }

  // Have to build checkboxes here after checkbox arrays are built
  foreach ($role_names as $rid => $name) {
157 158 159 160 161 162
    $form['checkboxes'][$rid] = array(
      '#type' => 'checkboxes',
      '#options' => $options,
      '#default_value' => isset($status[$rid]) ? $status[$rid] : array(),
      '#attributes' => array('class' => array('rid-' . $rid)),
    );
163
    $form['role_names'][$rid] = array('#markup' => check_plain($name), '#tree' => TRUE);
164
  }
165

166
  $form['actions'] = array('#type' => 'actions');
167
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save permissions'));
168

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

171 172 173
  return $form;
}

174 175 176
/**
 * Save permissions selected on the administer permissions page.
 *
177
 * @see user_admin_permissions()
178
 */
179
function user_admin_permissions_submit($form, &$form_state) {
180
  foreach ($form_state['values']['role_names'] as $rid => $name) {
181
    user_role_change_permissions($rid, $form_state['values'][$rid]);
182 183 184 185
  }

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

186
  // Clear the cached pages and blocks.
187
  cache_invalidate_tags(array('content' => TRUE));
188 189 190
}

/**
191 192 193 194 195
 * Returns HTML for the administer permissions page.
 *
 * @param $variables
 *   An associative array containing:
 *   - form: A render element representing the form.
196 197 198
 *
 * @ingroup themeable
 */
199 200 201
function theme_user_admin_permissions($variables) {
  $form = $variables['form'];

202
  $roles = user_role_names();
203
  foreach (element_children($form['permission']) as $key) {
204 205 206
    $row = array();
    // Module name
    if (is_numeric($key)) {
207
      $row[] = array('data' => drupal_render($form['permission'][$key]), 'class' => array('module'), 'id' => 'module-' . $form['permission'][$key]['#id'], 'colspan' => count($form['role_names']['#value']) + 1);
208 209 210 211 212
    }
    else {
      // Permission row.
      $row[] = array(
        'data' => drupal_render($form['permission'][$key]),
213
        'class' => array('permission'),
214 215
      );
      foreach (element_children($form['checkboxes']) as $rid) {
216
        $form['checkboxes'][$rid][$key]['#title'] = $roles[$rid] . ': ' . $form['permission'][$key]['#markup'];
217 218
        $form['checkboxes'][$rid][$key]['#title_display'] = 'invisible';
        $row[] = array('data' => drupal_render($form['checkboxes'][$rid][$key]), 'class' => array('checkbox'));
219 220
      }
    }
221
    $rows[] = $row;
222 223 224
  }
  $header[] = (t('Permission'));
  foreach (element_children($form['role_names']) as $rid) {
225
    $header[] = array('data' => drupal_render($form['role_names'][$rid]), 'class' => array('checkbox'));
226
  }
227
  $output = theme('system_compact_link');
228
  $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'permissions')));
229
  $output .= drupal_render_children($form);
230 231 232
  return $output;
}

233
/**
234
 * Returns HTML for an individual permission description.
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
 *
 * @param $variables
 *   An associative array containing:
 *   - permission_item: An associative array representing the permission whose
 *     description is being themed. Useful keys include:
 *     - description: The text of the permission description.
 *     - warning: A security-related warning message about the permission (if
 *       there is one).
 *   - hide: A boolean indicating whether or not the permission description was
 *     requested to be hidden rather than shown.
 *
 * @ingroup themeable
 */
function theme_user_permission_description($variables) {
  if (!$variables['hide']) {
    $description = array();
    $permission_item = $variables['permission_item'];
    if (!empty($permission_item['description'])) {
      $description[] = $permission_item['description'];
    }
    if (!empty($permission_item['warning'])) {
      $description[] = '<em class="permission-warning">' . $permission_item['warning'] . '</em>';
    }
    if (!empty($description)) {
      return implode(' ', $description);
    }
  }
}