Hot to get user's roles

Xano - May 25, 2007 - 12:29

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 ht only thing I've found was $user->role (which worked for that topicstarter who was using Drupal 4.7) but that doesn't do the trick for me.

Nope

NancyDru - May 25, 2007 - 14:21

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

Hey Nancy, I've already done

Xano - May 25, 2007 - 14:46

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 :-)

Just did some poking around

NancyDru - May 25, 2007 - 15:20

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

okaaaaaaaaaaay.............

Xano - May 25, 2007 - 17:05

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

ok so this works

Jerimee - August 6, 2007 - 20:51

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) ???

awesome

Jerimee - August 8, 2007 - 01:38

awesome

If you want to do a check on

charlie1234 - July 14, 2009 - 01:05

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():

<?php
if(in_array('role_you_want_to_check_for', $user->roles)) {
     return
true;
} else {
     return
false;
}
?>

Thanks!

zietbukuel - September 6, 2009 - 04:48

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

is it possible in similar way

czeky - September 24, 2009 - 15:59

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

please help me

nirvale - November 2, 2009 - 08:09

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 ...

 
 

Drupal is a registered trademark of Dries Buytaert.