New Drupal Module Released

Back in October, I released my first module for Drupal, the open-source content management system. These days, I seem to be developing exclusively for Drupal, and with a robust API and thriving community, I can only say how much fun it is to work with.

However, like any platforms, there are pieces missing. Fortunately, Drupal is one of those platforms that is very easily extended through modules. I came across one of those missing pieces while working on a project. I do all of my development in a sandbox, and when the work is complete, I QA the product in a staging environment before pushing the code and database changes to a production environment. I found that there was no way to manage user/role permissions in code; all management was a manual process via the web interface.

Having to repeat manual tasks like this across different environments increases the odds of errors. Steps can be omitted or performed improperly. In the case of permissions, I decided to correct that by writing a module called Permissions API. What this module allows you to do is to grant and revoke permissions to roles in code.

This is probably most useful in the context of programmatically creating CCK content types. The ability to import CCK content types through code is great until you decide that you want members of specific roles to be able to do something with this content type. Currently, the only way to grant the permissions is to navigate through the access control page in the admin interface, which is completely unusable if you have a lot of roles and a lot of modules. This module addresses that problem by providing two functions:

permissions_grant_permissions()
permissions_revoke_permissions()

Each function accepts a role id and an array of permissions. Sample usage would be:


<?php
function mymodule_update_1(){
  
// Handle roles and permissions
  
$rid db_result(db_query('SELECT r.rid FROM {role} r WHERE r.name = \'some custom role\''));
  if(
$rid 0){
    
$permissions = array(
      
'create some_content_type content',
      
'edit some_content_type content',
      
'edit own some_content_type content',
    );
    
permissions_grant_permissions($rid$permissions);  
  }
}
?>

This module also provides functions for granting all permissions defined by a given module to a given role, as well as granting all defined permissions to a given role, which is useful for creating admin-type roles. Sample usage would be:

<?php
function mymodule_update_2(){
  
// Create a "super-admin" role by granting all permissions
  
$rid db_result(db_query('SELECT r.rid FROM {role} r WHERE r.name = \'some custom role\''));
  if(
$rid 0){
    
permissions_grant_all_permissions($rid);
  }
}
?>

The next example shows how to grant all permissions defined by a specific module to a specific role:

<?php
function mymodule_update_3(){
  
// Grant all permissions defined by a module's hook_perm implementation
  
$rid db_result(db_query('SELECT r.rid FROM {role} r WHERE r.name = \'some custom role\''));
  if(
$rid 0){
    
permissions_grant_all_permissions_by_module($rid'some module')
  }
}
?>

[tags]api, code, drupal, module, permissions[/tags]

Comments

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

About Erich

Erich is a web developer and a native New Englander who is passionate about life, the universe, and everything.

He is currently a senior Drupal developer at Harvard University, working on the IQSS OpenScholar project.  Prior to joining the team at Harvard, he was the engineering manager at CommonPlaces e-Solutions, in Hampstead, NH, contributing as the lead engineer on the Greenopolis.com and Twolia.com.

Erich is active in the Drupal community, having contributed modules and patches to the community. He presented at DrupalCon in Szeged Hungary, and co-presented at DrupalCon 2009 in Washington, DC.

Erich lives in New Hampshire with his wife, two sons, and two weimaraners.  When not writing code, Erich enjoys landscaping and woodworking.

Faceted search

Categories

Content type

Project types

Artwork Type

Artwork Tags

Recent comments

ebeyrent's tweets

Activity Stream

September 2, 2010

September 1, 2010

  • Twitter ebeyrent tweeted "@cpliakas LOL @webkenny as @acquia product" 11:23am #

August 31, 2010

  • Twitter ebeyrent tweeted "Dear @msnbc, I want to see the hurricane report, not a goddamn advertisement!! #thisisseriousshit" 12:08pm #
  • Twitter ebeyrent tweeted "Need a good D6 starter theme, looking at Blueprint, Fusion, and Omega. Any other recommendations? Preferences? #drupal" 11:35am #

August 30, 2010

  • Twitter ebeyrent tweeted "Having fun upgrading a D5 site to D6. #drupal" 3:05pm #
  • Twitter ebeyrent tweeted "@DrupalSnark Believe it or not, this was actually from a DrupalCamp presentation at Yale this weekend..." 11:38am #
  • Twitter ebeyrent tweeted "Today is an exciting and bittersweet milestone. My oldest son enters first grade..." 6:31am #

August 28, 2010

  • Twitter ebeyrent tweeted "Following @jjeff's presentation was like walking into the opera wearing my clown shoes. #drupalcampct" 4:38pm #
  • Twitter ebeyrent tweeted "Overlay module in #drupal 7 is awful. Don't use it. #drupalwtf" 4:31pm #
  • Twitter ebeyrent tweeted "Profile module in #drupal 7 doesn't use fields. Still. Don't use it. #drupalwtf" 4:29pm #
  • Twitter ebeyrent tweeted "Slides from the "Hack-proof Your Drupal App" presentation at #drupalcampct are online: http://bit.ly/9q9yRK" 4:19pm #
  • Twitter ebeyrent tweeted "@jjeff consults the orb at #drupalcampct" 2:42pm #
  • Twitter ebeyrent tweeted "Listening to @jjeff's presentation at #drupalcampct" 2:21pm #