Skip to content
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.2] Move ability map to a method #13214

Merged
merged 1 commit into from
Apr 19, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
Copy link
Contributor

@silverqx silverqx May 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related bug #13716 @JosephSilber

];
}
}