0

I am writing a custom module for my Drupal 7 site. I have added the code to enable a configure form.

I want to restrict the config to admin users only, and some of the functions to another group of users. How do I go about doing this?

Any and all help greatly appreciated.

2

You can use hook_permission() in your module. This allows you to add a custom permission. For example:

return array(
  'your permission name' => array(
    'title' => t('your permission title'),
    'description' => t('Description.'),
  ),
);

Afterwards, go to the permission page and set the ticks where you need them.

  • How would I check in each function that the current user had the right permission? – Rooneyl Dec 13 '11 at 14:54
  • Use the user_access() function. Fore example: user_access('your role name') – Lance Dec 13 '11 at 16:06
  • 3
    In hook_permissions(), you don't define roles, you define permission, which you then apply to roles. The code itself is correct, but you probably want to use a description of the permission instead of a role name there. E.g. "access mymodule adminstration pages". – Berdir Dec 14 '11 at 9:57
  • @Rooneyl: Berdir is right. Sorry. – Lance Dec 14 '11 at 10:50

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.