user.admin.inc 9.36 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
  $query->condition('u.uid', 0, '<>');
23

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

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

  $destination = drupal_get_destination();
  $status = array(t('blocked'), t('active'));
39
  $roles = array_map('check_plain', user_role_names(TRUE));
40
  $accounts = array();
41
  foreach ($result as $account) {
42
    $account = user_load($account->uid);
43
    $users_roles = array();
44 45
    $roles_result = db_query('SELECT rid FROM {users_roles} WHERE uid = :uid', array(':uid' => $account->uid));
    foreach ($roles_result as $user_role) {
46
      $users_roles[] = $roles[$user_role->rid];
47
    }
48
    asort($users_roles);
49 50 51 52 53 54 55 56
    $username = array(
      '#theme' => 'username',
      '#account' => $account,
    );
    $item_list = array(
      '#theme' => 'item_list',
      '#items' => $users_roles,
    );
57
    $options[$account->uid] = array(
58 59 60
      'username' => drupal_render($username),
      'status' => $status[$account->status],
      'roles' => drupal_render($item_list),
61
      'member_for' => format_interval(REQUEST_TIME - $account->created),
62
      'access' => $account->access ? t('@time ago', array('@time' => format_interval(REQUEST_TIME - $account->access))) : t('never'),
63 64 65
    );
    $links = array();
    $links['edit'] = array(
66
      'title' => t('Edit'),
67
      'href' => 'user/' . $account->uid . '/edit',
68 69
      'query' => $destination,
    );
70
    if (module_invoke('content_translation', 'translate_access', $account)) {
71
      $links['translate'] = array(
72
        'title' => t('Translate'),
73 74 75 76
        'href' => 'user/' . $account->uid . '/translations',
        'query' => $destination,
      );
    }
77 78 79
    $options[$account->uid]['operations']['data'] = array(
      '#type' => 'operations',
      '#links' => $links,
80
    );
81 82 83

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

84
  }
85

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

  return $form;
}

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

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

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

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

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

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

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

183 184 185
  return $form;
}

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

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

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

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

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

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