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

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

85
  }
86

87
  $form['accounts'] = array(
88
    '#theme' => 'table',
89
    '#header' => $header,
90
    '#rows' => $options,
91
    '#empty' => t('No people available.'),
92
  );
93 94 95
  $form['pager'] = array(
    '#theme' =>'pager',
  );
96 97 98 99 100 101

  return $form;
}

/**
 * Menu callback: administer permissions.
102
 *
103
 * @ingroup forms
104 105
 * @see user_admin_permissions_submit()
 * @see theme_user_admin_permissions()
106
 */
107
function user_admin_permissions($form, $form_state, $rid = NULL) {
108
  // Retrieve role names for columns.
109
  $role_names = user_role_names();
110
  if (isset($rid)) {
111
    $role_names = array($rid => $role_names[$rid]);
112
  }
113
  // Fetch permissions for all roles or the one selected role.
114
  $role_permissions = user_role_permissions(array_keys($role_names));
115

116 117 118 119 120
  // Store $role_names for use when saving the data.
  $form['role_names'] = array(
    '#type' => 'value',
    '#value' => $role_names,
  );
121 122
  // Render role/permission overview:
  $options = array();
123
  $module_info = system_get_info('module');
124
  $hide_descriptions = system_admin_compact_mode();
125 126 127 128

  // Get a list of all the modules implementing a hook_permission() and sort by
  // display name.
  $modules = array();
129
  foreach (module_implements('permission') as $module) {
130
    $modules[$module] = $module_info[$module]['name'];
131
  }
132
  asort($modules);
133

134
  foreach ($modules as $module => $display_name) {
135
    if ($permissions = module_invoke($module, 'permission')) {
136
      $form['permission'][] = array(
137
        '#markup' => $module_info[$module]['name'],
138
        '#id' => $module,
139
      );
140
      foreach ($permissions as $perm => $perm_item) {
141 142 143 144 145 146
        // 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.') : '',
        );
147
        $options[$perm] = '';
148 149 150 151 152
        $user_permission_description = array(
          '#theme' => 'user_permission_description',
          '#permission_item' => $perm_item,
          '#hide' => $hide_descriptions,
        );
153 154
        $form['permission'][$perm] = array(
          '#type' => 'item',
155
          '#markup' => $perm_item['title'],
156
          '#description' => drupal_render($user_permission_description),
157
        );
158 159
        foreach ($role_names as $rid => $name) {
          // Builds arrays for checked boxes for each role
160
          if (in_array($perm, $role_permissions[$rid])) {
161 162 163 164 165 166 167 168 169
            $status[$rid][] = $perm;
          }
        }
      }
    }
  }

  // Have to build checkboxes here after checkbox arrays are built
  foreach ($role_names as $rid => $name) {
170 171 172 173 174 175
    $form['checkboxes'][$rid] = array(
      '#type' => 'checkboxes',
      '#options' => $options,
      '#default_value' => isset($status[$rid]) ? $status[$rid] : array(),
      '#attributes' => array('class' => array('rid-' . $rid)),
    );
176
    $form['role_names'][$rid] = array('#markup' => check_plain($name), '#tree' => TRUE);
177
  }
178

179
  $form['actions'] = array('#type' => 'actions');
180
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save permissions'));
181

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

184 185 186
  return $form;
}

187 188 189
/**
 * Save permissions selected on the administer permissions page.
 *
190
 * @see user_admin_permissions()
191
 */
192
function user_admin_permissions_submit($form, &$form_state) {
193
  foreach ($form_state['values']['role_names'] as $rid => $name) {
194
    user_role_change_permissions($rid, $form_state['values'][$rid]);
195 196 197 198
  }

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

199
  // Clear the cached pages and blocks.
200
  cache_invalidate_tags(array('content' => TRUE));
201 202 203
}

/**
204 205 206 207 208
 * Returns HTML for the administer permissions page.
 *
 * @param $variables
 *   An associative array containing:
 *   - form: A render element representing the form.
209 210 211
 *
 * @ingroup themeable
 */
212 213 214
function theme_user_admin_permissions($variables) {
  $form = $variables['form'];

215
  $roles = user_role_names();
216
  foreach (element_children($form['permission']) as $key) {
217 218 219
    $row = array();
    // Module name
    if (is_numeric($key)) {
220
      $row[] = array('data' => drupal_render($form['permission'][$key]), 'class' => array('module'), 'id' => 'module-' . $form['permission'][$key]['#id'], 'colspan' => count($form['role_names']['#value']) + 1);
221 222 223 224 225
    }
    else {
      // Permission row.
      $row[] = array(
        'data' => drupal_render($form['permission'][$key]),
226
        'class' => array('permission'),
227 228
      );
      foreach (element_children($form['checkboxes']) as $rid) {
229
        $form['checkboxes'][$rid][$key]['#title'] = $roles[$rid] . ': ' . $form['permission'][$key]['#markup'];
230 231
        $form['checkboxes'][$rid][$key]['#title_display'] = 'invisible';
        $row[] = array('data' => drupal_render($form['checkboxes'][$rid][$key]), 'class' => array('checkbox'));
232 233
      }
    }
234
    $rows[] = $row;
235 236 237
  }
  $header[] = (t('Permission'));
  foreach (element_children($form['role_names']) as $rid) {
238
    $header[] = array('data' => drupal_render($form['role_names'][$rid]), 'class' => array('checkbox'));
239
  }
240 241 242 243 244 245 246 247 248 249 250 251 252
  $system_compact_link = array(
    '#theme' => 'system_compact_link',
  );
  $table = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#attributes' => array(
      'id' => 'permissions',
    ),
  );
  $output = drupal_render($system_compact_link);
  $output .= drupal_render($table);
253
  $output .= drupal_render_children($form);
254 255 256
  return $output;
}

257
/**
258
 * Returns HTML for an individual permission description.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
 *
 * @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);
    }
  }
}