function node_access_example_node_access

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

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;
    }
    // We check that we are logged in, the private flag (field) for this node
    // was enabled, but our name is 'foobar', then we give access in any case.
    if (($op == 'view' || $op == 'update') && (!empty($account->name) && $node->private && $account->name == 'foobar')) {
        drupal_set_message(t('Access to node @nodename allowed because requester name (@name) is specifically allowed', array(
            '@nodename' => $node->name,
            '@name' => $account->name,
        )));
        return NODE_ACCESS_ALLOW;
    }
    return NODE_ACCESS_IGNORE;
}