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. |
|
| static protected array<class-string, class-string|false> | $policyClassCache | Cached model class to policy class mappings. |
|
| static protected array<string, bool> | $guestMethodCache | Cached guest-access results for class methods. |
|
| static protected null|Closure, bool> | $guestClosureCache | Cached guest-access results for closures. |
|
| 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.
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 100
__construct(Container $container, Closure $userResolver, array $abilities = [], array $policies = [], array $beforeCallbacks = [], array $afterCallbacks = [], callable|null $guessPolicyNamesUsingCallback = null)
Create a new gate instance.
at line 115
bool
has(array|UnitEnum|string $ability)
Determine if a given ability has been defined.
at line 133
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 143
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 153
protected Response
authorizeOnDemand(mixed $condition, string|null $message, int|string|null $code, bool $allowWhenResponseIs)
Authorize a given condition or callback.
at line 180
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 207
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 227
protected Closure
buildAbilityCallback(string $ability, string $callback)
Create the ability callback for a callback string.
at line 265
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 278
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 291
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 301
bool
allows(iterable|UnitEnum|string $ability, mixed $arguments = [])
Determine if all of the given abilities should be granted for the current user.
at line 309
bool
denies(iterable|UnitEnum|string $ability, mixed $arguments = [])
Determine if any of the given abilities should be denied for the current user.
at line 317
bool
check(iterable|UnitEnum|string $abilities, mixed $arguments = [])
Determine if all of the given abilities should be granted for the current user.
at line 331
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 345
bool
none(iterable|UnitEnum|string $abilities, mixed $arguments = [])
Determine if all of the given abilities should be denied for the current user.
at line 355
Response
authorize(UnitEnum|string $ability, mixed $arguments = [])
Determine if the given ability should be granted for the current user.
at line 363
Response
inspect(UnitEnum|string $ability, mixed $arguments = [])
Inspect the user for the given ability.
at line 385
mixed
raw(string $ability, mixed $arguments = [])
Get the raw result from the authorization callback.
at line 420
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 442
protected bool
methodAllowsGuests(mixed $class, string $method)
Determine if the given class method allows guests.
at line 471
protected bool
callbackAllowsGuests(callable $callback)
Determine if the callback allows guests.
at line 495
protected bool
parameterAllowsGuests(ReflectionParameter $parameter)
Determine if the given parameter allows guests.
at line 504
protected bool|Response|null
callAuthCallback(mixed $user, string $ability, array $arguments)
Resolve and call the appropriate authorization callback.
at line 514
protected mixed
callBeforeCallbacks(mixed $user, string $ability, array $arguments)
Call all of the before callbacks and return if a result is given.
at line 532
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 550
protected void
dispatchGateEvaluatedEvent(mixed $user, string $ability, array $arguments, bool|Response|null $result)
Dispatch a gate evaluation event.
at line 562
protected callable
resolveAuthCallback(mixed $user, string $ability, array $arguments)
Resolve the callable for the given ability and arguments.
at line 589
mixed
getPolicyFor(object|string $class)
Get a policy instance for a given class.
at line 650
protected string|null
getPolicyFromAttribute(string $class)
Get the policy class from the UsePolicy attribute.
at line 666
protected array
guessPolicyName(string $class)
Guess the policy name for the given class.
at line 694
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 708
mixed
resolvePolicy(object|string $class)
Build a policy class instance of the given type.
at line 716
protected bool|callable
resolvePolicyCallback(mixed $user, string $ability, array $arguments, mixed $policy)
Resolve the callback for a policy check.
at line 749
protected mixed
callPolicyBefore(mixed $policy, mixed $user, string $ability, array $arguments)
Call the "before" method on the given policy, if applicable.
at line 765
protected mixed
callPolicyMethod(mixed $policy, string $method, mixed $user, array $arguments)
Call the appropriate method on the given policy.
at line 788
protected string
formatAbilityToMethod(string $ability)
Format the policy ability into a method name.
at line 797
Gate
forUser(mixed $user)
Get a gate instance for the given user.
at line 813
protected mixed
resolveUser()
Resolve the user from the user resolver.
at line 821
array
abilities()
Get all of the defined abilities.
at line 829
array
policies()
Get all of the defined policies.
at line 840
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 854
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 872
Builder
scope(string $ability, Builder $query)
Apply the policy's scope method to filter a query to authorized rows.
Runs before() callbacks first. If a before callback returns true (allow all), the query is returned unmodified. If it returns false (deny all), a "no rows" constraint is added. If null (no opinion), the policy's *Scope method is called.
After callbacks are not run — they expect a boolean result, not a Builder.
at line 935
Expression
select(string $ability, Builder|Model|string $query)
Get a SQL expression from the policy for per-row authorization.
Runs before() callbacks first. If a before callback returns true, DB::raw('true') is returned. If false, DB::raw('false'). If null, the policy's *Select method is called.
After callbacks are not run — they expect a boolean result, not an Expression.
Accepts a Builder for full query context, or a model class/instance as shorthand (internally creates a fresh query).
at line 985
static void
flushState()
Flush all static state.