function AccessResult::allowedIfHasPermission
Same name in other branches
- 8.9.x core/lib/Drupal/Core/Access/AccessResult.php \Drupal\Core\Access\AccessResult::allowedIfHasPermission()
- 10 core/lib/Drupal/Core/Access/AccessResult.php \Drupal\Core\Access\AccessResult::allowedIfHasPermission()
- 11.x core/lib/Drupal/Core/Access/AccessResult.php \Drupal\Core\Access\AccessResult::allowedIfHasPermission()
Creates an allowed access result if the permission is present, neutral otherwise.
Checks the permission and adds a 'user.permissions' cache context.
Parameters
\Drupal\Core\Session\AccountInterface $account: The account for which to check a permission.
string $permission: The permission to check for.
Return value
\Drupal\Core\Access\AccessResult If the account has the permission, isAllowed() will be TRUE, otherwise isNeutral() will be TRUE.
59 calls to AccessResult::allowedIfHasPermission()
- AccessResultTest::testCacheContexts in core/
tests/ Drupal/ Tests/ Core/ Access/ AccessResultTest.php - @covers ::addCacheContexts @covers ::resetCacheContexts @covers ::getCacheContexts @covers ::cachePerPermissions @covers ::cachePerUser @covers ::allowedIfHasPermission
- AggregatorFeedBlock::blockAccess in core/
modules/ aggregator/ src/ Plugin/ Block/ AggregatorFeedBlock.php - Indicates whether the block should be shown.
- aggregator_jsonapi_aggregator_feed_filter_access in core/
modules/ aggregator/ aggregator.module - Implements hook_jsonapi_ENTITY_TYPE_filter_access() for 'aggregator_feed'.
- BaseFieldOverrideAccessControlHandler::checkAccess in core/
lib/ Drupal/ Core/ Field/ BaseFieldOverrideAccessControlHandler.php - Performs access checks.
- BlockContentAccessControlHandler::checkAccess in core/
modules/ block_content/ src/ BlockContentAccessControlHandler.php - Performs access checks.
File
-
core/
lib/ Drupal/ Core/ Access/ AccessResult.php, line 114
Class
- AccessResult
- Value object for passing an access result with cacheability metadata.
Namespace
Drupal\Core\AccessCode
public static function allowedIfHasPermission(AccountInterface $account, $permission) {
$access_result = static::allowedIf($account->hasPermission($permission))
->addCacheContexts([
'user.permissions',
]);
if ($access_result instanceof AccessResultReasonInterface) {
$access_result->setReason("The '{$permission}' permission is required.");
}
return $access_result;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.