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.

<strong>Drupal</strong> Permissions and Security<br />

The second permission grants the ability to search site content using the default<br />

search form. The third permission extends the second to include an additional<br />

form for advanced searches.<br />

The presence of these very specific permissions may seem odd, given that<br />

there is an access content permission in the node module that grants<br />

users the ability to view site content. However, search may be considered<br />

a special case by some sites. Separating the search content permission<br />

from the access content permission adds a layer of flexibility that<br />

enables project customization.<br />

People who complain that <strong>Drupal</strong> is too complex should consider this case for a<br />

moment. From a site builder perspective, having three extra permissions means more<br />

configuration work. However, imagine how frustrating it would be if you needed to<br />

disable search for some users but could not (or could only do so programmatically).<br />

In cases like these, <strong>Drupal</strong> almost always embraces flexibility. The presence of<br />

multiple permissions in a core module means that someone has needed that<br />

separation for a good reason. (In the next chapter, in fact, we will discuss the reason<br />

for the bypass node access permission, which is a new feature in <strong>Drupal</strong> 7.)<br />

Defining your module's permissions<br />

Before writing any code for hook_permission(), it is the best practice to take out a<br />

pen and paper (or a good diagramming program), and make a chart of the actions<br />

you expect users to take. In fact, many experienced developers write this hook at the<br />

end of development, after puzzling through all the use cases in the code.<br />

Let's consider the preceding example module. It is very direct. We want to show<br />

some information about users to trusted site administrators. Our use case looks<br />

something like the following:<br />

[ 218 ]

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

Saved successfully!

Ooh no, something went wrong!