Patrick Teglia on Drupal
Adding permissions to a role programatically
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;
}
?>
Draggable Views, a UI sanity saver!
In many instances, I have seen drag and drop (dnd) ordering, rearranging, what have you, used where it perhaps didn't really need to be, but sometimes, there is a real good use for it. I ran into that today.

