How can I get the roles for the currently logged-in user, in Drupal 8?

In Drupal 7, it could be achieved by accessing $GLOBALS['user']->roles;.

1 Answer 1


Something like:

$current_user = \Drupal::currentUser();
$roles = $current_user->getRoles();

will return an array like:

  [0] => authenticated
  [1] => administrator
  [2] => some_other_role

where the array values are role IDs (equivalent to the machine name of the role in Drupal 7). In OO code, use the appropriate mechanism get the current user from the container, eg

$current_user = $container->get('current_user');
  • 3
    To check if a user is at least one of multiple given roles: if (!empty(array_intersect(['administrator', 'some_other_role'], \Drupal::currentUser()->getRoles()))) { ....
    – leymannx
    Commented Apr 14, 2022 at 8:29
  • That approach worked for me perfectly. Thanks @leymannx !
    – betarobot
    Commented Dec 2, 2022 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.