You are here

function node_access_example_node_access in Examples for Developers 7

Implements hook_node_access().

Allows view and edit access to private nodes, when the account requesting access has the username 'foobar'.

hook_node_access() was introduced in Drupal 7. We use it here to demonstrate allowing certain privileges to an arbitrary user.

See also

hook_node_access()

Related topics

File

node_access_example/node_access_example.module, line 220
Module file illustrating API-based node access.

Code

function node_access_example_node_access($node, $op, $account) {

  // If $node is a string, the node has not yet been created. We don't care
  // about that case.
  if (is_string($node)) {
    return NODE_ACCESS_IGNORE;
  }
  if (($op == 'view' || $op == 'update') && (!empty($account->name) && $account->name == 'foobar') && !empty($node->private)) {
    drupal_set_message(t('Access to node @nid allowed because requester name (@name) is specifically allowed', array(
      '@name' => $node->name,
      '@uid' => $account->uid,
    )));
    return NODE_ACCESS_ALLOW;
  }
  return NODE_ACCESS_IGNORE;
}