You are here

public static function AccessResult::allowedIfHasPermissions in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Access/AccessResult.php \Drupal\Core\Access\AccessResult::allowedIfHasPermissions()

Creates an allowed access result if the permissions are present, neutral otherwise.

Checks the permission and adds a 'user.permissions' cache contexts.

Parameters

\Drupal\Core\Session\AccountInterface $account: The account for which to check permissions.

array $permissions: The permissions to check.

string $conjunction: (optional) 'AND' if all permissions are required, 'OR' in case just one. Defaults to 'AND'

Return value

\Drupal\Core\Access\AccessResult If the account has the permissions, isAllowed() will be TRUE, otherwise isNeutral() will be TRUE.

4 calls to AccessResult::allowedIfHasPermissions()
AccessResultTest::testAllowedIfHasPermissions in core/tests/Drupal/Tests/Core/Access/AccessResultTest.php
Tests allowedIfHasPermissions().
PermissionAccessCheck::access in core/modules/user/src/Access/PermissionAccessCheck.php
Checks access.
ShortcutSetAccessControlHandler::checkCreateAccess in core/modules/shortcut/src/ShortcutSetAccessControlHandler.php
Performs create access checks.
TermAccessControlHandler::checkAccess in core/modules/taxonomy/src/TermAccessControlHandler.php
Performs access checks.

File

core/lib/Drupal/Core/Access/AccessResult.php, line 128
Contains \Drupal\Core\Access\AccessResult.

Class

AccessResult
Value object for passing an access result with cacheability metadata.

Namespace

Drupal\Core\Access

Code

public static function allowedIfHasPermissions(AccountInterface $account, array $permissions, $conjunction = 'AND') {
  $access = FALSE;
  if ($conjunction == 'AND' && !empty($permissions)) {
    $access = TRUE;
    foreach ($permissions as $permission) {
      if (!($permission_access = $account
        ->hasPermission($permission))) {
        $access = FALSE;
        break;
      }
    }
  }
  else {
    foreach ($permissions as $permission) {
      if ($permission_access = $account
        ->hasPermission($permission)) {
        $access = TRUE;
        break;
      }
    }
  }
  return static::allowedIf($access)
    ->addCacheContexts(empty($permissions) ? [] : [
    'user.permissions',
  ]);
}