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

Responding when access is denied<br />

In a significant change from earlier <strong>Drupal</strong>, the drupal_access_denied() function<br />

should no longer be called when returning a normal page context.<br />

• Page callback functions wanting to report an access denied message should<br />

return MENU_ACCESS_DENIED instead of calling drupal_access_denied()<br />

• However, functions that are invoked in contexts where that return value<br />

might not bubble up to menu_execute_active_handler() should call<br />

drupal_access_denied(). For more details see:<br />

http://api.drupal.org/api/function/drupal_access_denied/7<br />

However, what does this mean in practice?<br />

One advantage of using a menu callback is that if access is denied for the page<br />

request, <strong>Drupal</strong> automatically handles the response by running the traditional<br />

drupal_access_denied() function.<br />

However, the <strong>Drupal</strong> 7 rendering engine respects more contexts than the traditional<br />

web page. Your callback function might return a JSON object, a file, or be responding<br />

as a part of a larger page (such as a form). For example, consider the following<br />

snippet of code from contact_site_form():<br />

// Check if flood control has been activated for sending e-mails.<br />

$limit = variable_get('contact_threshold_limit', 5);<br />

$window = variable_get('contact_threshold_window', 3600);<br />

[ 224 ]

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

Saved successfully!

Ooh no, something went wrong!