0

I have created a content type called Details it is the collection of employees detail. And also I have created a Taxonomy terms called manager, employee, lead. Also I have created a field(as Taxonomy reference) for user profile to indicate whether she/he is manager or lead or employee. Now I want to put the restriction based on the user. I have achieve the restriction using my custom module. The codes are below:

    function my_module_node_access($node, $op, $account) {
  global $user;
  $user_obj = user_load($account->uid);
  if (isset($node->type) && ($node->type == 'details') && ($op == 'view')) {
        if (($user->uid == 0) || ($user_obj->field_user_company['und'][0]['tid'] != $node->field_company['und'][0]['tid'])){
            return NODE_ACCESS_DENY;
        }       
    }  
  return NODE_ACCESS_IGNORE;
}

The above code simply do, if the node taxonomy term, and the user field taxonomy term is same then he will perfectly access to the nodes. I got struggle with views. I have created a view to show the node title that can be access by the user. Manager has 100 nodes to view, in views settings I have enable the pager, 25 items per page to display. Pager splits into 4 page and shows correctly. But when comes to employee user he has access to 1 node only, but in views it still shows the pager in 4 parts.

function my_module_views_post_execute(&$view) {
  if (($view->name == 'views1') || ($view->name == 'views2')) {
        $nids = array();
      foreach($view->result as $key=>$result) {
          $nids[$key] = $result->nid;
      }
      $nodes = node_load_multiple($nids);
      foreach ($nodes as $node) {
            if (!node_access('view',$node)) {
                $key = array_search($node->nid, $nids);

                unset($view->result[$key]);
            }
        }
     }
  }

The views pager display must be changed dynamically depends on the number of nodes. Is anyone knows kindly show me a right path to achieve this???

1 Answer 1

0

hook_node_access() doesn't really do any access checks on node listings or views.

To get this to work you should look into implementing hook_node_grants() and hook_node_access_records()

That logic will be included in your views query so you could get rid of hook_views_post_execute()

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.