You are here

public static function BookAccess::getRoleGrants in Book access 7.2

Same name and namespace in other branches
  1. 6.2 book_access.module \BookAccess::getRoleGrants()

Returns the role book grants.

Parameters

$bid: The book ID.

$roles: The variables where to store the value returned by user_roles().

$defaults: An array containing the default values for the grants.

Return value

An array of grants, in the format

$grants[$grant][$rid];

, where

$grant;

is a string between 'grant_view', 'grant_update', 'grant_delete', 'grant_admin_access', 'grant_add_child', 'grant_edit_outline', and

$rid;

is the role ID.

2 calls to BookAccess::getRoleGrants()
BookAccessTestCase::caseRoleDefaultsPropagateToNewNode in ./book_access_test_case.test
book_access_ui_grants_form in ./book_access_ui.admin.inc
Form builder for the book access configuration page.

File

./book_access.module, line 468
Allows to set the access control for book nodes on a per book basis. Based on forum_access.module and tac_lite.module.

Class

BookAccess
@file

Code

public static function getRoleGrants($bid, &$roles, array $defaults = array()) {
  $grants = array(
    'grant_view' => array(),
    'grant_update' => array(),
    'grant_delete' => array(),
    'grant_admin_access' => array(),
    'grant_add_child' => array(),
    'grant_edit_outline' => array(),
  );
  $roles = user_roles();
  $rcopy = $roles;
  $rids = array_keys($roles);
  $result = db_query("SELECT * FROM {book_access_role} WHERE nid = :nid AND rid IN (:rid)", array(
    ':nid' => $bid,
    ':rid' => $rids,
  ));

  // Build the role access permissions for the book.
  if ($result
    ->rowCount() > 0) {
    foreach ($result as $access) {
      unset($rcopy[$access->rid]);
      foreach (self::grantIDs() as $id) {
        $grants[$id][$access->rid] = !empty($access->{$id});
      }
    }
  }
  else {
    foreach ($rids as $rid) {
      $roleDefaults = variable_get("book_access_default_role_{$rid}_access");
      if ($roleDefaults) {
        unset($rcopy[$rid]);
        foreach (self::grantIDs() as $id) {
          $grants[$id][$rid] = in_array($id, $roleDefaults);
        }
      }
    }
  }
  $defaults = array_filter($defaults);

  // Set the default role access permissions for the roles that don't have
  // access permissions already set.
  foreach (self::grantIDs() as $id) {
    foreach ($rcopy as $rid => $name) {
      $grants[$id][$rid] = !empty($defaults[$id]);
    }
  }
  return $grants;
}