From d7aac0c1b12e43e139433f68bb39a0a152ce9d7b Mon Sep 17 00:00:00 2001
From: Bradley M. Froehle <bfroehle@math.berkeley.edu>
Date: Mon, 15 Nov 2010 12:13:09 -0800
Subject: [PATCH] 963656: WIP -- add  to node_access_view_all_nodes

---
 modules/node/node.module |   73 +++++++++++++++++++++++++++------------------
 1 files changed, 44 insertions(+), 29 deletions(-)

diff --git modules/node/node.module modules/node/node.module
index 025be15..07ed123 100644
--- modules/node/node.module
+++ modules/node/node.module
@@ -3030,43 +3030,58 @@ function node_access_grants($op, $account = NULL) {
  * }
  * @endcode
  *
+ * @param $account
+ *   The user object for the user performing the operation. If omitted, the
+ *   current user is used.
  * @return
  *   TRUE if 'view' access to all nodes is granted, FALSE otherwise.
  *
  * @see hook_node_grants()
  * @see _node_query_node_access_alter()
  */
-function node_access_view_all_nodes() {
-  $access = &drupal_static(__FUNCTION__);
+function node_access_view_all_nodes($account = NULL) {
+  global $user;
+  if (!$account) {
+    $account = $user;
+  }
 
-  if (!isset($access)) {
-    // If no modules implement the node access system, access is always TRUE.
-    if (!module_implements('node_grants')) {
-      $access = TRUE;
-    }
-    else {
-      $query = db_select('node_access');
-      $query->addExpression('COUNT(*)');
-      $query
-        ->condition('nid', 0)
-        ->condition('grant_view', 1, '>=');
+  // Statically cache result for $user, but recalculate for other accounts.
+  $user_access = &drupal_static(__FUNCTION__);
+  if ($account->uid == $user->uid && isset($user_access)) {
+    return $user_access;
+  }
 
-      $grants = db_or();
-      foreach (node_access_grants('view') as $realm => $gids) {
-        foreach ($gids as $gid) {
-          $grants->condition(db_and()
-            ->condition('gid', $gid)
-            ->condition('realm', $realm)
-          );
-        }
-      }
-      if (count($grants) > 0 ) {
-        $query->condition($grants);
-      }
-      $access = $query
-        ->execute()
-        ->fetchField();
+  // If no modules implement the node access system, access is always TRUE.
+  if (!module_implements('node_grants')) {
+    $access = TRUE;
+  }
+  else {
+    $query = db_select('node_access');
+    $query->addExpression('COUNT(*)');
+    $query
+      ->condition('nid', 0)
+      ->condition('grant_view', 1, '>=');
+
+    $grants = db_or();
+    foreach (node_access_grants('view', $account) as $realm => $gids) {
+      foreach ($gids as $gid) {
+        $grants->condition(db_and()
+          ->condition('gid', $gid)
+          ->condition('realm', $realm)
+        );
       }
+    }
+    if (count($grants) > 0 ) {
+      $query->condition($grants);
+    }
+    $access = $query
+      ->execute()
+      ->fetchField();
+  }
+
+  // Cache the result if we are considering the global $user account.
+  if ($account->uid == $user->uid) {
+    $user_access = $access;
   }
 
   return $access;
@@ -3129,7 +3144,7 @@ function _node_query_node_access_alter($query, $base_table, $type) {
     return;
   }
   // If viewing nodes, make sure access rules should be enforced.
-  if ($op == 'view' && node_access_view_all_nodes()) {
+  if ($op == 'view' && node_access_view_all_nodes($account)) {
     return;
   }
 
-- 
1.7.3.1

