class User
{
public function hasPermissionTo($action, $department)
{
// first get permission
$permission = Permission::where('action', $action)->first();
// get all roles which have this permission
$roles = $permission->roles;
return DB::table('department_role_user')
->where('user_id', $this->id) // current user
->where('department_id', $department->id) // the dept
->whereIn('role_id', $roles->pluck('id')) // any of the roles
->exists();
}
}
用法
并像这样使用它 .
if ($user->hasPermissionTo('do-something', $someDept)) {
// allow action
} else {
// no way
}
1 回答
没有看到你的任何代码,我被迫在这里相当通用 . 但这是基本概念 .
架构
假设您已经拥有
departments
,users
,roles
和permissions
这样的表,那么您接下来需要的就是定义一个连接表 . 像这样的东西:department_role_user
department_id
//这个部门role_id
//此角色已分配给user_id
//这个用户授权
在User模型上定义类似
hasPermissionTo()
方法的内容 .定义
用法
并像这样使用它 .
这也应该与Laravel的Gates和Policies很好地配合 . 只需在门/策略定义中使用新的
hasPermissionTo()
方法 .