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

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)

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(string $ability, Builder $query)

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

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

Get a SQL expression from the policy for per-row authorization.

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 100
__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 115
bool has(array|UnitEnum|string $ability)

Determine if a given ability has been defined.

Parameters

array|UnitEnum|string $ability

Return Value

bool

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.

Parameters

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

Return Value

Response

Exceptions

AuthorizationException

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.

Parameters

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

Return Value

Response

Exceptions

AuthorizationException

at line 153
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 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.

Parameters

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

Return Value

Gate

Exceptions

InvalidArgumentException

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.

Parameters

string $name
string $class
array|null $abilities

Return Value

Gate

at line 227
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 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.

Parameters

string $class
string $policy

Return Value

Gate

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.

Parameters

callable $callback

Return Value

Gate

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.

Parameters

callable $callback

Return Value

Gate

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.

Parameters

iterable|UnitEnum|string $ability
mixed $arguments

Return Value

bool

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.

Parameters

iterable|UnitEnum|string $ability
mixed $arguments

Return Value

bool

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.

Parameters

iterable|UnitEnum|string $abilities
mixed $arguments

Return Value

bool

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.

Parameters

iterable|UnitEnum|string $abilities
mixed $arguments

Return Value

bool

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.

Parameters

iterable|UnitEnum|string $abilities
mixed $arguments

Return Value

bool

at line 355
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 363
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 385
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 420
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 442
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 471
protected bool callbackAllowsGuests(callable $callback)

Determine if the callback allows guests.

Parameters

callable $callback

Return Value

bool

at line 495
protected bool parameterAllowsGuests(ReflectionParameter $parameter)

Determine if the given parameter allows guests.

Parameters

ReflectionParameter $parameter

Return Value

bool

at line 504
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 514
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 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.

Parameters

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

Return Value

bool|Response|null

at line 550
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 562
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 589
mixed getPolicyFor(object|string $class)

Get a policy instance for a given class.

Parameters

object|string $class

Return Value

mixed

at line 650
protected string|null getPolicyFromAttribute(string $class)

Get the policy class from the UsePolicy attribute.

Parameters

string $class

Return Value

string|null

at line 666
protected array guessPolicyName(string $class)

Guess the policy name for the given class.

Parameters

string $class

Return Value

array

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.

Parameters

callable $callback

Return Value

Gate

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

Build a policy class instance of the given type.

Parameters

object|string $class

Return Value

mixed

at line 716
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 749
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 765
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 788
protected string formatAbilityToMethod(string $ability)

Format the policy ability into a method name.

Parameters

string $ability

Return Value

string

at line 797
Gate forUser(mixed $user)

Get a gate instance for the given user.

Parameters

mixed $user

Return Value

Gate

at line 813
protected mixed resolveUser()

Resolve the user from the user resolver.

Return Value

mixed

at line 821
array abilities()

Get all of the defined abilities.

Return Value

array

at line 829
array policies()

Get all of the defined policies.

Return Value

array

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.

Parameters

Response $response

Return Value

Gate

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.

Parameters

Container $container

Return Value

Gate

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.

Parameters

string $ability
Builder $query

Return Value

Builder

Exceptions

RuntimeException

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).

Parameters

string $ability
Builder|Model|string $query

Return Value

Expression

Exceptions

RuntimeException

at line 985
static void flushState()

Flush all static state.

Return Value

void