function UserSession::getRoles

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Session/UserSession.php \Drupal\Core\Session\UserSession::getRoles()
  2. 8.9.x core/lib/Drupal/Core/Session/UserSession.php \Drupal\Core\Session\UserSession::getRoles()
  3. 11.x core/lib/Drupal/Core/Session/UserSession.php \Drupal\Core\Session\UserSession::getRoles()

Returns a list of roles.

Parameters

bool $exclude_locked_roles: (optional) If TRUE, locked roles (anonymous/authenticated) are not returned.

Return value

array List of role IDs.

Overrides AccountInterface::getRoles

1 call to UserSession::getRoles()
UserSession::hasRole in core/lib/Drupal/Core/Session/UserSession.php
Whether a user has a certain role.

File

core/lib/Drupal/Core/Session/UserSession.php, line 95

Class

UserSession
An implementation of the user account interface for the global user.

Namespace

Drupal\Core\Session

Code

public function getRoles($exclude_locked_roles = FALSE) {
  $roles = $this->roles;
  if ($exclude_locked_roles) {
    $roles = array_values(array_diff($roles, [
      AccountInterface::ANONYMOUS_ROLE,
      AccountInterface::AUTHENTICATED_ROLE,
    ]));
  }
  return $roles;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.