Reputation: 14791
I am developing a web application using Laravel. I am using Nova for admin panel. What I am doing now is I am authorizing my resource using policies as mentioned in the documentation. But seems like it is not working. This is what I have done so far. I have created a nova resource like this.
class Item extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = \App\Models\Item::class;
/**
* The single value that should be used to represent the resource when being displayed.
*
* @var string
*/
public static $title = 'id';
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'id',
];
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
ID::make()->sortable(),
];
}
/**
* Get the cards available for the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function cards(Request $request)
{
return [];
}
/**
* Get the filters available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function filters(Request $request)
{
return [];
}
/**
* Get the lenses available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function lenses(Request $request)
{
return [];
}
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [];
}
}
Then I created a Laravel Model class for that resource with the name Item.
Then I created policy.
class ItemPolicy
{
use HandlesAuthorization;
public function viewAny(User $user)
{
return true;
}
public function view(User $user, $item)
{
return true;
}
public function create(User $user)
{
return false;
}
public function update(User $user, $item)
{
return false;
}
public function delete(User $user, $item)
{
return false;
}
public function restore(User $user, $item)
{
return false;
}
public function forceDelete(User $user, $item)
{
return false;
}
}
I register the policy in the in AuthServiceProvider.
protected $policies = [
Item::class => ItemPolicy::class,
];
When I see the list of item in the nova admin panel, I can still create the item. What is wrong? The option for creating an item should be hidden.
Upvotes: 6
Views: 7662
Reputation: 743
Your registered your policy wrong
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
// 'App\Model' => 'App\Policies\ModelPolicy',
'App\Item' => 'App\Policies\ItemPolicy',
];
Upvotes: 0
Reputation: 1
Using rolePolicy or permissionPolicy method to define policy
// in app/Providers/NovaServiceProvider.php
// ...
public function tools()
{
return [
// ...
\Vyuldashev\NovaPermission\NovaPermissionTool::make()
->rolePolicy(RolePolicy::class)
->permissionPolicy(PermissionPolicy::class),
];
}
Upvotes: 0
Reputation: 126
Check AuthServiceProvider once again.
where you define the policy mapping array:
protected $policies = [
Item::class => ItemPolicy::class,
];
The Item - should be your Model, not Nova Resource
Upvotes: 1
Reputation: 385
Remove the viewAny()
method from the ItemPolicyPolicy
class
Upvotes: 0
Reputation: 1239
Add the following to your Nova resource class:
public static function authorizable()
{
return true;
}
Upvotes: 4
Reputation: 466
Maybe because you are missing model type in method arguments
Add Item $item
in all methods where passing $item, like this:
public function update(User $user, Item $item)
{
return false;
}
Also you can exclude all methods you want to be unavailable and by default them will be disabled
Upvotes: -1