You are here

protected function MenuPerRoleLinkTreeManipulator::isUserAdmin in Menu Per Role 8

Check if the current user is admin. Either due to uid 1 or admin roles.

Return value

bool TRUE if the user admin. FALSE otherwise.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to MenuPerRoleLinkTreeManipulator::isUserAdmin()
MenuPerRoleLinkTreeManipulator::bypassAccessCheck in src/MenuPerRoleLinkTreeManipulator.php
Check if the user can bypass the access check.

File

src/MenuPerRoleLinkTreeManipulator.php, line 154

Class

MenuPerRoleLinkTreeManipulator
Menu Per Role link tree manipulator service.

Namespace

Drupal\menu_per_role

Code

protected function isUserAdmin() : bool {
  if ($this->account
    ->id() == 1) {
    return TRUE;
  }

  // Get admin roles only one time.
  if (!$this->adminRoles) {

    /** @var \Drupal\user\RoleStorageInterface $role_storage */
    $role_storage = $this->entityTypeManager
      ->getStorage('user_role');

    /** @var string[] $admin_roles */
    $admin_roles = $role_storage
      ->getQuery()
      ->condition('is_admin', TRUE)
      ->execute();
    $this->adminRoles = $admin_roles;
  }
  $account_roles = $this->account
    ->getRoles(TRUE);
  foreach ($account_roles as $account_role) {
    if (in_array($account_role, $this->adminRoles)) {
      return TRUE;
    }
  }
  return FALSE;
}