18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9<br />

hook_node_access() is the simpler of the two systems. It is a self-contained hook that<br />

allows individual access control modules to pass judgment on a node. Note, however,<br />

that in <strong>Drupal</strong> core its use is limited to only three of the four node operations: Create,<br />

Update and Delete. We can see this clearly in node.module's implementation:<br />

/**<br />

* Implements hook_node_access().<br />

*/<br />

function node_node_access($node, $op, $account) {<br />

$type = is_string($node) ? $node : $node->type;<br />

if (in_array($type, node_permissions_get_configured_types())) {<br />

if ($op == 'create' && user_access('create ' . $type . ' content',<br />

$account)) {<br />

return NODE_ACCESS_ALLOW;<br />

}<br />

if ($op == 'update') {<br />

if (user_access('edit any ' . $type . ' content', $account) ||<br />

(user_access('edit own ' . $type . ' content', $account) && ($account-<br />

>uid == $node->uid))) {<br />

return NODE_ACCESS_ALLOW;<br />

}<br />

}<br />

if ($op == 'delete') {<br />

if (user_access('delete any ' . $type . ' content', $account)<br />

|| (user_access('delete own ' . $type . ' content', $account) &&<br />

($account->uid == $node->uid))) {<br />

return NODE_ACCESS_ALLOW;<br />

}<br />

}<br />

}<br />

}<br />

return NODE_ACCESS_IGNORE;<br />

Because hook_node_access() fires before checking the {node_access} table, it<br />

is used to define the default behavior for node permissions. This behavior is very<br />

useful for items like creating and editing content according to node type, but it can<br />

be very limiting when defining the rules for viewing a node. For that reason, <strong>Drupal</strong><br />

core never asserts a value on the View operation for a node.<br />

[ 251 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!