1

I am trying to build a list of node ids that have view access for a specific role. Is this at all possible? I currently have an array of all 'page' content type nodes "pages".

I now need to loop through these and see if they have a specific role enabled, if so get the id and display the link on a page if the user has access to that page.

I know I can do that using the following example. But this just checks if the user has view access, and the user could have access from another role they have which my list would not be accurate for what I am trying to achieve.

if(node_access('view', $nodeid, $user)){
    // do something
}

Or is there a way to check if a specific users role has view access rather than just access in general.

The reason I am trying to do this, is I am trying to get the list of specific pages dynamically if a role is given to that node "page".

Sorry if my terminology is off I am not super familiar with drupal.

  • "node ids that have view access for a specific role. "? What do you mean? It's roles that have privilages to content types, and to nodes of that types. – Mołot Jul 16 '15 at 7:22
  • Exactly, I probably worded it wrong, or you read it wrong sorry. I came up with a solution. But yea I wanted to show all nodes where a user with a specific role had view access. – Bowenac Jul 16 '15 at 12:20
  • Could you reword it, then? And you know, self-answers are allowed, even encouraged. – Mołot Jul 16 '15 at 12:21
  • Yea I will post what I used as an answer in a min, just woke up. – Bowenac Jul 16 '15 at 12:23
1

This is what I ended up using, it could probably be optimized a bit but it works.

<?php
if ($node = menu_get_object()) {
  // Get the nid
  $currentNode = $node->nid;
}
if (in_array("Gold Member", $user->roles)) {
    echo '<div id="title"><h1 class="title display">Free Courses for Gold Members</h1></div>';

    echo '<ul class="course_page_ul">';
        $role_type = 'Gold Member';// Texas Member
        $getRole = db_query("SELECT rid FROM role WHERE name = '%s'",$role_type);
        while ($roleid = db_fetch_object ($getRole)) {
          $roleids[] = $roleid;
        }   
        //Get the ID of the role type we are using from the array
        foreach($roleids as $roleId){
            $role_id = $roleId->rid;
        }

        $node_type = "page";
        $result = db_query("SELECT * FROM node WHERE type = '%s'", $node_type);
        $nids = array();
        while ($obj = db_fetch_object ($result)) {
          $nids[] = $obj;
        }

        $idsToInclude = array();
        foreach ($nids as $pageId) {
            $page_id = $pageId->nid;
            $realm = 'content_access_rid';// content access
            $grant_view = 1; // Has view
            $result = db_query("SELECT * FROM node_access WHERE nid != '%d' and nid = '%d' and gid = '%d' and realm = '%s' and grant_view = '%d'",$currentNode, $page_id, $role_id, $realm, $grant_view);

            while ($obj = db_fetch_object ($result)) {
              $idsToInclude[] = $obj;
            }           
        }

        //var_dump($user->uid);
        //var_dump($nids);

        foreach($idsToInclude as $id){
            $courses = $id->nid;
            $nodeid = node_load($courses );
            if(node_access('view', $nodeid, $user)){ 
                echo '<li class="course_link"><a href="/node/'.$courses .'" title="">'.$nodeid->title.'</a></li>'; 
            }
        } 
    echo '</ul>';
}

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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