see the title

Comments

Steven’s picture

Use $user->rid for the role ID, or $user->role for the role name. Be sure to declare $user as global.

alexis’s picture

Hi, I'm using Drupal 4.5.2 with PHPTemplate and $user->role did not work for me.

I have users with multiples roles and used something like this to see who could edit some node ("administrador" is a role that I created):

global $user
foreach ($user->roles as $role) {
		if ($role == "administrador") {
			$canEdit = 1;
			break;
		}
	}

Alexis Bellido - Ventanazul web solutions

gncuster’s picture

Thanks so much for your help!

Steven’s picture

Yes, drupal has added support for multiple user roles since the original post was made.

--
If you have a problem, please search before posting a question.

gncuster’s picture

I am trying this same thing in a block, and using the example code. The $role is not ever getting filled. My code:

global $user;

$role = $user->role;
return t($role);

if (($role == "administrator") || ($role == "authenticated user") || ($role == "Historian")) {
return t('<a href="/forum/">Goto the Forum</a><br><a href="/image/">View Pictures</a><br><a href="/node/add/blog">Write a Blog</a><br><a href="/node/add/event">Post an event</a>');
} else {
return t('<a href="/image/">View Pictures</a><br><a href="/node/add/blog">Write a Blog</a><br><a href="/node/add/event">Post an event</a>');
}
jdlilly’s picture

I'm seeing the same issue:

global $user;
global $r = $user->role;

print $user;
print $r;

This prints "Object" for the user (not correct) and prints nothing for the role. Yet all the messages indicate the way to get role is $user->role.
Thanks for any ideas.

crunchywelch’s picture

global $user;
print_r($user);

this will get you farther than print()....

jdlilly’s picture

OK, thanks. Sorry, I'm new at this.

I did print_r($user) and I got all of the details about the user. There wasn't a role value but there was a roles value. After some tinkering, I found this to work:

i f ($user->roles[2]) // I know [2] corresponds to the admin role
do Admin stuff
if ($user->roles[3]) // I know [3] corresponds to the designer role
do Designer stuff

Thanks again.

rwreed’s picture

since roles is an array you might find this a more helpful way to use it.

if (in_array('administrator',$user->roles)) {
stuff goes here;
}

of course administrator may be replaced by any role.
hope that helps
Randy

travischristopher’s picture

answered my own question

this page is pretty darn useful for themers
http://drupal.org/node/45774