class AccessResultForbidden extends AccessResult implements AccessResultReasonInterface (View source)

Value object indicating a forbidden access result, with cacheability metadata.

Traits

Trait for \Drupal\Core\Cache\RefinableCacheableDependencyInterface.

Trait for \Drupal\Core\Cache\CacheableDependencyInterface.

Properties

protected string[] $cacheContexts

Cache contexts.

from  CacheableDependencyTrait
protected string[] $cacheTags

Cache tags.

from  CacheableDependencyTrait
protected int $cacheMaxAge

Cache max-age.

from  CacheableDependencyTrait
protected string $reason

The reason why access is forbidden. For use in error messages.

Methods

$this
setCacheability(CacheableDependencyInterface $cacheability)

Sets cacheability; useful for value object constructors.

getCacheTags()

{@inheritdoc}

addCacheableDependency($other_object)

{@inheritdoc}

addCacheContexts(array $cache_contexts)

{@inheritdoc}

addCacheTags(array $cache_tags)

{@inheritdoc}

neutral(string|null $reason = NULL)

Creates an AccessResultInterface object with isNeutral() === TRUE.

allowed()

Creates an AccessResultInterface object with isAllowed() === TRUE.

forbidden(string|null $reason = NULL)

Creates an AccessResultInterface object with isForbidden() === TRUE.

static AccessResult
allowedIf(bool $condition)

Creates an allowed or neutral access result.

static AccessResult
forbiddenIf(bool $condition, string|null $reason = NULL)

Creates a forbidden or neutral access result.

static AccessResult
allowedIfHasPermission(AccountInterface $account, string $permission)

Creates an allowed access result if the permission is present, neutral otherwise.

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

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

bool
isAllowed()

Checks whether this access result indicates access is explicitly allowed.

bool
isForbidden()

Checks whether this access result indicates access is explicitly forbidden.

bool
isNeutral()

Checks whether this access result indicates access is not yet determined.

$this
resetCacheContexts()

Resets cache contexts (to the empty array).

$this
resetCacheTags()

Resets cache tags (to the empty array).

$this
setCacheMaxAge(int $max_age)

Sets the maximum age for which this access result may be cached.

$this
cachePerPermissions()

Convenience method, adds the "user.permissions" cache context.

$this
cachePerUser()

Convenience method, adds the "user" cache context.

orIf(AccessResultInterface $other)

Combine this access result with another using OR.

andIf(AccessResultInterface $other)

Combine this access result with another using AND.

$this
inheritCacheability(AccessResultInterface $other)

Inherits the cacheability of the other access result, if any.

__construct(null|string $reason = NULL)

Constructs a new AccessResultForbidden instance.

string
getReason()

Gets the reason for this access result.

setReason($reason)

Sets the reason for this access result.

Details

protected $this setCacheability(CacheableDependencyInterface $cacheability)

Sets cacheability; useful for value object constructors.

Parameters

CacheableDependencyInterface $cacheability

The cacheability to set.

Return Value

$this

getCacheTags()

{@inheritdoc}

getCacheContexts()

{@inheritdoc}

getCacheMaxAge()

{@inheritdoc}

addCacheableDependency($other_object)

{@inheritdoc}

Parameters

$other_object

addCacheContexts(array $cache_contexts)

{@inheritdoc}

Parameters

array $cache_contexts

addCacheTags(array $cache_tags)

{@inheritdoc}

Parameters

array $cache_tags

mergeCacheMaxAge($max_age)

{@inheritdoc}

Parameters

$max_age

static AccessResultNeutral neutral(string|null $reason = NULL)

Creates an AccessResultInterface object with isNeutral() === TRUE.

Parameters

string|null $reason

(optional) The reason why access is neutral. Intended for developers, hence not translatable.

Return Value

AccessResultNeutral

isNeutral() will be TRUE.

static AccessResultAllowed allowed()

Creates an AccessResultInterface object with isAllowed() === TRUE.

Return Value

AccessResultAllowed

isAllowed() will be TRUE.

static AccessResultForbidden forbidden(string|null $reason = NULL)

Creates an AccessResultInterface object with isForbidden() === TRUE.

Parameters

string|null $reason

(optional) The reason why access is forbidden. Intended for developers, hence not translatable.

Return Value

AccessResultForbidden

isForbidden() will be TRUE.

static AccessResult allowedIf(bool $condition)

Creates an allowed or neutral access result.

Parameters

bool $condition

The condition to evaluate.

Return Value

AccessResult

If $condition is TRUE, isAllowed() will be TRUE, otherwise isNeutral() will be TRUE.

static AccessResult forbiddenIf(bool $condition, string|null $reason = NULL)

Creates a forbidden or neutral access result.

Parameters

bool $condition

The condition to evaluate.

string|null $reason

(optional) The reason why access is forbidden. Intended for developers, hence not translatable

Return Value

AccessResult

If $condition is TRUE, isForbidden() will be TRUE, otherwise isNeutral() will be TRUE.

static AccessResult allowedIfHasPermission(AccountInterface $account, string $permission)

Creates an allowed access result if the permission is present, neutral otherwise.

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

Parameters

AccountInterface $account

The account for which to check a permission.

string $permission

The permission to check for.

Return Value

AccessResult

If the account has the permission, isAllowed() will be TRUE, otherwise isNeutral() will be TRUE.

static AccessResult allowedIfHasPermissions(AccountInterface $account, array $permissions, string $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

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

AccessResult

If the account has the permissions, isAllowed() will be TRUE, otherwise isNeutral() will be TRUE.

bool isAllowed()

Checks whether this access result indicates access is explicitly allowed.

Return Value

bool

When TRUE then isForbidden() and isNeutral() are FALSE.

See also

AccessResultAllowed

bool isForbidden()

Checks whether this access result indicates access is explicitly forbidden.

This is a kill switch — both orIf() and andIf() will result in isForbidden() if either results are isForbidden().

Return Value

bool

When TRUE then isAllowed() and isNeutral() are FALSE.

bool isNeutral()

Checks whether this access result indicates access is not yet determined.

Return Value

bool

When TRUE then isAllowed() and isForbidden() are FALSE.

See also

AccessResultNeutral

$this resetCacheContexts()

Resets cache contexts (to the empty array).

Return Value

$this

$this resetCacheTags()

Resets cache tags (to the empty array).

Return Value

$this

$this setCacheMaxAge(int $max_age)

Sets the maximum age for which this access result may be cached.

Parameters

int $max_age

The maximum time in seconds that this access result may be cached.

Return Value

$this

$this cachePerPermissions()

Convenience method, adds the "user.permissions" cache context.

Return Value

$this

$this cachePerUser()

Convenience method, adds the "user" cache context.

Return Value

$this

AccessResultInterface orIf(AccessResultInterface $other)

Combine this access result with another using OR.

When ORing two access results, the result is:

  • isForbidden() in either ⇒ isForbidden()
  • otherwise if isAllowed() in either ⇒ isAllowed()
  • otherwise both must be isNeutral() ⇒ isNeutral()

Truth table:

Parameters

AccessResultInterface $other

The other access result to OR this one with.

Return Value

AccessResultInterface

AccessResultInterface andIf(AccessResultInterface $other)

Combine this access result with another using AND.

When ANDing two access results, the result is:

  • isForbidden() in either ⇒ isForbidden()
  • otherwise, if isAllowed() in both ⇒ isAllowed()
  • otherwise, one of them is isNeutral() ⇒ isNeutral()

Truth table:

Parameters

AccessResultInterface $other

The other access result to AND this one with.

Return Value

AccessResultInterface

$this inheritCacheability(AccessResultInterface $other)

Inherits the cacheability of the other access result, if any.

This method differs from addCacheableDependency() in how it handles max-age, because it is designed to inherit the cacheability of the second operand in the andIf() and orIf() operations. There, the situation "allowed, max-age=0 OR allowed, max-age=1000" needs to yield max-age 1000 as the end result.

Parameters

AccessResultInterface $other

The other access result, whose cacheability (if any) to inherit.

Return Value

$this

__construct(null|string $reason = NULL)

Constructs a new AccessResultForbidden instance.

Parameters

null|string $reason

(optional) A message to provide details about this access result.

string getReason()

Gets the reason for this access result.

Return Value

string

The reason of this access result or an empty string if no reason is provided.

AccessResultInterface setReason($reason)

Sets the reason for this access result.

Parameters

$reason

string|null The reason of this access result or NULL if no reason is provided.

Return Value

AccessResultInterface

The access result instance.