Get role from UID

joachim - January 22, 2008 - 09:46

What's the best way to get user roles for a given UID -- or rather, check the given UID has a particular role?

I want to check something in a theme, based on whether the node's author belongs to a role.

You could do a user load ...

mdixoncm - January 22, 2008 - 13:49

You could do a user load to get the user object, the roles will be available in the object - for example

$account = user_load(array('uid'=>$uid));
print_r($account->roles);

But - it is generally bad form to check roles explicitly - instead you should really check for a particular permission, and then assign that permission to the role in question, this then allows much greater flexibility and makes your code more portable

$account=user_load(array('uid'=>$uid));
if (user_access('administer site configuration',$account)){
  //do your stuff here
}

The above will check to see if the user has the "administer site configuration" permission ...

Mike,
Computerminds offer Drupal development, consulting and training

 
 

Drupal is a registered trademark of Dries Buytaert.