# Availability
We can declare whether an Action or its associated Feature is available through the use of the availability
method
When Lantern checks an Action's availability, it also checks the availability of its direct parent Feature.
This allows multiple Actions
to inherit availability but introduces an important restriction:
Inheritance
The Feature of an Action must have a signature that will allow it to be instantiated without arguments
being passed to its __construct()
method.
# AvailabilityBuilder
When you declare the availabilty of an Action
you will use the AvailabilityBuilder
protected function availability(Lantern\Features\AvailabilityBuilder $builder) {
//…
}
# Available methods
Debugging availability
It is often helpful in development to see why an Action
is failing the availability checks.
With all the assert…
methods below, you have the option of providing a $failureMessage
. This can be
inspected when using the gate
collector of the Laravel Debugbar (opens new window).
# user()
Returns the user to use for your checks.
Auth::user()
is used as the default user for checking availability and typically, you will want to check if a given Action is available to the current logged in user.
However, it can be useful to check the Availability of a different user. E.g. if you list out the users belonging to a company in your app, and have certain Actions only available to certain users.
In order to check the availablity for a different user in this way pass the user through as an argument to the available
method
if ($action = MyAction::make()->available($otherUser)) {
$action->perform();
}
Auth::user()
As it is possible to check an Action against a user other than the logged in user you should
never get your user object from Auth::user()
from within protected function availability(){}
.
# userCan()
Check Laravel's authorisation policy (opens new window) for a given ability.
# userCannot()
The opposite of userCan()
.
# assertTrue()
Check if an expression is true
.
# assertFalse()
Check if an expression is false
.
# assertNull()
Check if an expression is null
.
# assertNotNull()
Check if an expression is not null
.
# assertEmpty()
Check if an expression is empty.
# assertNotEmpty()
Check if an expression is not empty.
# assertEqual()
Check if 2 expressions are equal
in value (==
not ===
).
# assertNotEqual()
Check if 2 expressions are not equal
in value (==
not ===
).
Authorisation
For more info on using these Availability checks in your app, look at the docs on authorisation.
# Customising availability checks
Since version 1.1.0
The base AvailabilityBuilder
(opens new window)
offers only the rudimentary assertions you see above, but as you begin to flesh out your domain model
you will quickly want to add a little more meaning to your checks.
To achieve this, you can extend the AvailabilityBuilder
with a child class of your own.
← Actions Constraints →