[5.5] Support for multiword models to authorize resource controllers #19821
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Not sure if this should be pushed to 5.4 branch or master branch.
Method
authorizeResource
inframework/src/Illuminate/Foundation/Auth/Access/AuthorizesRequests.php
can be used to authorize resource controllers. So if we have modelOrder
and resource controllerOrdersController
we can useauthorizeResorce(Order::class)
method in controller to authorize each REST method withOrderPolicy
.It works fine but if we want to authorize controller for multiword model for example:
OrderDetail
it will not work. The problem is this codeSo when the second param is null, it will take model name and change the first letter to lowercase.
For example
$parameter
for modelOrder
will beorder
but for modelOrderDetail
it will beorderDetail
.Unfortunatly this is wrong value because as default laravel params in route model binding are in
snake_case
notcamelCase
.This issue returns always 401 Unauthorized. Of course we can specify the second param maually like so:
authorizeResorce(OrderDetail::class, 'order_detail')
but nobody knows about that during implementing ACL feature in his laravel app.To save time for future users, support for multiword models should be done automatically like so:
Result for models:
order
(still the same)order_detail
(snake case instead of camel case)