2

I am in middle of a problem where i have to check node access of current user against an array of node ids.

I have a two content types (A and B) and one contains the reference of other based on a taxonomy. A contains a user role reference fields to allow access to certain roles only. Now i want to show related contents from A on detail page of B. I have used a custom query to fetch all nodes from A somehow related to B. So code is looks like:

$related_nodes = <function_to_get_related_node>;
$result = node_load_multiple($related_nodes);

I have restricted couple of node from A content type to display only certain user roles but $result shows all node from A. Node access not worked.

In my function i have used db_select() with addTag('node_access') but it doesn't seems to be working.

So Basically i want some sort of code to check node_access on array of $related_nodes and filter before it passed to node_load_multiple function. Could someone help me find a way? Thanks!

2 Answers 2

0

How about adding checking if the node has reference of current user while fetching related nodes.

So,

$related_nodes = <get_related_node>;  

  // Might look Dumb
  function get_related_node($user=NULL) {
    if(is_null($user)) {
      global $user;
    }
    db_select('node','n')
     //Your existing joins
     // One more join for the user reference field, :O
     ->leftJoin('field_data_userreference','ur','ur.entity_id=n.nid')
     // your existing conditions for query.
     // One more condition for the current user as the value on the field.
     ->condition('ur.entity_id',$user->uid,'=')
    // And the execution continues.
  }

Fields mahcine names to be fixed as per the names provided. It should filter your reference nodes while fetching.

2
  • But i have a user role references field not a user. So i need to show only node which is allowed to view by current user role or anonymous if no role is selected.
    – Anurag
    Commented Jun 5, 2015 at 11:49
  • in that case, instead of user id you would be intersted in comparing role ids of current user in query
    – arpitr
    Commented Jun 5, 2015 at 15:19
0

This is untested, but assuming the reference field table (let's call it "field_data_field_roles") is structured something like this:

entity_type | entity_id | field_roles_rid (role id)

...then this should return a new array of node ids filtered by the conditions described in your post:

global $user;

$rids = array_keys($user->roles); // array of role ids belonging to current user
$nids = array('1', '2', '3'); // array of node ids

$query = db_select('node','n');

// Set up the join, targeting the table containing the reference field data.
$query->join('field_data_field_roles', 'r', 'n.nid = r.entity_id');

// Declare your conditions. I threw in the 'node_access' tag, not knowing
// whether the check was applied when you retrieved the original set of nids.
$query
  ->fields('n', array('nid'))
  ->condition('n.nid', $nids, 'IN')
  ->condition('r.field_roles_rid', $rids, 'IN')
  ->addTag('node_access');

// Fetch matching nids.
$result = $query->execute()->fetchField();

Cheers -

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.