By xano on
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
Nope
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:
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
NancyDru
Hey Nancy, I've already done
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
I just looked at user.module and came up with this:
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
okaaaaaaaaaaay.............
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
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) ???
implode(', ',
implode(', ', array_slice($user->roles, 1))
http://us2.php.net/manual/en/function.array-slice.php
Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database
NancyDru
awesome
awesome
Power Awesome
Power Awesome
If you want to do a check on
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():
Thanks!
Thanks a lot, I've been looking for this :)
--
Juan Timaná
is it possible in similar way
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
Check Role ID
I'm a bit late here, but this works for me:
user_access()
Why not just use user_access() if you want to know if the current user has that permission.
NancyDru
Other Uses
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.
please help me
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
Excuse my english, spanish is my native laguage ...
user_load then list all the roles array items
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:
Just be aware ...
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.
Only D7
User_load takes a different parameter in D6.
NancyDru
Hmm...
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.
NancyDru
User role in a block
If you have PHP filter module enabled, you can make a block named "User roles" and put this in content:
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.
Why do you need to write all
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?