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.

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

The form is additionally tagged with a $form['#token'] element during<br />

drupal_process_form(). The #token is used to ensure that a form request came<br />

from a known request (that is, an HTTP request that has been issued a valid session<br />

for the site). The #token value is set with drupal_get_token():<br />

function drupal_get_token($value = '') {<br />

return drupal_hmac_base64($value, session_id().<br />

drupal_get_private_key(). drupal_get_hash_salt());<br />

}<br />

When <strong>Drupal</strong> processes a form, both the $form_build_id and $form['#token']<br />

values are validated to ensure that the form request originated from the <strong>Drupal</strong> site.<br />

We should also note that <strong>Drupal</strong> forms default to using the POST<br />

method. While it is possible to submit <strong>Drupal</strong> forms via GET, developers<br />

are always encouraged to use POST, which is more secure. We will look<br />

at securing GET requests when we discuss AJAX handling a little later in<br />

this chapter.<br />

Disabling form elements<br />

In addition to the global security of a specific form, you may also wish to enable or<br />

disable specific parts of a form, either your own module's form or that provided by<br />

<strong>Drupal</strong> core (or another contributed module). In the first example of this chapter,<br />

we saw how this can be done using the user_access() function (or a similar access<br />

control function) to mark an individual form element or entire section of a form<br />

as inaccessible.<br />

$form['menu'] = array(<br />

'#type' => 'fieldset',<br />

'#title' => t('Menu settings'),<br />

'#access' => user_access('administer menu'),<br />

'#collapsible' => TRUE,<br />

'#collapsed' => !$link['link_title'],<br />

);<br />

When the content editing form is rendered, users without the administer menu<br />

permission will not see this element of the form.<br />

Note that '#access' => FALSE is not the same as<br />

'#disabled' => FALSE in <strong>Drupal</strong>'s Forms API. Using<br />

#disabled => FALSE will render the form element and disable<br />

data entry to that element, while '#access' => FALSE removes<br />

the element entirely from the output.<br />

[ 230 ]

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

Saved successfully!

Ooh no, something went wrong!