AccessResult::allowedIfHasPermissions

public static AccessResult::allowedIfHasPermissions(AccountInterface $account, array $permissions, $conjunction = 'AND')

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.

File

core/lib/Drupal/Core/Access/AccessResult.php, line 129

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']);
}
doc_Drupal
2016-10-29 08:42:29
Comments
Leave a Comment

Please login to continue.