I wrote this little bit of code after asking around to find out if there was a way to add permissions by role and came up empty. Ends up there is a nice module for doing this for install profiles and could be used for update functions as well I am sure. I haven't looked at it, but you can check out the Install Profile API.
This apparently has some nifty items that aid in your install profile construction (thanks Boris!), however all I need is to add some checks to some checkboxes on the Permissions page via code, so I am using this:
<?php
/**
* _add_permissions() is a helper function to add permissions by role to the db
*/
function _add_permissions($rid, $permissions) {
if (!is_array($permissions)) {
$permissions = explode(', ', $permissions);
}
$current_perms = explode(', ', db_result(db_query("SELECT perm FROM {permission} WHERE rid=%d", $rid)));
foreach($permissions as $permission) {
if (!in_array($permission, $current_perms)) {
$current_perms[] = $permission;
}
}
$current_perms = implode(', ', $current_perms);
$return = db_query("UPDATE {permission} SET perm= '%s' WHERE rid=%d", $current_perms, $rid);
return $return;
}
?>This little function will take either a $permissions variable that is either an array, or a comma separated list of values that you want added to the permissions of a role (which you specify with the $rid value).
So, plop this into the top of your module.install file, and in any of your xyz_update_23() functions, call to it with a simple:
<?php
$add_permission_status = _add_permissions(3, 'delete all xyznode content, delete own xyznode content, add xyznode content, whatever custom permission you have created');
?>Or, you can supply a nice array and feed that to it:
<?php
$new_perms = array();
$rid = 3;
$new_perms[] = 'delete all xyznode content';
$new_perms[] = 'delete own xyznode content';
$new_perms[] = 'add xyznode content';
$new_perms[] = 'whatever custom permission you have created';
$add_permission_status = _add_permissions($rid, $new_perms);
?>Run your update, check out your permissions page, and provided these are actual permissions, you should see them checked. Woot!
According to the chatter in #drupal IRC channel, Drupal 7 handles this through it's own api call, sweeeeeeeeeet!


In Drupal 7, I wanna make a
In Drupal 7, I wanna make a content-type student by installing module Student. Here is what my code look like:
Install Process: Works, I installed it, then check the content was created.
function student_install()
{
$type_values = array(
'op' => 'Save content type',
'type' => 'student',
'name' => 'Student',
'orig_type' => '',
'old_type' => '',
'description' => 'Desc',
'help' => 'Exp',
'title_label' => '',
'body_label' => '',
'base' => '',
'custom' => '1',
'locked' => '0',
'modified' => '1'
);
$op = isset($type_values['op']) ? $type_values['op'] : '';
$type = node_type_set_defaults();
$type->type = trim($type_values['type']);
$type->name = trim($type_values['name']);
$type->orig_type = trim($type_values['orig_type']);
$type->old_type = isset($type_values['old_type']) ? $type_values['old_type'] : $type->type;
$type->description = $type_values['description'];
$type->help = $type_values['help'];
$type->title_label = $type_values['title_label'];
$type->body_label = $type_values['body_label'];
// title_label is required in core; has_title will always be true, unless a
// module alters the title field.
$type->has_title = ($type->title_label != '');
$type->has_body = ($type->body_label != '');
Right?
This is natively supported in
This is natively supported in Drupal 7, and the Permissions API module backports it to D6.
http://drupal.org/project/permissions_api
The Secure Permissions module also allows exporting of perms to code, and auto-maintenance of an admin role.