You are here

function user_admin_perm in Drupal 4

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

Menu callback: administer permissions.

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

File

modules/user.module, line 1749
Enables the user registration and login system.

Code

function user_admin_perm($str_rids = NULL) {
  if (preg_match('/^([0-9]+[+ ])*[0-9]+$/', $str_rids)) {

    // The '+' character in a query string may be parsed as ' '.
    $rids = preg_split('/[+ ]/', $str_rids);
  }
  if ($rids) {
    $breadcrumbs = drupal_get_breadcrumb();
    $breadcrumbs[] = l(t('all roles'), 'admin/access');
    drupal_set_breadcrumb($breadcrumbs);
    $result = db_query('SELECT r.rid, p.perm FROM {role} r LEFT JOIN {permission} p ON r.rid = p.rid WHERE r.rid IN (%s) ORDER BY name', implode(', ', $rids));
  }
  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 . ',';
  }
  if ($rids) {
    $result = db_query('SELECT rid, name FROM {role} r WHERE r.rid IN (%s) ORDER BY name', implode(', ', $rids));
  }
  else {
    $result = db_query('SELECT rid, name FROM {role} ORDER BY name');
  }
  $role_names = array();
  while ($role = db_fetch_object($result)) {
    $role_names[$role->rid] = $role->name;
  }

  // Render role/permission overview:
  $options = array();
  foreach (module_list(FALSE, FALSE, TRUE) as $module) {
    if ($permissions = module_invoke($module, 'perm')) {
      $form['permission'][] = array(
        '#type' => 'markup',
        '#value' => t('%module module', array(
          '%module' => $module,
        )),
      );
      asort($permissions);
      foreach ($permissions as $perm) {
        $options[$perm] = '';
        $form['permission'][$perm] = array(
          '#type' => 'markup',
          '#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' => $status[$rid],
    );
    $form['role_names'][$rid] = array(
      '#type' => 'markup',
      '#value' => l($name, 'admin/access/' . $rid),
      '#tree' => TRUE,
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save permissions'),
  );
  return drupal_get_form('user_admin_perm', $form);
}