Authorization
Masonite also provides a simple way to authorize user actions against a given resource. This is achieved with two concepts: gates and policies. Gates are as the name suggests an authorization check that you will be able to invoke to verify user access. Policies are a way to groupe authorization logic around a model.
Gates
Gates are simple callable that will define if a user is authorized to perform a given action. A handy Gate
facade is available to easily manipulate gates.
Registering Gates
Gates receive a user instance as their first argument and may receive additionals arguments such as a Model instance. You needs to define Gates
in boot()
method of your service provider.
In the following example we are adding gates to verify if a user can create and update posts. Users can create posts if they are administrators and can update posts they have created only.
You can then check if a gate exists or has been registed by using has()
which is returning a boolean:
If a unknown gate is used a GateDoesNotExist
exception will be raised.
Authorizing Actions
Then anywhere (often in a controller) in your code you can use those gates to check if the current authenticated user is authorized to perform the given action defined by the gate.
Gates exposes different methods to perform verification: allows()
, denies()
, none()
, any()
, authorize()
inspect()
.
allows()
, denies()
, none()
and any()
return a boolean indicating if user is authorized
authorize()
does not return a boolean but will raise an AuthorizationException
exception instead that will be rendered as an HTTP response with a 403 status code.
Finally for better control over the authorization check you can analyse the response with inspect()
:
Via the User Model
Authorizes
class can be added to your User model to allow quick permission checks:
A fluent authorization api will now be available on User
instances:
All of those methods receive the gate name as first argument and then some additional arguments if required.
With a given user
You can use the for_user()
method on the Gate facade to make the verification against a given user instead of the authenticated user.
Gate Hooks
During the gate authorization process, before
and after
hooks can be triggered.
A before
hook can be added like this:
The after
hook works the same way:
If the after
callback is returning a value it will take priority over the gate result check.
Policies
Policies are classes that organize authorization logic around a specific model.
Creating Policies
You can run the craft command:
You can also create a policy with a set of predefined gates by using the --model
flag:
A model policy comes with common actions that we can perform on a model:
You are free to add any other methods on your policies:
Registering Policies
Then in your service provider (as for defining gates) you should register the policies and bind them with a model:
An example policy for the Post model may look like this:
If an unknown policy is used then a PolicyDoesNotExist
exception will be raised.
Authorizing Actions
You can then use the Gate
facade methods to authorize actions defined in your policies. With the previously defined PostPolicy
we could make the following calls:
The create()
or view_any()
methods do not take a model instance, that is why the model class should be provided so that Gate mechanism can infer from which policy those methods are belonging to.
Last updated