18.10.2016 Views

Drupal 7 Module Development

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Node Access<br />

Once we have these permissions in place, we can simply enforce them inside<br />

hook_node_grants(). We must add the permission logic to our foreach loop.<br />

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

// Get the user roles.<br />

$roles = array_keys($account->roles);<br />

}<br />

// Error checking.<br />

if (empty($roles)) {<br />

return array();<br />

}<br />

// Initialize a $grants array.<br />

$grants = array();<br />

// Iterate through the roles.<br />

foreach ($roles as $grant_id) {<br />

// Check the permission callback!<br />

if (user_access($op . ' role access content')) {<br />

$grants['role_access'][] = $grant_id;<br />

}<br />

}<br />

// Return the grants.<br />

return $grants;<br />

Now we have a mechanism for restricting the access rules based on the operation<br />

being performed. Our recommended configuration looks like this:<br />

[ 272 ]

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

Saved successfully!

Ooh no, something went wrong!