Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Drupal has two benefit functions:
1. user_is_anonymous
2. user_is_logged_in
Drupal 7 has merge module adminrole to core. I think Drupal should have function to check if user is admin role or not.
I suggest this code merge to core.
function user_is_admin() {
global $user;
if($user->uid == 1) {
return TRUE;
}
$rid = variable_get('user_admin_role');
if (isset($rid)) {
return isset($user->roles[$rid]);
}
}
Therefore, Drupal has three benefit functions:
1. user_is_anonymous
2. user_is_logged_in
3. user_is_admin
Comments
Comment #1
ijortengab CreditAttribution: ijortengab commentedComment #2
ankitadmane CreditAttribution: ankitadmane commentedIt 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.
Comment #3
dcam CreditAttribution: dcam commentedNew features must be added to the current development version first, which is Drupal 8.
I can't imagine that this will ever be added, however, because the admin role is not guaranteed to be available. It's created by the standard installation profile. It may not be installed or it might even get deleted by some admins. I'll let someone higher up decide whether or not to close this issue though.
Comment #4
ijortengab CreditAttribution: ijortengab as a volunteer commentedClosed wont fix, in favor of user_access('administer site configuratoin');
Comment #5
wombatbuddy CreditAttribution: wombatbuddy commentedDrupal 9
\Drupal::currentUser()->hasPermission('administer site configuration');
$user = \Drupal::entityTypeManager()->getStorage('user')->load(3);
$user->hasPermission('administer site configuration');