25

I have some permissions that I would like to assign to some roles programmatically for easier installation.

Because I'm installing the module and creating both permission and roles, I don't have the role id's, where I want to set the permissions. So far I have only found ways to assign roles programmatically that need the role ID.

I wonder if I somehow could look up the roles with their name (string), retrieve the role ID and then assign permission. It just doesn't feel right.

  • The permission table use the role ID, not the role name. You need to get the role ID, and then add the permission using that ID. The permission table then need the string for the permission, and the name of the module implementing it (at least on Drupal 7); I am not sure if the module name is really used in any way. – kiamlaluno Jan 31 '12 at 17:56
  • Is there anything specific that you don't understand about the code that you should use? What code did you use? Showing your code would be useful; it would help who answers to suggest what you need to do (e.g. reporting what is wrong in your code). – kiamlaluno Jan 31 '12 at 17:59
  • Thanks for the answer. I think I have to look up the IDs in the database. It should not be that difficult. – vogelsang Feb 1 '12 at 7:49
1

Features module does this quite nicely, allowing you to export roles and permissions (and lots of other stuff) in one fell swoop:

http://drupal.org/project/features

But if you want to roll your own check out Permissions API:

http://drupal.org/project/permissions_api

  • 2
    The problem is that you don't know the role id when you use features because they are assigned when you install your feature. Therefore, you need to find the IDs somehow to assign the permissions. It is probably just a lookup in the database. – vogelsang Feb 1 '12 at 7:47
23

Features is pretty handy for stuff like this. However, if you do need to create roles and add permissions programmatically, you don't have to go digging through the database on your own.

When you create a new user role with user_role_save(), it will update the object you passed in with the new user role id that was assigned. You can see an example of this in the standard_install() function in the Standard installation profile.

$admin_role = new stdClass();
$admin_role->name = 'administrator';
...
user_role_save($admin_role);
user_role_grant_permissions($admin_role->rid, array_keys(module_invoke_all('permission')));

This example shows that you can create a new user role, then access its rid property, which was filled in by user_role_save(). It also shows how to add permissions to that role with user_role_grant_permissions(). In this case, it's granting the 'administrator' role all available permissions.

If you need to get the id of a user role created by someone else, you can use user_role_load_by_name() to get the object for that user role. Then you just grab the id with $role->rid.

20

I'd like to add a code example that is closer to the original question on how to assign permissions to existing roles programmatically.

You can look up the roles with their name, retrieve the role ID and then assign permissions as follows (for example in a hook_update implementation):

function custommodule_update_7001() {
  $permissions = array('view my custom entity');
  foreach(array('anonymous user', 'authenticated user') as $role_name) {
    $role = user_role_load_by_name($role_name);
    user_role_grant_permissions($role->rid, $permissions);
  }
}

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.