Skip to content

Commit

Permalink
Move ability map to a method (#13214)
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber authored and taylorotwell committed Apr 19, 2016
1 parent a37f3cc commit d0b5291
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/Illuminate/Foundation/Auth/Access/AuthorizesResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,6 @@

trait AuthorizesResources
{
/**
* Map of resource methods to ability names.
*
* @var array
*/
protected $resourceAbilityMap = [
'index' => 'view',
'create' => 'create',
'store' => 'create',
'show' => 'view',
'edit' => 'update',
'update' => 'update',
'delete' => 'delete',
];

/**
* Authorize a resource action based on the incoming request.
*
Expand All @@ -34,14 +19,34 @@ public function authorizeResource($model, $name = null, array $options = [], $re
{
$method = array_last(explode('@', with($request ?: request())->route()->getActionName()));

if (! in_array($method, array_keys($this->resourceAbilityMap))) {
$map = $this->resourceAbilityMap();

if (! in_array($method, array_keys($map))) {
return new ControllerMiddlewareOptions($options);
}

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

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

/**
* Get the map of resource methods to ability names.
*
* @return array
*/
protected function resourceAbilityMap()
{
return [
'index' => 'view',
'create' => 'create',
'store' => 'create',
'show' => 'view',
'edit' => 'update',
'update' => 'update',
'delete' => 'delete',
];
}
}

0 comments on commit d0b5291

Please sign in to comment.