You are here

function user_admin_perm in Drupal 6

Same name and namespace in other branches
  1. 4 modules/user.module \user_admin_perm()
  2. 5 modules/user/user.module \user_admin_perm()

Menu callback: administer permissions.

See also

user_admin_perm_submit()

theme_user_admin_perm()

Related topics

1 string reference to 'user_admin_perm'
user_menu in modules/user/user.module
Implementation of hook_menu().

File

modules/user/user.admin.inc, line 518
Admin page callback file for the user module.

Code

function user_admin_perm($form_state, $rid = NULL) {
  if (is_numeric($rid)) {
    $result = db_query('SELECT r.rid, p.perm FROM {role} r LEFT JOIN {permission} p ON r.rid = p.rid WHERE r.rid = %d', $rid);
  }
  else {
    $result = db_query('SELECT r.rid, p.perm FROM {role} r LEFT JOIN {permission} p ON r.rid = p.rid ORDER BY name');
  }

  // Compile role array:
  // Add a comma at the end so when searching for a permission, we can
  // always search for "$perm," to make sure we do not confuse
  // permissions that are substrings of each other.
  while ($role = db_fetch_object($result)) {
    $role_permissions[$role->rid] = $role->perm . ',';
  }

  // Retrieve role names for columns.
  $role_names = user_roles();
  if (is_numeric($rid)) {
    $role_names = array(
      $rid => $role_names[$rid],
    );
  }

  // Render role/permission overview:
  $options = array();
  foreach (module_list(FALSE, FALSE, TRUE) as $module) {
    if ($permissions = module_invoke($module, 'perm')) {
      $form['permission'][] = array(
        '#value' => $module,
      );
      asort($permissions);
      foreach ($permissions as $perm) {
        $options[$perm] = '';
        $form['permission'][$perm] = array(
          '#value' => t($perm),
        );
        foreach ($role_names as $rid => $name) {

          // Builds arrays for checked boxes for each role
          if (strpos($role_permissions[$rid], $perm . ',') !== FALSE) {
            $status[$rid][] = $perm;
          }
        }
      }
    }
  }

  // Have to build checkboxes here after checkbox arrays are built
  foreach ($role_names as $rid => $name) {
    $form['checkboxes'][$rid] = array(
      '#type' => 'checkboxes',
      '#options' => $options,
      '#default_value' => isset($status[$rid]) ? $status[$rid] : array(),
    );
    $form['role_names'][$rid] = array(
      '#value' => $name,
      '#tree' => TRUE,
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save permissions'),
  );
  return $form;
}