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; ?>
<?php global $user; print 'roles: '.implode(', ', $user->roles); ?>
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.
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.
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.
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
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.
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.
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:
<?php
$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)
?>
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.
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.
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.
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.
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
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.
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.
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:
<?php
$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)
?>
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.
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:
<?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
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 :-)
My name is Bart - web & events
Just did some poking around
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
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
My name is Bart - web & events
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á
www.planleft.com
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
$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 ...
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:
-----
Donny Nyamweya - cmsproducer.com
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:
<?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.
Hot to get user's roles
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.
- Log in or register to post comments
-
⋅ Categories: Drupal 5.xComments
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:
<?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
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 :-)
My name is Bart - web & events
Just did some poking around
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
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
My name is Bart - web & events
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á
www.planleft.com
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
$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 ...
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:
-----
Donny Nyamweya - cmsproducer.com
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:
<?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.