0

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:

  1. Administrator
  2. Authenticate User
  3. Anonymous user
  4. 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' => 'Approved Programmes Registration',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_test_form'),
    'access callback' => 'user_access', 
    'access arguments' => array('Teaching')
  ); 

  return $items;
}

My code is not working.

0

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' => 'Approved Programmes Registration',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('MYMODULE_form_test_form'),
    'access callback' => '_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->roles array.
  return (isset($account->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.

0

You have to implement hook_permission

e.g.,

function MyModule_permission() {
  return array(
    'teaching' => array(
      'title' => t('Teaching my module'),
      'description' => t('Perform teaching tasks for my module.'),
    ),
  );
}
function form_test_menu() {
    $items['programme_reg'] = array(
        'title' => 'Approved Programmes Registration',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('form_test_form'),
        'access callback' => 'user_access', 
        'access arguments' => array('teaching'),
    ); 
    return $items;
}

After that, you can give access from permission page to any role

0

The code is wrong because the access callback used (user_access()) accepts a user permission string as argument, not a role name. To check the role the currently logged in user has, you need your own access callback. I would rather user permissions, since this would permit me not to alter the code in my module, in the case I need to check for more roles.

If I were asked to check for roles, I would use the following code, which runs on Drupal 7.36 and higher.

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

  return $items;
}

function form_test_check_role($role_name) {
  if ($role = user_role_load_by_name($role_name)) {
    return user_has_role($role->rid);
  }

  // The role doesn't exists; return FALSE as safe value.
  return FALSE;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.