function user_permissions_form

7.x user.module user_permissions_form()
6.x user.module user_permissions_form()
4.x user.module user_permissions_form()
5.x user.module user_permissions_form()

This is the permissions form, where users can set which roles have which permissions.

File

modules/user/user.module, line 1344

Code

function user_permissions_form() {
  global $user;
  $form = array();

  fp_add_css(fp_get_module_path("user") . "/css/user.css");

  // Let's get all of our permissions from the permissions table.
  $permissions = array();
  $res = db_query("SELECT * FROM role_permissions");
  while ($cur = db_fetch_array($res)) {
    $permissions [$cur ["rid"]][] = $cur ["perm"];
  }

  $cb = $m = 1;

  $form ["mark" . $m++] = array(

    "value" => t("Use this form to assign permissions to roles in the system.
                  These permissions are defined by module."),
  );

  $roles = array();
  $res = db_query("SELECT * FROM roles ORDER BY rid ");
  while ($cur = db_fetch_array($res)) {
    $roles [$cur ["rid"]] = $cur ["name"];
  }



  $col_count = count($roles) + 1;

  $form ["mark" . $m++] = array(
    "value" => "<table class='admin-perm-table' width='100%' cellpadding='0' cellspacing='0'>
                ",
  );



  // Go through all of the modules, then call hook_perm for them, to get their permissions.
  $modules = modules_implement_hook("perm");
  $mcount = 0;
  foreach ($modules as $module) {

    $disp_module = $module;
    $disp_module = ucwords(str_replace("_", " ", $disp_module));

    $form ["mark" . $m++] = array(
      "value" => "<tr><td colspan='$col_count' class='perm-module-name'>" . $disp_module . " " . t("module") . "</td></tr>",
    );


    if ($mcount == 0) {

      // Draw the headers...
      $form ["mark" . $m++] = array(
        "value" => "<tr class='headers'><th class='permission-name-and-desc'>" . t("Permission") . "</th>",
      );
      foreach ($roles as $key => $role) {
        $form ["mark" . $m++] = array(
          "value" => "<th class='role-" . fp_get_machine_readable($role) . "')>" . $role . "</th>",
        );
      }
      $form ["mark" . $m++] = array(
        "value" => "</tr>",
      );
      // End of headers    
    }
    $mcount++;
    $zebra = "even";

    // Let's get all the perms.    
    $perms = call_user_func($module . "_perm");
    foreach ($perms as $perm_name => $perm_details) {
      $title = @$perm_details ["title"];
      $desc = @$perm_details ["description"];
      $extra_desc = "";
      $attributes = array();

      if (isset($perm_details ['admin_restricted']) && intval($perm_details ['admin_restricted']) === 1 && intval($user->id) !== 1) {
        // This permission is restricted to admin only.  Meaning only user_id 1 is allowed to grant it.
        $extra_desc = "<div class='restricted-permissions'>" . t("Sorry, you cannot access this permission.") . "</div>";
        $attributes ['disabled'] = 'disabled';
      }

      $zebra = ($zebra == "even") ? "odd" : "even";

      $form ["mark" . $m++] = array(
        "value" => "<tr class='perm-cb-row perm-cb-row-$zebra'><td class='perm-details' valign='top'>
                      <div class='perm-title' title='$perm_name'>$title</div>                      
                      <div class='perm-desc' title='$perm_name'>$desc</div>
                      $extra_desc
                   </td>",
      );


      foreach ($roles as $key => $role) {

        // Should this be checked by default (cause it was all ready in our table?)
        $default_value = array();
        if (@is_array($permissions [$key]) && in_array($perm_name, $permissions [$key])) {
          // Yes, it was in there!  Set up the default_value as an array that
          // looks like array(key => key).  That is how we set a checkbox to be checked
          // by default.
          $default_value = array($key . "___$perm_name" => $key . "___$perm_name");
        }

        $element_name = "perm_cb_" . $cb++;

        if (isset($attributes ['disabled'])) {
          $form [$element_name] = array(
            'type' => 'value',
            'value' => current($default_value),
          );
          $element_name = "markperm_disabledcb_" . $cb++;
        }


        $form [$element_name] = array(
          "type" => "checkboxes",
          "options" => array("$key" . "___$perm_name" => ""),
          "value" => $default_value,
          "prefix" => "<td class='perm-cb'>",
          "suffix" => "</td>",
          "attributes" => $attributes,
        );



      } // foreach roles



      $form ["mark" . $m++] = array(
        "value" => "</tr>",
      );



    } // foreach perms


  } // foreach modules



  $form ["mark" . $m++] = array(
    "value" => "</table>",
  );


  $form ["submit"] = array(
    "type" => "submit",
    "value" => t("Save permissions"),
    "spinner" => TRUE,
    "prefix" => "<hr>",
  );

  return $form;
}