Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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

Because I am 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 programatically 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.

share|improve this question
    
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
add comment

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer
1  
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
add comment

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.

share|improve this answer
1  
This is nice for granting/revoking multiple permissions at once: api.drupal.org/api/drupal/modules%21user%21user.module/function/… –  Brian Wood Apr 10 '13 at 0:17
    
++user_role_grant_permissions() –  cdmo Aug 1 '13 at 18:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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