Php Laravel Nova的用户管理;允许创建用户,但仅限于某些角色
我有两个模型:用户和角色。一个用户属于多个角色,有一个透视表用户角色来链接这些角色 我有三个角色:管理员>经理>员工 我想:Php Laravel Nova的用户管理;允许创建用户,但仅限于某些角色,php,laravel,user-management,laravel-nova,Php,Laravel,User Management,Laravel Nova,我有两个模型:用户和角色。一个用户属于多个角色,有一个透视表用户角色来链接这些角色 我有三个角色:管理员>经理>员工 我想: 经理只能查看/创建/更新/删除员工用户 管理员可以查看/创建/更新/删除具有任何角色的任何用户 这似乎是一个常见的问题,但我很难让这一切发生在Nova内部的Laravel政策上。(这确实需要在Nova内部进行管理。) 用户模型: namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use \App\Role;
class User extends Authenticatable
{
use Notifiable;
public function roles()
{
return $this->belongsToMany(Role::class)->using('App\RoleUser');
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
榜样:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use \App\User;
class Role extends Model
{
public function users()
{
return $this->belongsToMany(User::class)->using('App\RoleUser');
}
}
但是,这会在用户资源页面上向登录的管理员公开管理员列表。我真的不知道这是不是一个好办法。我需要重新考虑整个设置吗?有没有更干净/更简单的方法我不知道?再次强调,这必须与Nova合作。关于这方面的任何建议都将非常好,谢谢-
public function before(User $user, $ability)
{
if (count($user->roles->whereInStrict('name', 'Admin')) > 0) {
return true;
}
}
public function view(User $user, User $user2)
{
if (count($user->roles->whereInStrict('name', 'Manager')) > 0 && count($user2->roles->whereInStrict('name', 'Employee')) > 0) {
return true;
}
return false;
}