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');
  • 1
    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
    Apr 14 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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