Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 530 Vote(s) - 3.4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get the currently logged in user's role in Drupal 7?

#1
How to get the currently logged in user's role in Drupal 7 ? Do you know a simple way of accomplish this ? Is there some drupal core functions for this?
Reply

#2
You can access to the user roles by just using this PHP snippet:

<?php $GLOBALS['user']->roles; ?>
Reply

#3
I have found a interesting solution to check for multiple roles of a user:

global $user;
$check = array_intersect(array('moderator', 'administrator'), array_values($user->roles));
if (empty($check) ? FALSE : TRUE) {
// is admin
} else {
// is not admin
}
Reply

#4
$user->roles is an array of the roles that belong to the user keyed by the role ID, value is the role string. So if you wanted to check if user had role 'authenticated user' your code snippet would look something like this (not necessarily the most optimized approach, in_array is a fairly performance-expensive function):

global $user;

if (in_array('authenticated user', $user->roles)) {
//do stuff here
}

Note that in_array can also accept an array as the "needle" (argument #1) so you could check against multiple role options:

in_array(array('authenticated user', 'anonymous user'), $user->roles);
Reply

#5
// Load the currently logged in user.



global $user;
print_r($user->roles);//this gives you current user roles

//to check whether he is administrator, you can do so by

if (in_array('administrator', $user->roles)) {
// do some stuff
$form['field_end_date']['#disabled'] = FALSE;
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through