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 103
__construct(Container $container, Closure $userResolver, array $abilities = [], array $policies = [], array $beforeCallbacks = [], array $afterCallbacks = [], callable|null $guessPolicyNamesUsingCallback = null)
Create a new gate instance.
at line 118
bool
has(array|UnitEnum|string $ability)
Determine if a given ability has been defined.
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.
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.
at line 157
protected Response
authorizeOnDemand(mixed $condition, string|null $message, int|string|null $code, bool $allowWhenResponseIs)
Authorize a given condition or callback.
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.
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.
at line 231
protected Closure
buildAbilityCallback(string $ability, string $callback)
Create the ability callback for a callback string.
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.
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.
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.
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.
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.
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.
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.
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.
at line 363
Response
authorize(UnitEnum|string $ability, mixed $arguments = [])
Determine if the given ability should be granted for the current user.
at line 371
Response
inspect(UnitEnum|string $ability, mixed $arguments = [])
Inspect the user for the given ability.
at line 393
mixed
raw(string $ability, mixed $arguments = [])
Get the raw result from the authorization callback.
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.
at line 450
protected bool
methodAllowsGuests(mixed $class, string $method)
Determine if the given class method allows guests.
at line 479
protected bool
callbackAllowsGuests(callable $callback)
Determine if the callback allows guests.
at line 505
protected bool
parameterAllowsGuests(ReflectionParameter $parameter)
Determine if the given parameter allows guests.
at line 514
protected bool|Response|null
callAuthCallback(mixed $user, string $ability, array $arguments)
Resolve and call the appropriate authorization callback.
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.
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.
at line 560
protected void
dispatchGateEvaluatedEvent(mixed $user, string $ability, array $arguments, bool|Response|null $result)
Dispatch a gate evaluation event.
at line 576
protected callable
resolveAuthCallback(mixed $user, string $ability, array $arguments)
Resolve the callable for the given ability and arguments.
at line 603
mixed
getPolicyFor(object|string $class)
Get a policy instance for a given class.
at line 660
protected string|null
getPolicyFromAttribute(string $class, bool $includeParents = false)
Get the policy class from the UsePolicy attribute.
at line 682
protected array
guessPolicyName(string $class)
Guess the policy name for the given class.
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.
at line 724
mixed
resolvePolicy(object|string $class)
Build a policy class instance of the given type.
at line 732
protected bool|callable
resolvePolicyCallback(mixed $user, string $ability, array $arguments, mixed $policy)
Resolve the callback for a policy check.
at line 765
protected mixed
callPolicyBefore(mixed $policy, mixed $user, string $ability, array $arguments)
Call the "before" method on the given policy, if applicable.
at line 781
protected mixed
callPolicyMethod(mixed $policy, string $method, mixed $user, array $arguments)
Call the appropriate method on the given policy.
at line 804
protected string
formatAbilityToMethod(string $ability)
Format the policy ability into a method name.
at line 813
Gate
forUser(mixed $user)
Get a gate instance for the given user.
at line 837
protected mixed
resolveUser()
Resolve the user from the user resolver.
at line 845
array
abilities()
Get all of the defined abilities.
at line 853
array
policies()
Get all of the defined policies.
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.
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.
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.
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.
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.
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.
at line 1059
protected void
callPolicyScope(mixed $policy, string $method, mixed $user, Builder $query)
Apply a policy scope to the given query.
at line 1078
protected Expression|Builder
callPolicySelection(mixed $policy, string $method, mixed $user, Builder $query)
Get a scalar boolean selection from a policy method.
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.
at line 1140
static void
flushState()
Flush all static state.