How do I limit the access basing on the user role? How do I use an access callback to limit the access basing on the user role?

For example, in my site I have the following roles:


Administrator
Authenticate User
Anonymous user
Teacher


I want to make a particular page available only to the users who have the Teacher role. How can I achieve this?

function form_test_menu() {
  $items['programme_reg'] = array(
    'title' =&gt; 'Approved Programmes Registration',
    'page callback' =&gt; 'drupal_get_form',
    'page arguments' =&gt; array('form_test_form'),
    'access callback' =&gt; 'user_access', 
    'access arguments' =&gt; array('Teaching')
  ); 

  return $items;
}


My code is not working.
user_access function takes permission name as an argument you can not pass user role name to it.

Here is how you can achieve this:

function MYMODULE_menu() {
  $items = array();

  $items['programme_reg'] = array(
    'title' =&gt; 'Approved Programmes Registration',
    'page callback' =&gt; 'drupal_get_form',
    'page arguments' =&gt; array('MYMODULE_form_test_form'),
    'access callback' =&gt; '_MYMODULE_check_user_role',
  ); 

  return $items;
}

function _MYMODULE_check_user_role() {
  $account = $GLOBALS['user'];

  // Assuming that RID 4 is Teacher.
  // You can also check for role name in $account-&gt;roles array.
  return (isset($account-&gt;roles[4]) ? TRUE : FALSE);
}


Assuming that user role Teacher has Role ID as 4, you can check and return TRUE/FALSE in your own custom access callback. You can also check using Role Name instead of Role ID, see here for more details.