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

}<br />

// Check anonymous users.<br />

else if ($access) {<br />

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

}<br />

With this code in place, we can easily assign the proper permissions to allow roles to<br />

view content as if they were authenticated users.<br />

Using hook_node_access_records_alter()<br />

This is great! Using very little code, we have made a major change to the business<br />

logic of the Role Access module, something that was nearly impossible prior<br />

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

We still have a problem, however. Since all custom roles are also given the<br />

'authenticated user' role, we are storing grants in the {node_access} table that may<br />

be too permissive. It may be that we do not want to store the records at all. So we<br />

have another hook we can use, in conjunction with a new permission.<br />

First, we edit role_access_extend_permission():<br />

/**<br />

* Implement hook_permission().<br />

*/<br />

function role_access_extend_permission() {<br />

return array(<br />

'view role access as authenticated user' => array(<br />

'title' => t('View role-restricted content as authenticated<br />

user'),<br />

'description' => t('Allow anonymous users to view content<br />

created by authenticated users. Lack of this permission removes access<br />

for users in custom roles.'),<br />

),<br />

'assign role access as authenticated user' => array(<br />

'title' => t('Save role-restricted content as authenticated<br />

user'),<br />

'description' => t('Save new and updated content so that<br />

authenticated users have permissions. Normally this is set to<br />

off.'),<br />

),<br />

);<br />

}<br />

[ 279 ]

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

Saved successfully!

Ooh no, something went wrong!