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.

Node Access<br />

Here we see a distinct difference between <strong>Drupal</strong> 7 and <strong>Drupal</strong> 6 (and earlier): any<br />

module may respond to this access check. Prior to <strong>Drupal</strong> 7, only modules that<br />

defined a node type could respond, using the old hook_access() function. This<br />

constraint made it difficult for module developers to modify the business logic for<br />

node_access(). This is a major change in the <strong>Drupal</strong> API, and one which we will<br />

explore in some depth.<br />

The constants NODE_ACCESS_DENY and NODE_ACCESS_ALLOW are set by<br />

node.module. We will look at these later in the chapter.<br />

Notice also the note in the comments: If no module specified either allow or<br />

deny, we fall back to the node_access table. The execution order of Node<br />

Access hooks matters. When we consider the logic for our business rules, we must<br />

remember that other modules may also have a stake in the access rights to a node.<br />

So far, we're up to five return statements in the code.<br />

Access to a user's own nodes<br />

The next clause is an exception for handling nodes created by the current user:<br />

// Check if authors can view their own unpublished nodes.<br />

if ($op == 'view' && !$node->status && user_access('view own<br />

unpublished content', $account) && $account->uid == $node->uid &&<br />

$account->uid != 0) {<br />

$rights[$account->uid][$cid][$op] = TRUE;<br />

return TRUE;<br />

}<br />

<strong>Drupal</strong> assumes that unpublished content should not be visible to users. However,<br />

the view own unpublished content permission exists to allow authenticated users<br />

to see their content even if it has not been published. Unless a third-party module<br />

intervenes, only users with this permission, bypass node access or user 1 may<br />

view unpublished content.<br />

Invoking the node access API<br />

Now that <strong>Drupal</strong> has accounted for that special case, the code falls through to the<br />

{node_access} table for checking permissions.<br />

// If the module did not override the access rights, use those set<br />

// in the node_access table.<br />

if ($op != 'create' && $node->nid) {<br />

[ 248 ]

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

Saved successfully!

Ooh no, something went wrong!