Checking if a Drupal User has a Role

UPDATE: Over five years later, this post is still one of the most highly trafficked on my blog. Since then I've learned a lot and shared many other tips and modules. This past year I was honored to be a contributor to The Definitive Guide to Drupal 7, which includes many other great tips like this. I highly recommend it if you want to get the latest and greatest information on developing for Drupal.

I'm a little curious why a simple function to check if a user has a role doesn't exist in Drupal yet. Such curiosity that makes me wonder if I've just managed to miss it as long as I've been working with Drupal, so someone feel free to enlighten me. If anyone is looking for the same, please read on...

A fairly common task for me when adding PHP to CCK fields, Views arguments, content templates, etc. is to check whether the current user has been assigned a certain role. Generally in module development you're dealing with permissions and use user_access(), but when you're just customizing a site you often need such role based logic. (Drupal core has some, like the access settings for blocks.)

Use the following simple snippet to check if a user (in this example, it's the logged in user since I'm getting the global $user) has been assigned a certain role on your site:

global $user;

// Check to see if $user has the administrator role.
if (in_array('administrator', array_values($user->roles))) {
// Do something.

Replace 'administrator' with whatever role it is you're trying to check against. Alternatively, if you keep a helper module for use on your projects, go ahead and drop this function in it:

 * Check to see if a user has been assigned a certain role.
 * @param $role
 *   The name of the role you're trying to find.
 * @param $user
 *   The user object for the user you're checking; defaults to the current user.
 * @return
 *   TRUE if the user object has the role, FALSE if it does not.
function user_has_role($role, $user = NULL) {
  if (
$user == NULL) {

  if (
is_array($user->roles) && in_array($role, array_values($user->roles))) {


Please let me know if I put a typo up here or you know of an existing or easier way to do this!

New module idea... User Favorites

I've spent a couple hours today beginning to rebuild a website that I intended to offer up as a community activity tracking tool for the open source BASIC derivative language called FreeBASIC called the FB Tracker. Notes about the language itself (which rocks) aside, the site would be a fairly basic matter of allowing users to post and track projects, tutorials, snippets, sites, etc. through a single source. Most of this can be accomplished through some skillful use of Views + CCK, but there are a few community features I'll most likely be writing a module to implement. I have dubbed the module User Favorites and will post a few of my brainstorm ideas below...

I searched for a module similar to what I'm looking for and only found the Favorites module which is a far cry from what I need. The User Favorites module will actually allow users on a site to add content to a favorites list. It makes sense to give users the option of creating multiple favorites lists and will be fairly trivial to implement. Any of these lists may be set to be private or public lists. Users on large community or directory websites will be able to tag and track content through the site itself using their in site favorites list as well as provide feedback to the community regarding the value of content on the site.

Users will be able to browse the public favorites lists kept by other users as well as see who else on the site has favorited a specific piece of content. Favorites lists will be broken down by content type and sorted alphabetically, with users having the option to star a specific piece of content to make it sticky at the top of a list. Integration with Views means site developers will easily be able to create and sort lists of nodes ranked by how many times they've been favorited and/or starred. (Perhaps for the purposes of ranking, favorited content should count as 1 point while starred content should count as 3. Or rather... I can simply make that an administrative decision.) Integration with Userpoints means you can give users some love for providing feedback to other members of the community.

I'm happy to consider any further integrations with this module and am quite excited at the prospect of it being a first of its kind. However, if someone else knows of a module that may do something similar, please post it up here so I don't needlessly duplicate someone else's effort.

EDIT: A Tuesday morning, it's no longer past midnight, idea struck me on the way to work... I forgot to mention the idea of favoriting other users. This would allow you to track content posted by others and allow a ranking system for users similar to the one I mentioned above for content. It could be fun to use these statistics to display user relationships, user favorites compatibility (how much of your favorites match?), and other things like that. Maybe I can get Balazs to draw me up a user relationship graphic with his Drawing API. Lol

EDIT 2: Several folks have been posting up some very helpful pointers to modules that implement different parts of this idea. Let's just say... I'm never going to start a new module without doing a post like this first. This has been an invaluable learning experience, and hopefully I'll find some time to review the various modules and build on someone else's foundation.

Why use Drupal for e-commerce?

I recently posted a page to titled, Why use Drupal for e-commerce?. In it I made some observations about Drupal and how it is the perfect platform on which to base an e-commerce site. All the things we wish other e-commerce specific systems would do (or that we have to hack to get done) Drupal does out of the box. Feel free to give it a read and offer up any feedback here.

A brief summary of my reasons include:

  • It's got great search engine optimization (including easy SEF URLs with the path and pathauto modules).
  • It's nice and secure with an active security team. (I've even got an e-mail in the past with someone from the team pointing out a hole in my code who had never even used the module! Kudos to the security team at!)
  • It's a very powerful content management system, which is a must for future e-commerce sites and a great benefit to companies that market products supported by a niche market or particular community.
  • There are many folks out there (like me!) who are available to help and to work for you as you start to setup your Drupal + Ubercart site. Having the whole directory of contributed modules and themes is great.

Anyways, enough shameless promotion of Drupal and Ubercart for now. I'm just about to roll out the Alpha 7d release and need to do one final test on an update from a previous version to make sure I don't go breaking anyone's site with the release!