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

For our test module, we want to leverage <strong>Drupal</strong>'s role system to create tiers of users.<br />

Each member of a role will only be able to View, Edit, or Delete content created by<br />

other members of that role. Some users, however, may not be allowed to Edit or<br />

Delete content, so our module needs to separate the three permissions properly.<br />

Written out, here is what our rule set looks like:<br />

1. All users will be assigned to one or more roles.<br />

2. All nodes will be assigned grants based on the role(s) of the node author.<br />

3. Users will be assigned individual grants for the View, Update, and Delete<br />

actions based on their assigned roles and permissions.<br />

4. Users with the proper permission will be able to View, Update, and Delete a<br />

node, regardless of its publication state.<br />

5. If the author of a node changes, the grants for that node will change.<br />

6. If the author of a node is assigned new roles, the grants for the author's nodes<br />

will not be altered unless the nodes are updated.<br />

Remember:<br />

These rules all seem pretty straightforward, except for<br />

rule #6. To be honest, we put in rule #6 to prevent us from<br />

having to write additional code to handle this case, which<br />

would merely complicate our example. However, when<br />

writing a node access module, you need to keep in mind all<br />

the variables that may affect how your rule sets are enforced.<br />

• Node access rules are recalculated and stored every time a node is saved<br />

• User access grants are calculated for every page request<br />

• Node access rules must be rebuilt for the entire site any time a node access<br />

module is added or removed<br />

• Node access grants may be different for each of the three operations – View,<br />

Update, and Delete – even for the same user<br />

Your module needs to be aware of the greater context in which its rules operate.<br />

So writing out the expected behavior of the module – and posting that definition<br />

in a README file and as online and module help documentation – is a key part<br />

to writing a good node access module.<br />

Based on the rules and guidelines above, we have a good idea how to write our<br />

module. Let's call it Role Access and get to work.<br />

[ 265 ]

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

Saved successfully!

Ooh no, something went wrong!