I'd like to know if there is a way to determine the roles of a given user without using a customized SQL query for that. In the forum, the only thing I've found was $user->role (which worked for that topic starter who was using Drupal 4.7) but that doesn't do the trick for me.

Comments

nancydru’s picture

To get the name of the role, you would have to do a simple query.

Here's what I put in a block to get the role name:

<?php
  global $user;
  $role = db_result(db_query('SELECT r.name FROM {users_roles} ur LEFT JOIN {role} r ON r.rid=ur.rid WHERE ur.uid=%d LIMIT 1', $user->uid));
  print $role;
?>

If you just want the role id ("rid"), you can leave out the join stuff.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

xano’s picture

Hey Nancy,

I've already done this with a customized SQL query in the past, but now I've found this solution for 4.7 I was wondering if there was something similar for 5.x. Would be pretty stupid if there's a feature in 4.7 for this which doesn't exist in 5.x anymore.

Thanks for the tip anyway :-)

nancydru’s picture

I just looked at user.module and came up with this:

<?php
  global $user;
  print 'roles: '.implode(', ', $user->roles);
?>

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

xano’s picture

okaaaaaaaaaaay............. I'm pretty sure I tried print_r($user->roles) to test the variable, but no result showed itself on screen. Must have done something wrong. Thanks for giving me this piece of code. Now I know it was I who was wrong and it wasn't Drupal :-P

Jerimee’s picture

Q) but what does implode do?
A) it returns a string created by joining every element in an array with separator (in this case ', ')

Q) how do I modify this code to ignore the default authenticated role?
A) ???

nancydru’s picture

Jerimee’s picture

awesome

godman’s picture

Power Awesome

charlie-s’s picture

If you want to do a check on a users role when multiple roles may exist for that user, you can do it with in_array():

if(in_array('role_you_want_to_check_for', $user->roles)) {
     return true;
} else {
     return false;
}
zietbukuel’s picture

Thanks a lot, I've been looking for this :)

--
Juan Timaná

czeky’s picture

is it possible in similar way to check for role id? my roles doesn't have computer friendly names.. so I'd like to compare rol IDs.

thanx

jeni_dc’s picture

I'm a bit late here, but this works for me:

global $user;
if (array_key_exists('ROLE ID', $user->roles)) {
     return true;
} else {
     return false;
}
nancydru’s picture

Why not just use user_access() if you want to know if the current user has that permission.

AlastairG’s picture

They might not be checking for permissions. I found this page while looking to set dynamic product pricing based on a retailer's role. They all have permission to view the product page but they don't always get the same margin.

nirvale’s picture

hello

i am not php programer, and new in drupal of course, actually i'm ussing the pdf id card module, works fine, but i can't print the role user. i mean, i have many profiles with different roles to print.

the code is

$pdf->SetFont('freeserif','',8);
$pdf->setxy(53,5);
$pdf->Image($account->picture, 4,18, 33,40);
$pdf->Cell(100,33,strtoupper($account->profile_1nombre),0,0,"left",0,0);
$pdf->setxy(80,5);
$pdf->Cell(100,33,strtoupper($account->profile_2nombre),0,0,"right",0,0);
$pdf->setxy(53,5);
$pdf->Cell(100,43,strtoupper($account->profile_1apellido),0,0,"right",0,0);
$pdf->setxy(80,5);
$pdf->Cell(100,43,strtoupper($account->profile_2apellido),0,0,"right",0,0);
$pdf->setxy(53,5);
$pdf->Cell(100,63,strtoupper( THE MISSING CODE),0,0,"right",0,0);

Excuse my english, spanish is my native laguage ...

cmsproducer’s picture

As long as you have the UID (User ID) you want to get roles for, you can user the user_load function to get all the values attached to that UID, and then print_r the roles array to see what is in it... like this:

$user_current_uid = // insert the userID here;
$user_details = user_load($user_current_uid);
// output all the user roles arrays items
print_r($user_details->roles)
stefgosselin’s picture

user_load is a *very* expensive function call. Use sparingly. The other solution posted above does not require these and is probably much more efficient.

nancydru’s picture

User_load takes a different parameter in D6.

nancydru’s picture

I'm going to hazard a guess that $account (line 3) is the user object. $account->roles will have all the user's roles in it.

alesr’s picture

If you have PHP filter module enabled, you can make a block named "User roles" and put this in content:

<?php
global $user;
foreach($user->roles as $role) {
print "<div>".$role."</div>";
}
?>

Don't forget to select PHP code as a Text format!
This will print all roles of a curent user.
Works on D7. Use this for testing purposes.

adigunsherif’s picture

Why do you need to write all these codes when you can simply use views!

Views of type user. In d field, u choose user:name and user:role.

Voila! You have what you need. Isn't that very simple?