class Gate implements Gate

Traits

Properties

protected array $stringCallbacks

All of the defined abilities using class@method notation.

protected Response|null $defaultDenialResponse

The default denial response for gates and policies.

protected null|callable $guessPolicyNamesUsingCallback

The callback to be used to guess policy names.

protected null|ArrayObject<string, class-string|false> $policyClassCache

The policy classes resolved for this gate's policy configuration.

static protected array<string, bool> $guestMethodCache

Cached guest-access results for class methods.

static protected null|WeakMap<object, bool> $guestCallbackCache

Cached guest-access results for object callables.

static protected array<string, string> $abilityMethodCache

Cached ability name to method name mappings.

Methods

allow(string|null $message = null, int|string|null $code = null)

Create a new access response.

deny(string|null $message = null, int|string|null $code = null)

Throw an unauthorized exception.

denyWithStatus(int $status, string|null $message = null, int|string|null $code = null)

Deny with a HTTP status code.

denyAsNotFound(string|null $message = null, int|string|null $code = null)

Deny with a 404 HTTP status code.

__construct(Container $container, Closure $userResolver, array $abilities = [], array $policies = [], array $beforeCallbacks = [], array $afterCallbacks = [], callable|null $guessPolicyNamesUsingCallback = null)

Create a new gate instance.

bool
has(array|UnitEnum|string $ability)

Determine if a given ability has been defined.

allowIf(mixed $condition, string|null $message = null, int|string|null $code = null)

Perform an on-demand authorization check. Throw an authorization exception if the condition or callback is false.

denyIf(mixed $condition, string|null $message = null, int|string|null $code = null)

Perform an on-demand authorization check. Throw an authorization exception if the condition or callback is true.

authorizeOnDemand(mixed $condition, string|null $message, int|string|null $code, bool $allowWhenResponseIs)

Authorize a given condition or callback.

define(UnitEnum|string $ability, array|callable|string $callback)

Define a new ability.

resource(string $name, string $class, array|null $abilities = null)

Define abilities for a resource.

buildAbilityCallback(string $ability, string $callback)

Create the ability callback for a callback string.

policy(string $class, string $policy)

Define a policy class for a given class type.

before(callable $callback)

Register a callback to run before all Gate checks.

after(callable $callback)

Register a callback to run after all Gate checks.

bool
allows(iterable|UnitEnum|string $ability, mixed $arguments = [])

Determine if all of the given abilities should be granted for the current user.

bool
denies(iterable|UnitEnum|string $ability, mixed $arguments = [])

Determine if any of the given abilities should be denied for the current user.

bool
check(iterable|UnitEnum|string $abilities, mixed $arguments = [])

Determine if all of the given abilities should be granted for the current user.

bool
any(iterable|UnitEnum|string $abilities, mixed $arguments = [])

Determine if any one of the given abilities should be granted for the current user.

bool
none(iterable|UnitEnum|string $abilities, mixed $arguments = [])

Determine if all of the given abilities should be denied for the current user.

authorize(UnitEnum|string $ability, mixed $arguments = [])

Determine if the given ability should be granted for the current user.

inspect(UnitEnum|string $ability, mixed $arguments = [])

Inspect the user for the given ability.

mixed
raw(string $ability, mixed $arguments = [])

Get the raw result from the authorization callback.

bool
canBeCalledWithUser(mixed $user, mixed $class, string|null $method = null)

Determine whether the callback/method can be called with the given user.

bool
methodAllowsGuests(mixed $class, string $method)

Determine if the given class method allows guests.

bool
callbackAllowsGuests(callable $callback)

Determine if the callback allows guests.

bool
parameterAllowsGuests(ReflectionParameter $parameter)

Determine if the given parameter allows guests.

bool|Response|null
callAuthCallback(mixed $user, string $ability, array $arguments)

Resolve and call the appropriate authorization callback.

mixed
callBeforeCallbacks(mixed $user, string $ability, array $arguments)

Call all of the before callbacks and return if a result is given.

bool|Response|null
callAfterCallbacks(mixed $user, string $ability, array $arguments, bool|Response|null $result)

Call all of the after callbacks with check result.

void
dispatchGateEvaluatedEvent(mixed $user, string $ability, array $arguments, bool|Response|null $result)

Dispatch a gate evaluation event.

callable
resolveAuthCallback(mixed $user, string $ability, array $arguments)

Resolve the callable for the given ability and arguments.

mixed
getPolicyFor(object|string $class)

Get a policy instance for a given class.

string|null
getPolicyFromAttribute(string $class, bool $includeParents = false)

Get the policy class from the UsePolicy attribute.

array
guessPolicyName(string $class)

Guess the policy name for the given class.

guessPolicyNamesUsing(callable $callback)

Specify a callback to be used to guess policy names.

mixed
resolvePolicy(object|string $class)

Build a policy class instance of the given type.

bool|callable
resolvePolicyCallback(mixed $user, string $ability, array $arguments, mixed $policy)

Resolve the callback for a policy check.

mixed
callPolicyBefore(mixed $policy, mixed $user, string $ability, array $arguments)

Call the "before" method on the given policy, if applicable.

mixed
callPolicyMethod(mixed $policy, string $method, mixed $user, array $arguments)

Call the appropriate method on the given policy.

string
formatAbilityToMethod(string $ability)

Format the policy ability into a method name.

forUser(mixed $user)

Get a gate instance for the given user.

mixed
resolveUser()

Resolve the user from the user resolver.

array
abilities()

Get all of the defined abilities.

array
policies()

Get all of the defined policies.

defaultDenialResponse(Response $response)

Set the default denial response for gates and policies.

setContainer(Container $container)

Set the container instance used by the gate.

scope(UnitEnum|string $ability, Builder $query)

Apply the policy's scope method to filter a query to authorized rows.

select(UnitEnum|string $ability, Builder|Model|string $query)

Get a scalar boolean selection from the policy for per-row authorization.

array
resolveQueryPolicy(string $ability, Builder $query, string $nativeMethod, string $fallbackMethod)

Resolve the policy method and authorization state for a query operation.

denyQuery(Builder $query)

Deny every row on the given query.

void
callPolicyScope(mixed $policy, string $method, mixed $user, Builder $query)

Apply a policy scope to the given query.

callPolicySelection(mixed $policy, string $method, mixed $user, Builder $query)

Get a scalar boolean selection from a policy method.

buildScopeSelection(string $ability, mixed $policy, string $method, mixed $user, Builder $query)

Build a correlated scalar selection from a policy scope.

static void
flushState()

Flush all static state.

Details

protected Response allow(string|null $message = null, int|string|null $code = null)

Create a new access response.

Parameters

string|null $message
int|string|null $code

Return Value

Response

protected Response deny(string|null $message = null, int|string|null $code = null)

Throw an unauthorized exception.

Parameters

string|null $message
int|string|null $code

Return Value

Response

Response denyWithStatus(int $status, string|null $message = null, int|string|null $code = null)

Deny with a HTTP status code.

Parameters

int $status
string|null $message
int|string|null $code

Return Value

Response

Response denyAsNotFound(string|null $message = null, int|string|null $code = null)

Deny with a 404 HTTP status code.

Parameters

string|null $message
int|string|null $code

Return Value

Response

at line 103
__construct(Container $container, Closure $userResolver, array $abilities = [], array $policies = [], array $beforeCallbacks = [], array $afterCallbacks = [], callable|null $guessPolicyNamesUsingCallback = null)

Create a new gate instance.

Parameters

Container $container
Closure $userResolver
array $abilities
array $policies
array $beforeCallbacks
array $afterCallbacks
callable|null $guessPolicyNamesUsingCallback

at line 118
bool has(array|UnitEnum|string $ability)

Determine if a given ability has been defined.

Parameters

array|UnitEnum|string $ability

Return Value

bool

at line 137
Response allowIf(mixed $condition, string|null $message = null, int|string|null $code = null)

Perform an on-demand authorization check. Throw an authorization exception if the condition or callback is false.

Parameters

mixed $condition
string|null $message
int|string|null $code

Return Value

Response

Exceptions

AuthorizationException

at line 147
Response denyIf(mixed $condition, string|null $message = null, int|string|null $code = null)

Perform an on-demand authorization check. Throw an authorization exception if the condition or callback is true.

Parameters

mixed $condition
string|null $message
int|string|null $code

Return Value

Response

Exceptions

AuthorizationException

at line 157
protected Response authorizeOnDemand(mixed $condition, string|null $message, int|string|null $code, bool $allowWhenResponseIs)

Authorize a given condition or callback.

Parameters

mixed $condition
string|null $message
int|string|null $code
bool $allowWhenResponseIs

Return Value

Response

Exceptions

AuthorizationException

at line 184
Gate define(UnitEnum|string $ability, array|callable|string $callback)

Define a new ability.

Boot-only. The definition persists on the singleton Gate for the worker lifetime and affects authorization in every subsequent request.

Parameters

UnitEnum|string $ability
array|callable|string $callback

Return Value

Gate

Exceptions

InvalidArgumentException

at line 211
Gate resource(string $name, string $class, array|null $abilities = null)

Define abilities for a resource.

Boot-only. The definitions persist on the singleton Gate for the worker lifetime and affect authorization in every subsequent request.

Parameters

string $name
string $class
array|null $abilities

Return Value

Gate

at line 231
protected Closure buildAbilityCallback(string $ability, string $callback)

Create the ability callback for a callback string.

Parameters

string $ability
string $callback

Return Value

Closure

at line 269
Gate policy(string $class, string $policy)

Define a policy class for a given class type.

Boot-only. The policy persists on the singleton Gate for the worker lifetime and affects authorization in every subsequent request.

Parameters

string $class
string $policy

Return Value

Gate

at line 286
Gate before(callable $callback)

Register a callback to run before all Gate checks.

Boot-only. The callback persists on the singleton Gate for the worker lifetime and runs during authorization in every subsequent request.

Parameters

callable $callback

Return Value

Gate

at line 299
Gate after(callable $callback)

Register a callback to run after all Gate checks.

Boot-only. The callback persists on the singleton Gate for the worker lifetime and runs during authorization in every subsequent request.

Parameters

callable $callback

Return Value

Gate

at line 309
bool allows(iterable|UnitEnum|string $ability, mixed $arguments = [])

Determine if all of the given abilities should be granted for the current user.

Parameters

iterable|UnitEnum|string $ability
mixed $arguments

Return Value

bool

at line 317
bool denies(iterable|UnitEnum|string $ability, mixed $arguments = [])

Determine if any of the given abilities should be denied for the current user.

Parameters

iterable|UnitEnum|string $ability
mixed $arguments

Return Value

bool

at line 325
bool check(iterable|UnitEnum|string $abilities, mixed $arguments = [])

Determine if all of the given abilities should be granted for the current user.

Parameters

iterable|UnitEnum|string $abilities
mixed $arguments

Return Value

bool

at line 339
bool any(iterable|UnitEnum|string $abilities, mixed $arguments = [])

Determine if any one of the given abilities should be granted for the current user.

Parameters

iterable|UnitEnum|string $abilities
mixed $arguments

Return Value

bool

at line 353
bool none(iterable|UnitEnum|string $abilities, mixed $arguments = [])

Determine if all of the given abilities should be denied for the current user.

Parameters

iterable|UnitEnum|string $abilities
mixed $arguments

Return Value

bool

at line 363
Response authorize(UnitEnum|string $ability, mixed $arguments = [])

Determine if the given ability should be granted for the current user.

Parameters

UnitEnum|string $ability
mixed $arguments

Return Value

Response

Exceptions

AuthorizationException

at line 371
Response inspect(UnitEnum|string $ability, mixed $arguments = [])

Inspect the user for the given ability.

Parameters

UnitEnum|string $ability
mixed $arguments

Return Value

Response

at line 393
mixed raw(string $ability, mixed $arguments = [])

Get the raw result from the authorization callback.

Parameters

string $ability
mixed $arguments

Return Value

mixed

Exceptions

AuthorizationException

at line 428
protected bool canBeCalledWithUser(mixed $user, mixed $class, string|null $method = null)

Determine whether the callback/method can be called with the given user.

Parameters

mixed $user
mixed $class
string|null $method

Return Value

bool

at line 450
protected bool methodAllowsGuests(mixed $class, string $method)

Determine if the given class method allows guests.

Parameters

mixed $class
string $method

Return Value

bool

at line 479
protected bool callbackAllowsGuests(callable $callback)

Determine if the callback allows guests.

Parameters

callable $callback

Return Value

bool

at line 505
protected bool parameterAllowsGuests(ReflectionParameter $parameter)

Determine if the given parameter allows guests.

Parameters

ReflectionParameter $parameter

Return Value

bool

at line 514
protected bool|Response|null callAuthCallback(mixed $user, string $ability, array $arguments)

Resolve and call the appropriate authorization callback.

Parameters

mixed $user
string $ability
array $arguments

Return Value

bool|Response|null

at line 524
protected mixed callBeforeCallbacks(mixed $user, string $ability, array $arguments)

Call all of the before callbacks and return if a result is given.

Parameters

mixed $user
string $ability
array $arguments

Return Value

mixed

at line 542
protected bool|Response|null callAfterCallbacks(mixed $user, string $ability, array $arguments, bool|Response|null $result)

Call all of the after callbacks with check result.

Parameters

mixed $user
string $ability
array $arguments
bool|Response|null $result

Return Value

bool|Response|null

at line 560
protected void dispatchGateEvaluatedEvent(mixed $user, string $ability, array $arguments, bool|Response|null $result)

Dispatch a gate evaluation event.

Parameters

mixed $user
string $ability
array $arguments
bool|Response|null $result

Return Value

void

at line 576
protected callable resolveAuthCallback(mixed $user, string $ability, array $arguments)

Resolve the callable for the given ability and arguments.

Parameters

mixed $user
string $ability
array $arguments

Return Value

callable

at line 603
mixed getPolicyFor(object|string $class)

Get a policy instance for a given class.

Parameters

object|string $class

Return Value

mixed

at line 660
protected string|null getPolicyFromAttribute(string $class, bool $includeParents = false)

Get the policy class from the UsePolicy attribute.

Parameters

string $class
bool $includeParents

Return Value

string|null

at line 682
protected array guessPolicyName(string $class)

Guess the policy name for the given class.

Parameters

string $class

Return Value

array

at line 710
Gate guessPolicyNamesUsing(callable $callback)

Specify a callback to be used to guess policy names.

Boot-only. The callback persists on the singleton Gate for the worker lifetime and affects policy discovery in every subsequent request.

Parameters

callable $callback

Return Value

Gate

at line 724
mixed resolvePolicy(object|string $class)

Build a policy class instance of the given type.

Parameters

object|string $class

Return Value

mixed

at line 732
protected bool|callable resolvePolicyCallback(mixed $user, string $ability, array $arguments, mixed $policy)

Resolve the callback for a policy check.

Parameters

mixed $user
string $ability
array $arguments
mixed $policy

Return Value

bool|callable

at line 765
protected mixed callPolicyBefore(mixed $policy, mixed $user, string $ability, array $arguments)

Call the "before" method on the given policy, if applicable.

Parameters

mixed $policy
mixed $user
string $ability
array $arguments

Return Value

mixed

at line 781
protected mixed callPolicyMethod(mixed $policy, string $method, mixed $user, array $arguments)

Call the appropriate method on the given policy.

Parameters

mixed $policy
string $method
mixed $user
array $arguments

Return Value

mixed

at line 804
protected string formatAbilityToMethod(string $ability)

Format the policy ability into a method name.

Parameters

string $ability

Return Value

string

at line 813
Gate forUser(mixed $user)

Get a gate instance for the given user.

Parameters

mixed $user

Return Value

Gate

at line 837
protected mixed resolveUser()

Resolve the user from the user resolver.

Return Value

mixed

at line 845
array abilities()

Get all of the defined abilities.

Return Value

array

at line 853
array policies()

Get all of the defined policies.

Return Value

array

at line 864
Gate defaultDenialResponse(Response $response)

Set the default denial response for gates and policies.

Boot-only. The response persists on the singleton Gate for the worker lifetime and affects authorization failures in every subsequent request.

Parameters

Response $response

Return Value

Gate

at line 878
Gate setContainer(Container $container)

Set the container instance used by the gate.

Tests only. Swaps the container on the singleton Gate used by every coroutine; per-request use races across coroutines and breaks every concurrent authorization check.

Parameters

Container $container

Return Value

Gate

at line 893
Builder scope(UnitEnum|string $ability, Builder $query)

Apply the policy's scope method to filter a query to authorized rows.

A policy scope is preferred, with a scalar selection used as the fallback.

Parameters

UnitEnum|string $ability
Builder $query

Return Value

Builder

Exceptions

LogicException
RuntimeException

at line 953
Expression|Builder select(UnitEnum|string $ability, Builder|Model|string $query)

Get a scalar boolean selection from the policy for per-row authorization.

Parameters

UnitEnum|string $ability
Builder|Model|string $query

Return Value

Expression|Builder

Exceptions

LogicException
RuntimeException

at line 1011
protected array resolveQueryPolicy(string $ability, Builder $query, string $nativeMethod, string $fallbackMethod)

Resolve the policy method and authorization state for a query operation.

Parameters

string $ability
Builder $query
string $nativeMethod
string $fallbackMethod

Return Value

array

at line 1047
protected Builder denyQuery(Builder $query)

Deny every row on the given query.

Scope grouping prevents an existing caller OR from bypassing the denial.

Parameters

Builder $query

Return Value

Builder

at line 1059
protected void callPolicyScope(mixed $policy, string $method, mixed $user, Builder $query)

Apply a policy scope to the given query.

Parameters

mixed $policy
string $method
mixed $user
Builder $query

Return Value

void

Exceptions

LogicException

at line 1078
protected Expression|Builder callPolicySelection(mixed $policy, string $method, mixed $user, Builder $query)

Get a scalar boolean selection from a policy method.

Parameters

mixed $policy
string $method
mixed $user
Builder $query

Return Value

Expression|Builder

at line 1092
protected Builder buildScopeSelection(string $ability, mixed $policy, string $method, mixed $user, Builder $query)

Build a correlated scalar selection from a policy scope.

Parameters

string $ability
mixed $policy
string $method
mixed $user
Builder $query

Return Value

Builder

Exceptions

LogicException

at line 1140
static void flushState()

Flush all static state.

Return Value

void