function 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 283

Code

function user_permissions_form() {
  $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");
  //fpm($modules);
  foreach ($modules as $module) {
    $form ["mark" . $m++] = array(
      "value" => "<tr><td colspan='$col_count' class='perm-module-name'>" . ucwords($module) . " " . t("module") . "</td></tr>",
    );

    // Draw the headers...
    $form ["mark" . $m++] = array(
      "value" => "<tr class='headers'><th style='min-width: 400px;'>" . t("Permission") . "</th>",
    );
    foreach ($roles as $key => $role) {
      $form ["mark" . $m++] = array(
        "value" => "<th style='min-width: 100px;'>" . $role . "</th>",
      );
    }
    $form ["mark" . $m++] = array(
      "value" => "</tr>",
    );
    // End of headers    

    $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"];

      $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>
                   </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");
        }

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

      }



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



    }

  }



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


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

  return $form;
}