Download & Extend

SQL Error: INNER JOIN Clause getting added to wrong clause in compound queries

Project:Content Access
Version:6.x-1.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed (duplicate)

Issue Summary

First, this is not the double-distinct bug. What is happening is that content access's INNER JOIN clause is getting written to the wrong place in compound queries. In particular, it is getting attached to clauses that do not have a reference to the node table or in some cases, is getting written to the middle of the clause breaking the SQL altogether. In my case, the INNER JOIN clause got written in between my WHERE clause and my GROUP BY clause thus breaking my aggregate.

I'm curious how Content Access is writing this INNER JOIN clause. Is the module using core functions or searching for the first WHERE clause it finds and attaching itself to the end of it?

As a temporary solution, is there a way to override Content Access for a particular content type? I only have one content type where I am using custom queries for views and I'd like to just turn off all CA processing for that content type so I don't run into these issues.

Thanks.

(The clause is bigger than this but this shows that the CA clause has been written to the wrong location in the SQL statement)

SELECT COUNT( node.nid ) AS imagecount, MAX( node.changed ) AS node_changed, node_data_field_photogallery.field_photogallery_nid AS photogallery_nid
FROM node node
LEFT JOIN content_type_photo_image node_data_field_photogallery ON node.vid = node_data_field_photogallery.vid
WHERE node.type
IN (
'photo_image'
)
AND (
node.status <>0
)
INNER JOIN node_access na ON na.nid = node.nid
WHERE (
na.grant_view >=1
AND (
(
na.gid =0
AND na.realm = 'all'
)
OR (
na.gid =9
AND na.realm = 'content_access_author'
)
OR (
na.gid =2
AND na.realm = 'content_access_rid'
)
OR (
na.gid =8
AND na.realm = 'content_access_rid'
)
)
)
GROUP BY photogallery_nid

Comments

#1

Status:active» closed (works as designed)

That it's done by drupal itself, content access just tells it what to add there. So if there is a bug, it's in drupal or it's issue of those specific query.

#2

I just installed content_access in my site and now I'm experiencing this same problem. Given comment #1, it doesn't seem like there's anything that can be done to fix it within the content_access module. For now, I have to uninstall and continue to look for another access control solution. I'll be interested to hear if anyone has any ideas on how to fix this though.

#3

Status:closed (works as designed)» needs work

By design or not, this is a problem that has been posted on over and over and over by many people who cannot use Content Access because of this design. Content Access provides some excellent functionality but when the users need to override some of that functionality because CA or Drupal are not performing accurately it comes down to a question of whether it will be easier to have the change written into the Drupal core or into CA. If there was some way that CA not be globally applied to every content type but could be checked on or off for each content type, that would be a fairly straightforward to resolve this issue.

Alternately, my original question was asking what Drupal core call CA is using to inject this SQL, so I know how to address this question to the core.

Thanks.

#4

I have this Problem too, excecuting this query.
The INNER JOIN gets squeezed between my WHERE and Group
So sth. like this ....

SELECT COUNT( * )
FROM (

SELECT og_uid.nid, og_uid.uid, og_uid.created, n.title
FROM (
(

SELECT requestee_id uid
FROM user_relationships
WHERE requester_id =1
)
UNION (

SELECT requester_id uid
FROM user_relationships
WHERE requestee_id =1
)
)users
INNER JOIN og_uid og_uid ON users.uid = og_uid.uid
INNER JOIN node n ON og_uid.nid = n.nid
INNER JOIN og og ON og_uid.nid = og.nid
WHERE og_uid.is_active =1
AND n.status =1
AND og.og_directory =1
GROUP BY nid
)table_all

... becomes something like that ...

SELECT COUNT( * )
FROM (

SELECT og_uid.nid, og_uid.uid, og_uid.created, n.title
FROM (
(

SELECT requestee_id uid
FROM user_relationships
WHERE requester_id =1
)
UNION (

SELECT requester_id uid
FROM user_relationships
WHERE requestee_id =1
)
)users
INNER JOIN og_uid og_uid ON users.uid = og_uid.uid
INNER JOIN node n ON og_uid.nid = n.nid
INNER JOIN og og ON og_uid.nid = og.nid
WHERE og_uid.is_active =1
AND n.status =1
AND og.og_directory =1
INNER JOIN node_access na ON na.nid = og.nid
WHERE (
na.grant_view >=1
AND (
(
na.gid =0
AND na.realm = 'all'
)
OR (
na.gid =6
AND na.realm = 'user_relationship_node_access_author'
)
OR (
na.gid =0
AND na.realm = 'og_public'
)
OR (
na.gid =1
AND na.realm = 'og_subscriber'
)
OR (
na.gid =7
AND na.realm = 'og_subscriber'
)
OR (
na.gid =11
AND na.realm = 'og_subscriber'
)
OR (
na.gid =10
AND na.realm = 'og_admin'
)
)
GROUP BY og.nid
)table_all

#5

Try using

<code>

   INDENTED STUFF
</code>

to avoid losing your indenting, and condense it vertically, so it doesn't stretch over three screen heights.

#6

I am having a similiar problem and solved it changing node.module:

function node_db_rewrite_sql($query, $primary_table, $primary_field) {
if ($primary_field == 'nid' && !node_access_view_all_nodes()) {
   $return['join'] = _node_access_join_sql($primary_table);
   $return['where'] = _node_access_where_sql();
   $return['distinct'] = 1;
   return $return;
}
}

changed to:

function node_db_rewrite_sql($query, $primary_table, $primary_field) {
if ($primary_field == 'nid' && !node_access_view_all_nodes()) {
   if (($where=_node_access_where_sql('view','new_na'))!='') {
      $return['where'] = "".$primary_table.".".$primary_field." IN
(SELECT new_na.nid FROM {node_access} new_na WHERE ".$where.")";
      return $return;
   }
}
}

Right now my site works (using views and group by and other sql modules).

Does this solve this problem?

#7

Version:6.x-1.2» 6.x-1.x-dev

NO NEED TO HACK THE CORE.

Related #239139: Do not hijack the 'all' realm

#8

Status:needs work» needs review

That fix is committed to both 6.x and 7.x, please test it and tell me if it works for you.

#9

Status:needs review» closed (duplicate)

and let's move to #239139: Do not hijack the 'all' realm.