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

Using an existing module, you can do that with Content Access, which (when used together ACL) allows to set the permissions to access a node for each user.
This means that you need to set the access permissions manually for each node.

If you manually create the nodes, and then you want to be sure that only the user who is set as owner of the node is able to view (and edit) it, then you can create a custom module (which means a module that is used for your site), and implement hook_node_access() as follows (the code has been written to make it easier to read):

function mymodule_node_access($node, $op, $account) {
  // We are interested to existing objects. When the node is being created, Drupal passed the node type instead of the node object. 
  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;
}

Using this hook implementation, you don't need to manually edit the access permissions given for each node of that content type you create. Changing the owner of the node would also be easier, as you don't need to change the access permissions of that node; the code would automatically grant the update and view permissions to the user set as owner (or author) of the node.


You don't need any special module or custom code to do this. Just create manualy those nodes, set appropriate users as owner (author) of the nodes and set permissions to this contentype to edit own content only (not edit any content of this type) and you are done.

Tags:

Nodes

Users

7