Replies: 4 comments 6 replies
-
It was always expressed that Policies must return Check the documentation for Laravel 8.x, 9.x |
Beta Was this translation helpful? Give feedback.
-
See https://freek.dev/1325-when-to-use-gateafter-in-laravel An example policy shown the "Explaining Gate::after" section:
Laravel switched from doc blocks to return type declarations in 10. That's when my IDE started complaining. Please read the cited documentation before commenting. |
Beta Was this translation helpful? Give feedback.
-
I've rarely used the That said, Laravel's docs: https://laravel.com/docs/10.x/authorization#intercepting-gate-checks don't indicate a specific return type for bespoke Gate calls: use App\Models\User;
Gate::after(function (User $user, string $ability, bool|null $result, mixed $arguments) {
if ($user->isAdministrator()) {
return true;
}
});
IDE's don't always get it right. Especially if they can't trace all the way into the internals being leveraged. |
Beta Was this translation helpful? Give feedback.
-
This is how I assumed it worked. I do wonder what happens if neither the model policy method nor Of course, the |
Beta Was this translation helpful? Give feedback.
-
When writing model policy methods for an application where the "Admin" role has any permission by default, we've followed the approach outlined in https://freek.dev/1325-when-to-use-gateafter-in-laravel (edit: which is provided as a reference at https://spatie.be/docs/laravel-permission/v6/basic-usage/super-admin#content-gateafter)
This boils down to: only write conditions that return
true
, unless we want to deny an ability even to admins in which case we returnfalse
.And then our
Gate::after()
looks like:So when we want to deny authorization to a non-admin, we don't need to return anything.
Starting with Laravel 10, model policy methods have a
bool
return type declaration. While we've removed these and things seem to be working, I would like to continue following Laravel convention if possible, especially in future projects.I'm wondering if anyone has any thoughts on this?
In order to always return a boolean, we'd have to rewrite every policy method to include a check for admin role.
Beta Was this translation helpful? Give feedback.
All reactions