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

// Get a list of user roles.<br />

$roles = user_roles();<br />

// Get our permission definitions.<br />

$permissions = role_access_permission();<br />

// Initiate a results variable, for theming the output.<br />

$results = array();<br />

// Check each access rule.<br />

foreach (array('view', 'update', 'delete') as $op) {<br />

if (in_array($row->gid, array_keys($roles))) {<br />

$results[] = t('%roles with the permission %perm may %op<br />

this content', array('%role' => $roles[$row->gid], '%perm' =><br />

$permissions[$op . ' role access content']['title'], '%op' => $op));<br />

}<br />

}<br />

// Prepare for theming. The $results are already secure, having run<br />

// through t().<br />

$variables = array(<br />

'items' => $results,<br />

'title' => '',<br />

'type' => 'ul',<br />

);<br />

// Return a nicely themed list.<br />

return theme('item_list', $variables);<br />

}<br />

By providing this hook, both the developer and module users can enable Devel Node<br />

Access to see how node access rules are being enforced.<br />

Using the Devel Node Access by user block<br />

The Devel Node Access module also provides a block which displays the results of<br />

the node_access() function. This block can help you sort through the reason(s) why<br />

access has been granted or denied. It presents a table, showing the ten most recent<br />

site visitors and their access to a specific node.<br />

[ 284 ]

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

Saved successfully!

Ooh no, something went wrong!