Merge pull request #13 from qingmang/patch-1

后台增加权限判断
This commit is contained in:
JaguarJack 2020-07-25 07:50:13 +08:00 committed by GitHub
commit 7fc50c9916
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);
}
}