You are here

function _module_grants_node_access_where_sql in Module Grants 6.4

Same name and namespace in other branches
  1. 6.3 module_grants.module \_module_grants_node_access_where_sql()

Generate an SQL where clause for use in fetching a node listing.

Similar to _node_access_where_sql() in node.module but ANDs rather than ORs grants together on a per module base to create a more natural behaviour.

Parameters

$node_op: The operation that must be allowed to return a node.

$node_access_alias: If the node_access table has been given an SQL alias other than the default "na", that must be passed here.

$account: The user object for the user performing the operation. If omitted, the current user is used.

Return value

An SQL where clause.

1 call to _module_grants_node_access_where_sql()
module_grants_db_rewrite_sql in ./module_grants.module
Implementation of hook_db_rewrite_sql().

File

./module_grants.module, line 429
Module to apply access grants to pre-published content just as they are to published content and to make multiple content access modules work together in the expected way.

Code

function _module_grants_node_access_where_sql($node_op = 'view', $node_access_alias = 'na', $account = NULL) {
  global $user;
  if (user_access('administer nodes')) {
    return;
  }
  if (empty($account)) {
    $account = $user;
  }
  $all_grants = _module_grants_by_module($node_op, $account);
  $grants = array();
  foreach ($all_grants as $module => $module_grants) {
    $lenient_subquery = '';
    if (variable_get('module_grants_lenient', TRUE)) {
      $module_realms = array_keys(module_invoke($module, 'node_grants', $account, $node_op));
      if (!empty($module_realms)) {
        $lenient_subquery = "(SELECT COUNT(1) FROM {node_access} nasq WHERE {$node_access_alias}.nid=nasq.nid AND realm IN ('" . implode("','", $module_realms) . "')) = 0 OR ";
      }
    }
    $grants[] = '(' . $lenient_subquery . "(SELECT COUNT(1) FROM {node_access} nasq WHERE {$node_access_alias}.nid=nasq.nid AND ({$module_grants})) > 0)";
  }

  //return = count($grants) ? implode(' AND ', $grants) : '';

  //[#601064], comment #13
  $base_sql = "((SELECT COUNT(1) FROM {node_access} nasq WHERE {$node_access_alias}.nid=nasq.nid AND gid=0 AND realm='all') > 0)";
  $sql = $base_sql . (count($grants) ? ' OR ' . implode(' AND ', $grants) : '');
  return $sql;
}