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

function example_user_page($account) {<br />

global $user;<br />

if ($user->uid != $account->uid) {<br />

return MENU_ACCESS_DENIED;<br />

}<br />

// ...<br />

There is a subtle yet important difference between the two approaches. If we use<br />

a menu callback to assert access control, the tab link will only be rendered if the<br />

user passes the access check. If we use an access check within the page callback, the<br />

tab will always be rendered. It is poor usability to present a tab that only prints an<br />

'access denied' message to the user. For this reason, page-level access checks should<br />

almost always be handled by hook_menu().<br />

Should I use drupal_access_denied() or a custom page?<br />

drupal_access_denied() returns a version of the traditional Apache<br />

403 access denied page, served by <strong>Drupal</strong>. Good usability suggests that<br />

providing a friendlier error message page helps users navigate your site<br />

with ease. If you support this idea, feel free to create a custom 403 page.<br />

<strong>Drupal</strong> allows you to assign any content page as the 403 message page.<br />

The drupal_access_denied() function returns the output of that<br />

page, so there is no need to code a custom 403 message into your module<br />

since one can be created and edited through the normal <strong>Drupal</strong> content<br />

interface.<br />

The settings for your 403 and 404 page are found under the Site Information settings.<br />

[ 226 ]

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

Saved successfully!

Ooh no, something went wrong!