AccessResult::allowedIfHasPermissions

public static function 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 124

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']);
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Access!AccessResult.php/function/AccessResult::allowedIfHasPermissions/8.1.x

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部