Skip to content

Commit

Permalink
Fix getting '0' from route parameter in Authorize middleware (#42582)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien-phi authored May 31, 2022
1 parent 29c7b9b commit 532ea74
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Auth/Middleware/Authorize.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected function getModel($request, $model)
if ($this->isClassName($model)) {
return trim($model);
} else {
return $request->route($model, null) ?:
return $request->route($model, null) ??
((preg_match("/^['\"](.*)['\"]$/", trim($model), $matches)) ? $matches[1] : null);
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Auth/AuthorizeMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,24 @@ public function testSimpleAbilityWithStringParameterFromRouteParameter()
$this->assertSame('success', $response->content());
}

public function testSimpleAbilityWithStringParameter0FromRouteParameter()
{
$this->gate()->define('view-dashboard', function ($user, $param) {
return $param === '0';
});

$this->router->get('dashboard/{route_parameter}', [
'middleware' => Authorize::class.':view-dashboard,route_parameter',
'uses' => function () {
return 'success';
},
]);

$response = $this->router->dispatch(Request::create('dashboard/0', 'GET'));

$this->assertSame('success', $response->content());
}

public function testModelTypeUnauthorized()
{
$this->expectException(AuthorizationException::class);
Expand Down

0 comments on commit 532ea74

Please sign in to comment.