You are here

function node_access_example_node_grants in Examples for Developers 6

Same name and namespace in other branches
  1. 7 node_access_example/node_access_example.module \node_access_example_node_grants()

Implements hook_node_grants().

Tell the node access system what grant IDs the user belongs to for each realm, based on the operation being performed.

When the user tries to perform an operation on the node, Drupal calls hook_node_grants() to determine grant ID and realm for the user. Drupal looks up the grant ID and realm for the node, and compares them to the grant ID and realm provided here. If grant ID and realm match for both user and node, then the operation is allowed.

Grant ID and realm are both determined per node, by your module in hook_node_access_records().

In our example, we've created three access realms: One for authorship, and two that track with the permission system.

We always add node_access_example_author to the list of grants, with a grant ID equal to their user ID. We do this because in our model, authorship always gives you permission to edit or delete your nodes, even if they're marked private.

Then we compare the user's permissions to the operation to determine whether the user falls into the other two realms: node_access_example_view, and/or node_access_example_edit. If the user has the 'access any private content' permission we defined in hook_permission(), they're declared as belonging to the node_access_example_realm. Similarly, if they have the 'edit any private content' permission, we add the node_access_example_edit realm to the list of grants they have.

See also

node_access_example_perm()

node_access_example_node_access_records()

Related topics

File

node_access_example/node_access_example.module, line 111
This is an example illustrating how to restrict access to nodes based on some criterion associated with the user.

Code

function node_access_example_node_grants($account, $op) {
  if ($op == 'view' && user_access('access private content', $account)) {
    $grants['node_access_example'] = array(
      NODE_ACCESS_EXAMPLE_GRANT_ALL,
    );
  }
  if (($op == 'update' || $op == 'delete') && user_access('edit private content', $account)) {
    $grants['node_access_example'] = array(
      NODE_ACCESS_EXAMPLE_GRANT_ALL,
    );
  }
  $grants['node_access_example_author'] = array(
    $account->uid,
  );
  return $grants;
}