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 />

if (!user_access('access content', $account)) {<br />

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

return FALSE;<br />

}<br />

The first if clause ensures that we have a proper $account for the check.<br />

Remember that even anonymous users generate a valid $account object<br />

and may have assigned permissions.<br />

The second clause enforces the static cache. This is a performance optimization new<br />

to <strong>Drupal</strong> 7.<br />

The third is a user_access() check new to <strong>Drupal</strong> 7 and allows super-users to pass<br />

all node access checks and perform all operations on all nodes. This permission was<br />

split off from the administer nodes permission of prior versions in order to more<br />

clearly indicate how node access functions. It has the added benefit of allowing more<br />

granular permissions.<br />

The last is another user_access() check. It simply checks that a user may<br />

access content on the site. If not, then the user is always denied access to<br />

all node operations.<br />

Invoking hook_node_access()<br />

To this point, the code is fairly obvious and the intentions are clear: <strong>Drupal</strong> is<br />

running basic security checks against known values. At this point, the core node<br />

module begins querying other modules about the access status of the node. The next<br />

piece invokes hook_node_access() to check for access rules:<br />

// We grant access to the node if both of the following conditions<br />

// are met:<br />

// - No modules say to deny access.<br />

// - At least one module says to grant access.<br />

// If no module specified either allow or deny, we fall back to the<br />

// node_access table.<br />

$access = module_invoke_all('node_access', $node, $op, $account);<br />

if (in_array(NODE_ACCESS_DENY, $access, TRUE)) {<br />

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

return FALSE;<br />

}<br />

elseif (in_array(NODE_ACCESS_ALLOW, $access, TRUE)) {<br />

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

return TRUE;<br />

}<br />

[ 247 ]

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

Saved successfully!

Ooh no, something went wrong!