is there a way / variable to check if the logged in user is an admin or moderator or any kind of role?

I'd like to show the loginscreen only to admins/moderators...

Comments

cog.rusty’s picture

It is possible to check whether the logged-in user has a certain role. For example, something like this if you have a role called 'moderator'

global $user;
if (is_array($user->roles) && in_array('moderator', $user->roles)) {... do something ...}

However, this is useless for showing the login screen only to 'moderators', because if they are not already logged-in they are not moderators, they are just visitors.

jorre’s picture

thanks, this works out just perfect. there is a link to the login screen, I just don't want normal users to have a navigation menu available, only mods/admin

Pakfriet.be

sprite’s picture

If the user is logged in, they don't need a login screen.

If a visitor is not logged in, there isn't any way to know what role(s) they might belong to once logged in.

In any event, every hacker bot on planet earth knows that every drupal site's login pages are located at {yoursite}/user/login

spritefully yours
Technical assistance provided to the Drupal community on my own time ...
Thank yous appreciated ...

mos3abof’s picture

It is also possible to check so using the drupal built in user_access() function, for example the super administrator account doesn't have a role by default so you can check it using :

if(user_access('administer')) {... do something ...}

hope it might help somebody.

Note: I tried this with drupal version 5.x

wjaspers’s picture

And for Drupal 7.x, since the administrator role isn't included by default (i.e. we don't know what the RID or the role name will be) ...

if (user_access('administer site configuration')) {
// do something
}

谢艳’s picture

works like a charm!

captcha’s picture

Just what I was looking for, thanks

mikeaja’s picture

Thanks to cog.rusty and mos3abof for such clear, concise info and code.

This thread will be useful to many.

sri_techie’s picture

// Load the currently logged in user.
global $user;
if (in_array('administrator', $user->roles)) {
// do fancy stuff
$form['field_end_date']['#disabled'] = FALSE;
}

dks786’s picture

Hi All,

Where I need to write this code for check user role.
Do i need to write in any custom module with hook_init()?
or any other place?
Please suggest !

Selva.M’s picture

I have 100 users registered in Mysite and they are from different countries. They have also posted their Content.

When they logged in their account, how do I show if they are Online/Offline to other users ?

Any help much appreciated.

vm’s picture

The thread was generated under Durpal 4.7.x. If you are suing a core that old, upgrade. If you aren't, generate a new thread tagged with the version of Drupal in use.

الموسوعة العلمية’s picture

thanks, this works out just perfect. there is a link to the login screen, I just don't want normal users to have a navigation menu available, only mods/admin

captcha’s picture

Just what I was looking for, thanks

samantharaut’s picture

This thread is very useful, so I thought it'd be worth coming back here to update for Drupal 10. Based on the addition made here: https://www.drupal.org/node/3002289 which introduces hasRole()

// If current user is an administrator, do something
if (\Drupal::currentUser()->hasRole('administrator')) {
  // Do something
}

For my uses, I needed to hide a button for all users who were NOT administrators. This is what worked for me:

if (!\Drupal::currentUser()->hasRole('administrator')) {
  $form['actions']['revert']['#access'] = FALSE;
}
uberhacker’s picture

You probably don't want to exclude user 1 if the administrator role is not assigned.

if (\Drupal::currentUser()->id() == 1 || \Drupal::currentUser()->hasRole('administrator')) {
  // Do admin only stuff.
}