You are here

public function Security::hasPermission in Bamboo Twig 8

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

Does the current|given user has the given permission ?

Parameters

string $permission: Drupal permission string.

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

Return value

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

File

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

Class

Security
Provides a 'Security' Twig Extensions.

Namespace

Drupal\bamboo_twig_security\TwigExtension

Code

public function hasPermission($permission, $user = NULL) {
  if (is_null($user) && $this->currentUser
    ->isAnonymous()) {
    return NULL;
  }
  $user_id = $this->currentUser
    ->id();
  if (!is_null($user) && is_int($user)) {
    $user_id = $user;
  }
  $account = $this->userStorage
    ->load($user_id);
  if (!$account) {
    return NULL;
  }
  return $account
    ->hasPermission($permission);
}