You are here

public function Security::hasRole in Bamboo Twig 8.5

Same name and namespace in other branches
  1. 8 bamboo_twig_security/src/TwigExtension/Security.php \Drupal\bamboo_twig_security\TwigExtension\Security::hasRole()
  2. 8.2 bamboo_twig_security/src/TwigExtension/Security.php \Drupal\bamboo_twig_security\TwigExtension\Security::hasRole()
  3. 8.3 bamboo_twig_security/src/TwigExtension/Security.php \Drupal\bamboo_twig_security\TwigExtension\Security::hasRole()
  4. 8.4 bamboo_twig_security/src/TwigExtension/Security.php \Drupal\bamboo_twig_security\TwigExtension\Security::hasRole()

Does the current|given user has the given role ?

Parameters

string $role: Drupal role name.

int $user: (Optional) user id to check role. Otherwise current user is used.

Return value

bool True if the current|given user has the given role. Otherwise FALSE.

File

bamboo_twig_security/src/TwigExtension/Security.php, line 118

Class

Security
Provides a 'Security' Twig Extensions.

Namespace

Drupal\bamboo_twig_security\TwigExtension

Code

public function hasRole($role, $user = NULL) {

  // Get the current user when $user is not provided.
  if (!$user) {
    $user = $this
      ->getCurrentUser()
      ->id();
  }
  $account = $this
    ->getUserStorage()
    ->load($user);

  // If given user do not exists or is anonymous - don't go further.
  if (!$account || $account
    ->isAnonymous()) {
    return NULL;
  }
  return $account
    ->hasRole($role);
}