Skip to content

Commit

Permalink
Fix AuthorizeResources.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jun 3, 2016
1 parent f2abad5 commit 25443e3
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions src/Illuminate/Foundation/Auth/Access/AuthorizesResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,19 @@ trait AuthorizesResources
* Authorize a resource action based on the incoming request.
*
* @param string $model
* @param string|null $name
* @param string|null $parameter
* @param array $options
* @param \Illuminate\Http\Request|null $request
* @return \Illuminate\Routing\ControllerMiddlewareOptions
*/
public function authorizeResource($model, $name = null, array $options = [], $request = null)
public function authorizeResource($model, $parameter = null, array $options = [])
{
$method = array_last(explode('@', with($request ?: request())->route()->getActionName()));
$parameter = $parameter ?: strtolower(class_basename($model));

$map = $this->resourceAbilityMap();
foreach ($this->resourceAbilityMap() as $method => $ability) {
$modelName = in_array($method, ['index', 'create', 'store']) ? $model : $parameter;

if (! in_array($method, array_keys($map))) {
return new ControllerMiddlewareOptions($options);
$this->middleware("can:{$ability},{$modelName}", $options)->only($method);
}

if (! in_array($method, ['index', 'create', 'store'])) {
$model = $name ?: strtolower(class_basename($model));
}

return $this->middleware("can:{$map[$method]},{$model}", $options);
}

/**
Expand All @@ -40,12 +33,12 @@ public function authorizeResource($model, $name = null, array $options = [], $re
protected function resourceAbilityMap()
{
return [
'index' => 'view',
'create' => 'create',
'store' => 'create',
'show' => 'view',
'edit' => 'update',
'update' => 'update',
'index' => 'view',
'create' => 'create',
'store' => 'create',
'show' => 'view',
'edit' => 'update',
'update' => 'update',
'destroy' => 'delete',
];
}
Expand Down

0 comments on commit 25443e3

Please sign in to comment.