hook_node_access

  1. drupal
    1. 8
    2. 7
Versions
7 – 8 hook_node_access($node, $op, $account)

Control access to a node.

Modules may implement this hook if they want to have a say in whether or not a given user has access to perform a given operation on a node.

The administrative account (user ID #1) always passes any access check, so this hook is not called in that case. Users with the "bypass node access" permission may always view and edit content through the administrative interface.

Note that not all modules will want to influence access on all node types. If your module does not want to actively grant or block access, return NODE_ACCESS_IGNORE or simply return nothing. Blindly returning FALSE will break other node access modules.

Parameters

object|string $node Either a node object or a (machine-readable) content type on which to perform the access check.

string $op The operation to be performed. Possible values:

  • "create"
  • "delete"
  • "update"
  • "view"

object $account The user object to perform the access check operation on.

Return value

integer NODE_ACCESS_ALLOW if the operation is to be allowed; NODE_ACCESS_DENY if the operation is to be denied; NODE_ACCESS_IGNORE to not affect this operation at all.

Related topics

Code

modules/node/node.api.php, line 592

<?php
function hook_node_access($node, $op, $account) {
  $type = is_string($node) ? $node : $node->type;

  if (in_array($type, node_permissions_get_configured_types())) {
    if ($op == 'create' && user_access('create ' . $type . ' content', $account)) {
      return NODE_ACCESS_ALLOW;
    }

    if ($op == 'update') {
      if (user_access('edit any ' . $type . ' content', $account) || (user_access('edit own ' . $type . ' content', $account) && ($account->uid == $node->uid))) {
        return NODE_ACCESS_ALLOW;
      }
    }

    if ($op == 'delete') {
      if (user_access('delete any ' . $type . ' content', $account) || (user_access('delete own ' . $type . ' content', $account) && ($account->uid == $node->uid))) {
        return NODE_ACCESS_ALLOW;
      }
    }
  }

  // Returning nothing from this function would have the same effect.
  return NODE_ACCESS_IGNORE;
}
?>