How can I check if a user has a certain role?
8 Answers
Checking for a permission is recommended over checking for a role. When you use permissions, you can assign that permission to multiple roles, which makes your system more flexible. Also, remember that roles can be renamed, which would break your code.
That said, if you want to check for a role, you can do this:
// The currently logged-in user is stored in a global variable.
global $user;
// Check if the user has the 'editor' role.
if (in_array('editor', $user->roles)) {
// do fancy stuff
}
-
1What if I want to check more than 1 role. e.g. If "editor" or "publisher" than do fancy stuff. How to code. Pls advise. Can't figure out really.– AdamtanMay 15, 2013 at 10:04
-
10Please read the answer: it's not recommended to check for a role (or more than one). Check for permissions instead. The rest of your question (combining two conditions in an if-statement) is basic PHP, nothing Drupal-related. Read the PHP documentation on logical operators. May 15, 2013 at 18:47
To check if the current user has a single role or any of multiple roles, a great way is to do:
//can be used in access callback too
function user_has_role($roles) {
//checks if user has role/roles
return !!count(array_intersect(is_array($roles)? $roles : array($roles), array_values($GLOBALS['user']->roles)));
};
if (user_has_role(array('moderator', 'administrator'))) {
// $user is admin or moderator
} else if(user_has_role('tester')){
// $user is tester
} else{
// $user is not admin and not moderator
}
-
1This function probably should be named differently as there's
user_has_role($rid)
in core already.– leymannxJan 5, 2021 at 20:37
Update for Drupal version >= 7.36
You can use function user_has_role from Drupal API https://api.drupal.org/api/drupal/modules%21user%21user.module/function/user_has_role/7.
Try this example:
<?php
function MYMODULE_foo() {
$role = user_role_load_by_name('Author');
if (user_has_role($role->rid)) {
// Code if user has 'Author' role...
}
else {
// Code if user doesn't have 'Author' role...
}
$user = user_load(123);
if(user_has_role($role->rid, $user)) {
// Code if user has 'Author' role...
}
else {
// Code if user doesn't have 'Author' role...
}
}
?>
Here's the actual code from the comment that is referred to in the accepted answer as the best practice
<?php
function mymodule_perm() {
return array('access something special');
}
function dosomethingspecial() {
// For current user
if (user_access('access something special')) {
// Doing something special!
}
// For a specific user
if (user_access('access something special', $theuser)) {
// Doing something special!
}
}
?>
To be futureproof in case the role name changes it's best to check for the role id (rid) which can be found in the role table in the database.
If you want to check for a role with rid 16, do:
// Load the currently logged in user.
global $user;
// Check if the user has the 'editor' role, when 'editor' has role id 16
if (array_key_exists(16, $user->roles)) {
// do fancy stuff
}
-
2Actually, when you migrate things like roles, machine name will probably be kept, and ID will not– MołotDec 1, 2014 at 19:01
-
There is no machine name just a rid and a human name in the role table. The users_roles table assigns a uid to a rid. Dec 2, 2014 at 10:54
-
The problem with using rolenames is that it is possible the might change in the future (it is also easy to change their names) and then you will have to go back into your custom code to replace all the names otherwise the things you want to hide or show will not work anymore. If you use the role id you are safe and can change role names without having to check your custom code. Jan 19, 2021 at 9:18
There's user_has_role()
which accepts a role ID. So you could first get a role ID from a role name and then pass that to user_has_role()
.
$role = user_role_load_by_name('My Role');
$role_id = $role ? $role->rid : NULL;
global $user;
if ($role_id && user_has_role($role_id, $user)) {
// Yes, this user has this role.
}
The other answers didn't work; I am not sure why. I used the following code.
global $user;
// Check to see if $user has the administrator role.
if (in_array('administrator', array_values($user->roles))) {
// Do something.
}
A helper function could look like this.
/**
* Check to see if a user has been assigned a certain role.
*
* @param $role
* The name of the role you're trying to find.
* @param $account
* The account to check; if not given, use the currently logged-in user.
*
* @return
* TRUE if the user object has the role.
*/
function _user_has_role($role, $account = NULL) {
global $user;
if (!isset($account)) {
$account = $user;
}
return (is_array($account->roles) && in_array($role, array_values($account->roles));
}
-
in_array()
checks the value passed as first argument is a value for the array passed as second argument. There is no need to usearray_values()
.– apaderno ♦Oct 11, 2022 at 21:24
to check if a user has a certain role:
function test_role(){
global $user;
if(isset($user->roles['my_role'])){
return true;
}
else {
return false;
}
}