Skip to content

Commit

Permalink
[5.7] Authorize Middleware Doesn't Accept String Parameters (#25763)
Browse files Browse the repository at this point in the history
* Add test case for string parameter for Authorize middleware

* Pass model value as default value in Authorize middleware

* Add test using string route parameter
  • Loading branch information
damiankloip authored and taylorotwell committed Sep 27, 2018
1 parent 6119ebe commit 942fc31
Show file tree
Hide file tree
Showing 2 changed files with 37 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 @@ -72,7 +72,7 @@ protected function getGateArguments($request, $models)
*/
protected function getModel($request, $model)
{
return $this->isClassName($model) ? $model : $request->route($model);
return $this->isClassName($model) ? $model : $request->route($model, $model);
}

/**
Expand Down
36 changes: 36 additions & 0 deletions tests/Auth/AuthorizeMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,42 @@ public function testSimpleAbilityAuthorized()
$this->assertEquals($response->content(), 'success');
}

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

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

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

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

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

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

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

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

/**
* @expectedException \Illuminate\Auth\Access\AuthorizationException
* @expectedExceptionMessage This action is unauthorized.
Expand Down

0 comments on commit 942fc31

Please sign in to comment.