You are here

function nodeaccess_node_access_records in Nodeaccess 7

Same name and namespace in other branches
  1. 8.2 nodeaccess.module \nodeaccess_node_access_records()
  2. 8 nodeaccess.module \nodeaccess_node_access_records()
  3. 5 nodeaccess.module \nodeaccess_node_access_records()
  4. 6.2 nodeaccess.module \nodeaccess_node_access_records()
  5. 6 nodeaccess.module \nodeaccess_node_access_records()

Implements hook_node_access_records().

Parameters

Object $node:

Return value

array|NULL

File

./nodeaccess.module, line 679
Provide per node access control

Code

function nodeaccess_node_access_records($node) {
  if (nodeaccess_disabling() || !$node->status) {
    return NULL;
  }

  // Need to find out if node has own grants or whether to use defaults.
  $default = variable_get('nodeaccess_' . $node->type, array());

  // Setup default keys that are required by node_access_write_grants().
  $grant_defaults = array(
    'gid' => 0,
    'realm' => 'nodeaccess_rid',
    'grant_view' => 0,
    'grant_update' => 0,
    'grant_delete' => 0,
    'priority' => variable_get('nodeaccess-priority', 0),
  );
  $query = db_select('nodeaccess', 'n')
    ->fields('n', array(
    'gid',
    'realm',
    'grant_view',
    'grant_update',
    'grant_delete',
  ))
    ->condition('nid', $node->nid, '=');
  $result = $query
    ->execute();
  if (!$result
    ->rowCount()) {

    // Node has no own grants, use defaults.
    $grants = $default;
  }
  else {

    // Node has own grants, use them.
    $grants = array();
    while ($row = $result
      ->fetchAssoc()) {
      $grants[] = $row;
    }
  }

  // Apply author grants.
  $author_prefs = variable_get('nodeaccess_authors', array());

  // Array is pre-populated with grant values.
  $grant = $author_prefs[$node->type];
  $grant['gid'] = $node->uid;
  $grant['realm'] = 'nodeaccess_author';

  // Include author grant even with all values FALSE, it may be
  // needed to overwrite an older value.
  $grants[] = $grant;
  foreach ($grants as $id => $grant) {

    // Merge missing default grant keys.
    $grants[$id] = $grants[$id] + $grant_defaults;
  }
  return $grants;
}