Gate
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
Create a new access response.
Throw an unauthorized exception.
Deny with a HTTP status code.
Deny with a 404 HTTP status code.
Create a new gate instance.
Determine if a given ability has been defined.
Perform an on-demand authorization check. Throw an authorization exception if the condition or callback is false.
Perform an on-demand authorization check. Throw an authorization exception if the condition or callback is true.
Authorize a given condition or callback.
Define abilities for a resource.
Create the ability callback for a callback string.
Determine if all of the given abilities should be granted for the current user.
Determine if any of the given abilities should be denied for the current user.
Determine if all of the given abilities should be granted for the current user.
Determine if any one of the given abilities should be granted for the current user.
Determine if all of the given abilities should be denied for the current user.
Determine if the given ability should be granted for the current user.
Inspect the user for the given ability.
Get the raw result from the authorization callback.
Determine whether the callback/method can be called with the given user.
Determine if the given class method allows guests.
Determine if the callback allows guests.
Determine if the given parameter allows guests.
Resolve and call the appropriate authorization callback.
Call all of the before callbacks and return if a result is given.
Call all of the after callbacks with check result.
Dispatch a gate evaluation event.
Resolve the callable for the given ability and arguments.
Get a policy instance for a given class.
Get the policy class from the UsePolicy attribute.
Guess the policy name for the given class.
Specify a callback to be used to guess policy names.
Build a policy class instance of the given type.
Resolve the callback for a policy check.
Call the "before" method on the given policy, if applicable.
Call the appropriate method on the given policy.
Format the policy ability into a method name.
Resolve the user from the user resolver.
Get all of the defined abilities.
Get all of the defined policies.
Resolve the policy method and authorization state for a query operation.
Apply a policy scope to the given query.
Get a scalar boolean selection from a policy method.
Build a correlated scalar selection from a policy scope.
Flush all static state.
Details
in
HandlesAuthorization at line 12
protected Response
allow(string|null $message = null, int|string|null $code = null)
Create a new access response.
in
HandlesAuthorization at line 20
protected Response
deny(string|null $message = null, int|string|null $code = null)
Throw an unauthorized exception.
in
HandlesAuthorization at line 28
Response
denyWithStatus(int $status, string|null $message = null, int|string|null $code = null)
Deny with a HTTP status code.
in
HandlesAuthorization at line 36
Response
denyAsNotFound(string|null $message = null, int|string|null $code = null)
Deny with a 404 HTTP status code.
at line 105
__construct(Container $container, Closure $userResolver, array $abilities = [], array $policies = [], array $beforeCallbacks = [], array $afterCallbacks = [], callable|null $guessPolicyNamesUsingCallback = null)
Create a new gate instance.
at line 120
bool
has(array|UnitEnum|string $ability)
Determine if a given ability has been defined.
at line 139
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.
at line 149
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.
at line 159
protected Response
authorizeOnDemand(mixed $condition, string|null $message, int|string|null $code, bool $allowWhenResponseIs)
Authorize a given condition or callback.
at line 188
Gate
define(UnitEnum|string $ability, array|callable|string $callback)
Define a new ability.
The definition persists on the singleton Gate for the worker lifetime and affects authorization in every subsequent request.
at line 218
Gate
resource(string $name, string $class, array|null $abilities = null)
Define abilities for a resource.
The definitions persist on the singleton Gate for the worker lifetime and affect authorization in every subsequent request.
at line 239
protected Closure
buildAbilityCallback(string $ability, string $callback)
Create the ability callback for a callback string.
at line 279
Gate
policy(string $class, string $policy)
Define a policy class for a given class type.
The policy persists on the singleton Gate for the worker lifetime and affects authorization in every subsequent request.
at line 299
Gate
before(callable $callback)
Register a callback to run before all Gate checks.
The callback persists on the singleton Gate for the worker lifetime and runs during authorization in every subsequent request.
at line 315
Gate
after(callable $callback)
Register a callback to run after all Gate checks.
The callback persists on the singleton Gate for the worker lifetime and runs during authorization in every subsequent request.
at line 326
bool
allows(iterable|UnitEnum|string $ability, mixed $arguments = [])
Determine if all of the given abilities should be granted for the current user.
at line 334
bool
denies(iterable|UnitEnum|string $ability, mixed $arguments = [])
Determine if any of the given abilities should be denied for the current user.
at line 342
bool
check(iterable|UnitEnum|string $abilities, mixed $arguments = [])
Determine if all of the given abilities should be granted for the current user.
at line 356
bool
any(iterable|UnitEnum|string $abilities, mixed $arguments = [])
Determine if any one of the given abilities should be granted for the current user.
at line 370
bool
none(iterable|UnitEnum|string $abilities, mixed $arguments = [])
Determine if all of the given abilities should be denied for the current user.
at line 380
Response
authorize(UnitEnum|string $ability, mixed $arguments = [])
Determine if the given ability should be granted for the current user.
at line 388
Response
inspect(UnitEnum|string $ability, mixed $arguments = [])
Inspect the user for the given ability.
at line 410
mixed
raw(string $ability, mixed $arguments = [])
Get the raw result from the authorization callback.
at line 445
protected bool
canBeCalledWithUser(mixed $user, mixed $class, string|null $method = null)
Determine whether the callback/method can be called with the given user.
at line 467
protected bool
methodAllowsGuests(mixed $class, string $method)
Determine if the given class method allows guests.
at line 496
protected bool
callbackAllowsGuests(callable $callback)
Determine if the callback allows guests.
at line 522
protected bool
parameterAllowsGuests(ReflectionParameter $parameter)
Determine if the given parameter allows guests.
at line 531
protected bool|Response|null
callAuthCallback(mixed $user, string $ability, array $arguments)
Resolve and call the appropriate authorization callback.
at line 541
protected mixed
callBeforeCallbacks(mixed $user, string $ability, array $arguments)
Call all of the before callbacks and return if a result is given.
at line 559
protected bool|Response|null
callAfterCallbacks(mixed $user, string $ability, array $arguments, bool|Response|null $result)
Call all of the after callbacks with check result.
at line 577
protected void
dispatchGateEvaluatedEvent(mixed $user, string $ability, array $arguments, bool|Response|null $result)
Dispatch a gate evaluation event.
at line 593
protected callable
resolveAuthCallback(mixed $user, string $ability, array $arguments)
Resolve the callable for the given ability and arguments.
at line 620
mixed
getPolicyFor(object|string $class)
Get a policy instance for a given class.
at line 677
protected string|null
getPolicyFromAttribute(string $class, bool $includeParents = false)
Get the policy class from the UsePolicy attribute.
at line 699
protected array
guessPolicyName(string $class)
Guess the policy name for the given class.
at line 729
Gate
guessPolicyNamesUsing(callable $callback)
Specify a callback to be used to guess policy names.
The callback persists on the singleton Gate for the worker lifetime and affects policy discovery in every subsequent request.
at line 744
mixed
resolvePolicy(object|string $class)
Build a policy class instance of the given type.
at line 752
protected bool|callable
resolvePolicyCallback(mixed $user, string $ability, array $arguments, mixed $policy)
Resolve the callback for a policy check.
at line 785
protected mixed
callPolicyBefore(mixed $policy, mixed $user, string $ability, array $arguments)
Call the "before" method on the given policy, if applicable.
at line 801
protected mixed
callPolicyMethod(mixed $policy, string $method, mixed $user, array $arguments)
Call the appropriate method on the given policy.
at line 824
protected string
formatAbilityToMethod(string $ability)
Format the policy ability into a method name.
at line 833
Gate
forUser(mixed $user)
Get a gate instance for the given user.
at line 857
protected mixed
resolveUser()
Resolve the user from the user resolver.
at line 865
array
abilities()
Get all of the defined abilities.
at line 873
array
policies()
Get all of the defined policies.
at line 886
Gate
defaultDenialResponse(Response $response)
Set the default denial response for gates and policies.
The response persists on the singleton Gate for the worker lifetime and affects authorization failures in every subsequent request.
at line 903
Gate
setContainer(Container $container)
Set the container instance used by the gate.
Swaps the container on the singleton Gate used by every coroutine; per-request use races across coroutines and breaks every concurrent authorization check.
at line 919
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.
at line 979
Expression|Builder
select(UnitEnum|string $ability, Builder|Model|string $query)
Get a scalar boolean selection from the policy for per-row authorization.
at line 1037
protected array
resolveQueryPolicy(string $ability, Builder $query, string $nativeMethod, string $fallbackMethod)
Resolve the policy method and authorization state for a query operation.
at line 1073
protected Builder
denyQuery(Builder $query)
Deny every row on the given query.
Scope grouping prevents an existing caller OR from bypassing the denial.
at line 1085
protected void
callPolicyScope(mixed $policy, string $method, mixed $user, Builder $query)
Apply a policy scope to the given query.
at line 1104
protected Expression|Builder
callPolicySelection(mixed $policy, string $method, mixed $user, Builder $query)
Get a scalar boolean selection from a policy method.
at line 1118
protected Builder
buildScopeSelection(string $ability, mixed $policy, string $method, mixed $user, Builder $query)
Build a correlated scalar selection from a policy scope.
at line 1166
static void
flushState()
Flush all static state.