How can a user access only one specific node, and not all nodes of that type?

How can a user access only one specific node, and not all nodes of that type?

Larry Stewart Asked on October 16, 2015 in Drupal.
Add Comment
2 Answer(s)
Best answer

If you want to set a particular user to be able to access just one node, the easiest way is to use Content Access, which allows you to set the permissions for each user to access a particular node. After that you will have to manually set the access permissions for each node.

Of you want to go for the manual set, implement hook_node_access() and use a code similar to the one below:

function mymodule_node_access($node, $op, $account) {
  
  if (is_object($node)) {
    $bool = (
      ($node->type == 'the node type you need to check') && 
      (($op == 'view') || ($op == 'update'))
    );
    if ($bool) {
      if ($account->uid == $node->uid) {
        return NODE_ACCESS_ALLOW;
      }
      else {
        return NODE_ACCESS_DENY;
      }
    }
  }

  return NODE_ACCESS_IGNORE;
}

A perk of using this hook implementation is the  you don’t need to manually edit the access permissions of each node but just adjust the node owner.

Geraldine Stevens Answered on October 16, 2015.
Add Comment

If you want to set a particular user to be able to access just one node, the easiest way is to use Content Access, which allows you to set the permissions for each user to access a particular node. After that you will have to manually set the access permissions for each node.

Of you want to go for the manual set, implement hook_node_access() and use a code similar to the one below:

function mymodule_node_access($node, $op, $account) {
  
  if (is_object($node)) {
    $bool = (
      ($node->type == 'the node type you need to check') && 
      (($op == 'view') || ($op == 'update'))
    );
    if ($bool) {
      if ($account->uid == $node->uid) {
        return NODE_ACCESS_ALLOW;
      }
      else {
        return NODE_ACCESS_DENY;
      }
    }
  }

  return NODE_ACCESS_IGNORE;
}

A perk of using this hook implementation is the  you don’t need to manually edit the access permissions of each node but just adjust the node owner.

Geraldine Stevens Answered on October 16, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.