Gate
class Gate implements Gate (View source)
Traits
Properties
protected Container | $container | The container instance. |
|
protected callable | $userResolver | The user resolver callable. |
|
protected array | $abilities | All of the defined abilities. |
|
protected array | $policies | All of the defined policies. |
|
protected array | $beforeCallbacks | All of the registered before callbacks. |
|
protected array | $afterCallbacks | All of the registered after callbacks. |
|
protected array | $stringCallbacks | All of the defined abilities using class@method notation. |
|
protected callable|null | $guessPolicyNamesUsingCallback | The callback to be used to guess policy names. |
Methods
Create a new access response.
Throws an unauthorized exception.
Create a new gate instance.
Determine if a given ability has been defined.
Define a new ability.
Define abilities for a resource.
Create the ability callback for a callback string.
Define a policy class for a given class type.
Register a callback to run before all Gate checks.
Register a callback to run after all Gate checks.
Determine if the given ability should be granted for the current user.
Determine if the given ability 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.
Resolve the callable for the given ability and arguments.
Get a policy instance for a given class.
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.
Details
protected Response
allow(string|null $message = null, mixed $code = null)
Create a new access response.
protected Response
deny(string|null $message = null, mixed|null $code = null)
Throws an unauthorized exception.
void
__construct(Container $container, callable $userResolver, array $abilities = [], array $policies = [], array $beforeCallbacks = [], array $afterCallbacks = [], callable $guessPolicyNamesUsingCallback = null)
Create a new gate instance.
bool
has(string $ability)
Determine if a given ability has been defined.
$this
define(string $ability, callable|string $callback)
Define a new ability.
$this
resource(string $name, string $class, array $abilities = null)
Define abilities for a resource.
protected Closure
buildAbilityCallback(string $ability, string $callback)
Create the ability callback for a callback string.
$this
policy(string $class, string $policy)
Define a policy class for a given class type.
$this
before(callable $callback)
Register a callback to run before all Gate checks.
$this
after(callable $callback)
Register a callback to run after all Gate checks.
bool
allows(string $ability, array|mixed $arguments = [])
Determine if the given ability should be granted for the current user.
bool
denies(string $ability, array|mixed $arguments = [])
Determine if the given ability should be denied for the current user.
bool
check(iterable|string $abilities, array|mixed $arguments = [])
Determine if all of the given abilities should be granted for the current user.
bool
any(iterable|string $abilities, array|mixed $arguments = [])
Determine if any one of the given abilities should be granted for the current user.
bool
none(iterable|string $abilities, array|mixed $arguments = [])
Determine if all of the given abilities should be denied for the current user.
Response
authorize(string $ability, array|mixed $arguments = [])
Determine if the given ability should be granted for the current user.
Response
inspect(string $ability, array|mixed $arguments = [])
Inspect the user for the given ability.
mixed
raw(string $ability, array|mixed $arguments = [])
Get the raw result from the authorization callback.
protected bool
canBeCalledWithUser(Authenticatable|null $user, Closure|string|array $class, string|null $method = null)
Determine whether the callback/method can be called with the given user.
protected bool
methodAllowsGuests(string $class, string $method)
Determine if the given class method allows guests.
protected bool
callbackAllowsGuests(callable $callback)
Determine if the callback allows guests.
protected bool
parameterAllowsGuests(ReflectionParameter $parameter)
Determine if the given parameter allows guests.
protected bool
callAuthCallback(Authenticatable|null $user, string $ability, array $arguments)
Resolve and call the appropriate authorization callback.
protected bool|null
callBeforeCallbacks(Authenticatable|null $user, string $ability, array $arguments)
Call all of the before callbacks and return if a result is given.
protected bool|null
callAfterCallbacks(Authenticatable $user, string $ability, array $arguments, bool $result)
Call all of the after callbacks with check result.
protected callable
resolveAuthCallback(Authenticatable|null $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.
protected array
guessPolicyName(string $class)
Guess the policy name for the given class.
$this
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.
protected bool|callable
resolvePolicyCallback(Authenticatable $user, string $ability, array $arguments, mixed $policy)
Resolve the callback for a policy check.
protected mixed
callPolicyBefore(mixed $policy, Authenticatable $user, string $ability, array $arguments)
Call the "before" method on the given policy, if applicable.
protected mixed
callPolicyMethod(mixed $policy, string $method, Authenticatable|null $user, array $arguments)
Call the appropriate method on the given policy.
protected string
formatAbilityToMethod(string $ability)
Format the policy ability into a method name.
Gate
forUser(Authenticatable|mixed $user)
Get a gate instance for the given user.
protected 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.