【发布时间】:2023-03-23 19:55:02
【问题描述】:
我目前正在将 Nova 用于我的后台应用程序,并尝试根据用户的角色授予用户身份验证。我已经编写了一些策略,除了一个之外,它们都有效。我一遍又一遍地检查它,看看我是否错过了什么,但我找不到任何东西。我需要你们帮助重新审视一下,以帮助我弄清楚我做错了什么。这是我正在使用的代码
订单政策
<?php
namespace App\Policies;
use App\Models\Order;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class OrderPolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can view any order.
*
* @param \App\User $user
* @return mixed
*/
public function viewAny(User $user)
{
return $user->hasAnyRole(['crm']);
}
}
订购新星资源
class Order extends Resource
{
public static function indexQuery(NovaRequest $request, $query)
{
return $query->where('status_id', 'paid');
}
public static $model = 'App\Models\Order';
public static $title = 'id';
public static $search = [
'id', 'reference'
];
public function fields(Request $request)
{
return [
// fields that should be displayed
];
}
// other functions
}
AuthServiceProvider
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
// other policies
'App\Models\Order' => 'App\Policies\OrderPolicy',
];
public function boot()
{
$this->registerPolicies();
// ....
}
}
我在这个政策上做错了什么?
【问题讨论】:
-
究竟是什么不起作用?用户能看能不能看,能不能多解释一下
-
因此具有不同角色的用户仍然可以查看资源。
标签: laravel laravel-nova