Drupal 7 – Check user has role

To check if a user has a specific role like the authenticated user, the following code could help.

global $user;
if (in_array('authenticated user', $user->roles)) {
  // This user has 'authenticated user' role
}


 

If you wanna check whether the user has any one of the multiple roles, use the following instead

global $user;
$has_role = array_intersect(array('<role A>', '<role B>', '<role C>'), array_values($user->roles));
if (empty($has_role) ? FALSE : TRUE) {
  // This user has any one of the three roles
}

 

Done =)

Reference: StackOverflow – How to get the currently logged in user’s role in Drupal 7?

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.