-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.7] Authorize Middleware Doesn't Accept String Parameters #25763
[5.7] Authorize Middleware Doesn't Accept String Parameters #25763
Conversation
868afcb
to
ee642c7
Compare
ee642c7
to
73168c9
Compare
@@ -86,6 +86,24 @@ public function testSimpleAbilityAuthorized() | |||
$this->assertEquals($response->content(), 'success'); | |||
} | |||
|
|||
public function testSimpleAbilityWithStringParameter() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add test like:
- The route has parameter
document
- You pass the value
document
forsome_value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @TBlindaruk - do you mean like 2aa094d ? So we pass a parameter, get that back and is available in the ability callback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, like this, but I`m not sure if it is good behavior
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This behaviour already exists though? If you had a parameter on the route, the existing code calling route()
would already return this anyway. The new test method added doesn't really test the change with the default value but the current logic.
|
||
$response = $this->router->dispatch(Request::create('dashboard', 'GET')); | ||
|
||
$this->assertEquals($response->content(), 'success'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is actually the other way round ;-)
$this->assertEquals('success', $response->content());
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy to change this, every other assertion like this in the file is $this->assertEquals($response->content(), 'success')
though.
|
||
$response = $this->router->dispatch(Request::create('dashboard/true', 'GET')); | ||
|
||
$this->assertEquals($response->content(), 'success'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
This change broke our code. We had something like this:
and on our Policy:
And then when someone accessed the second rout the forum parameter would be null and now it becomes a string |
When using authorization gates, they can be used with arbitrary parameters that aren't necessarily models. E.g.
In blade templates:
And, as middleware via
authorize
in controllers:However, when trying to do that at the route middleware level, the
Authorize
middleware always assumes the parameter will be a model. So it's not possible to do the following:This breaks currently as
getGateArguments
(and thengetModel
) assumes the parameter will be a model (or class name) and tries to resolve it from route parameters as so. So the above example would be turned into an array of arguments like[null]
instead.This can be solved quite simply by passing the
$model
value as the default value to theroute()
call.It doesn't seem like this should cause any existing breakage, as it currently returns an array of null values in this case. Tests also don't fail with the change - only with the new test case added here (before the fix). This also makes the behaviour more consist across all usage.