You can have policy method without model https://laravel.com/docs/master/authorization#methods-without-models
Possible to register a policy without a model?
I'm working on a project that leverages policies for authorization, and this works well when I'm authorizing an entity that has a corresponding model. However, not every entity I want to authorize has a corresponding model.
Is there a way to register a policy without a model (or is that fundamentally incorrect?), or should I take a different approach?
Stumbled upon this looking for something else, and wanted to add some views and suggestions, which may help the OP @jdillon and any others coming her in the future.
I think what @martinbean was trying to articulate , is that the Policy class itself (Laravels implementation) is designed specifically to be tied to a Model class, but that doesn't mean you cannot still use the authentication system (Gates) within Laravel if you want something without a model.
To provide some context we have a huge number of both of these approaches within our applications, as both have a part to play... take the following examples;
Policies
- Verifying a user can Create, Update, Delete etc a blog post
- Verifying that a user can comment on a blog post
Simple example, but these could be solved using the Policy approach, tied to a model, and having functions related to the desired action. Then inside the methods use a combination of checking the users permissions, they created the post, and even system settings such as comments are enabled for posts (individually or at all)... you get the idea.
This is what the core examples pertain to, and is fine if the logic is predominantly tied to a model and its associated Crud activities.
Pretty much every object in our system has policy, with various methods associated with each, predominantly CRUD related, but not always, though usually always related to the changing of the state of the model.
Non-Model Policies (Basic Gates)
Take the following examples;
- Verify a user can clear the system caches from within and admin panel
- Verify a user has the ability to put the system into a form of maintenance mode
- Verify a user can force log other users out, or force refresh tokens
These are more aligned to what I believe @jdillon was referring to, and obviously they do not relate to a model, and it would be wrong to 'shoehorn' them into a Policy class bound to a model. So where do action oriented checks like this go... Gates, just plain old gates.
While it has already been mentioned that Gates can be used for this, and are ideal for doing so, I wanted to clarify some confusion with regards to the Class approach to using a gate... It doesn't have to be a Policy, it can be any old class and method, essentially the second parameter to define on the Gate facade just wants a callable that it can feed a user... either inline or as a reference.
Take this example from one of our apps:
Gate::define('system.admin.cache.clear', 'App\Modules\Auth\SystemAdminAbilities@clearCache');
The above is one of our defined 'abilities', which we manage using the Gate system, but it in no way uses the model policies. It is literally a class with callable methods, within which we check a whole bunch of things depending on what we want to check the user can do.
Summary
As always, there is always more than one way to do something, some suit some situations better than others, and some are preferable for other reasons. The key aspect is to not get hung up on something being the 'correct way', and instead reach for the way which suits your needs the most without being afraid to mix and match.
Please or to participate in this conversation.