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

// Check the permission.<br />

$access = user_access('assign role access as authenticated user',<br />

$account);<br />

}<br />

// Get the defined role id for 'authenticated user'.<br />

$rid = DRUPAL_AUTHENTICATED_RID;<br />

// Now add the role.<br />

if ($access) {<br />

$grants['role_access'][] = array(<br />

'realm' => 'role_access',<br />

'gid' => $rid,<br />

'grant_view' => 1,<br />

'grant_update' => 1,<br />

'grant_delete' => 1,<br />

'priority' => 0,<br />

);<br />

}<br />

// Or take it away.<br />

else {<br />

foreach ($grants as $key => $grant) {<br />

if ($grant['realm'] != 'role_access') {<br />

continue;<br />

}<br />

if ($grant['gid'] == $rid) {<br />

unset($grants[$key]);<br />

}<br />

}<br />

}<br />

When this code runs, our $grants will be modified as needed, and the records sent<br />

to the {node_access} table will reflect our new permissions. Another var_dump()<br />

looks like so:<br />

array(1) {<br />

[1]=><br />

array(6) {<br />

["realm"]=> string(11) "role_access"<br />

["gid"]=> int(5)<br />

["grant_view"]=> int(1)<br />

["grant_update"]=> int(1)<br />

["grant_delete"]=> int(1)<br />

["priority"]=> int(0)<br />

}<br />

}<br />

[ 281 ]

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

Saved successfully!

Ooh no, something went wrong!