后台增加权限判断

后台可以快捷的通过 `request()->user()->can('权限标识');` 来判断某个用户是否拥有某个权限,便于权限区分
This commit is contained in:
青芒科技 2020-07-24 21:36:40 +08:00 committed by GitHub
parent 8d21dc8acc
commit c5650e80bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -82,5 +82,33 @@ class Users extends CatchModel
}
return array_unique($permissionIds);
}
/**
* 后台根据用户标识判断用户是否拥有某个权限
* @param string $permission_mark
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*
* 用法 request()->user()->can('permission@create');
*/
public function can($permission_mark = '')
{
// 超级管理员直接返回true
if (Utils::isSuperAdmin()){
return true;
}
// 查询当前用户的权限
$permissionIds = $this->getPermissionsBy();
// 根据mark找对应的id
$current = Permissions::where('permission_mark',$permission_mark)->find();
if (!$current){
return false;
}
// in_array 判断是否包含
return in_array($current['id'],$permissionIds);
}
}