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

109
  // Retrieve role names for columns.
110
  $role_names = user_role_names();
111
  if (isset($rid)) {
112
    $role_names = array($rid => $role_names[$rid]);
113
  }
114
  // Fetch permissions for all roles or the one selected role.
115
  $role_permissions = user_role_permissions(array_keys($role_names));
116

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

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

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

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

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

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

185 186 187
  return $form;
}

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

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

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

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

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

258
/**
259
 * Returns HTML for an individual permission description.
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 287
 *
 * @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);
    }
  }
}