Index: modules/node/node.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/node/node.module,v
retrieving revision 1.1225
diff -u -r1.1225 node.module
--- modules/node/node.module	30 Jan 2010 02:52:08 -0000	1.1225
+++ modules/node/node.module	4 Feb 2010 21:55:31 -0000
@@ -2918,25 +2918,33 @@
     }
     // Skip the extra joins and conditions for node admins.
     if (!user_access('bypass node access')) {
-      // The node_access table has the access grants for any given node.
-      $access_alias = $query->join('node_access', 'na', 'na.nid = n.nid');
-      $or = db_or();
-      // If any grant exists for the specified user, then user has access to the
-      // node for the specified operation.
-      foreach (node_access_grants($op, $query->getMetaData('account')) as $realm => $gids) {
-        foreach ($gids as $gid) {
-          $or->condition(db_and()
-            ->condition("{$access_alias}.gid", $gid)
-            ->condition("{$access_alias}.realm", $realm)
-          );
-        }
-      }
+      // Find all instances of the {node} table being joined.
+      $tables = $query->getTables();
+      $grants = node_access_grants($op, $query->getMetaData('account'));
+      foreach ($tables as $nalias => $tableinfo) {
+        $table = $tableinfo['table'];
+        if (!$table instanceof SelectQueryInterface && $table == 'node') {
+          // The node_access table has the access grants for any given node.
+          $access_alias = $query->join('node_access', 'na', "na.nid = {$nalias}.nid");
+          $or = db_or();
+          // If any grant exists for the specified user, then user has access
+          // to the node for the specified operation.
+          foreach ($grants as $realm => $gids) {
+            foreach ($gids as $gid) {
+              $or->condition(db_and()
+                ->condition("{$access_alias}.gid", $gid)
+                ->condition("{$access_alias}.realm", $realm)
+              );
+            }
+          }
+
+          if (count($or->conditions())) {
+            $query->condition($or);
+          }
 
-      if (count($or->conditions())) {
-        $query->condition($or);
+          $query->condition("{$access_alias}.grant_$op", 1, '>=');
+        }
       }
-
-      $query->condition("{$access_alias}.grant_$op", 1, '>=');
     }
   }
 }
