Patrick Teglia on Drupal

Syndicate content
Updated: 56 min 21 sec ago

Adding permissions to a role programatically

Wed, 02/17/2010 - 11:36

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;
}
?>

read more

Draggable Views, a UI sanity saver!

Sat, 12/26/2009 - 21:11

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.

read more

Making git a bit easier on me

Thu, 12/03/2009 - 13:09
So lately I have been having all kinds of issues forgetting to do a git pull before doing a git push.

read more