From c5650e80bd4edd15258cab005f5f4ce3f8635b07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9D=92=E8=8A=92=E7=A7=91=E6=8A=80?= <67047984+qingmang@users.noreply.github.com> Date: Fri, 24 Jul 2020 21:36:40 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=90=8E=E5=8F=B0=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=9D=83=E9=99=90=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 后台可以快捷的通过 `request()->user()->can('权限标识');` 来判断某个用户是否拥有某个权限,便于权限区分 --- catch/permissions/model/Users.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/catch/permissions/model/Users.php b/catch/permissions/model/Users.php index 26c5074..d27826f 100644 --- a/catch/permissions/model/Users.php +++ b/catch/permissions/model/Users.php @@ -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); } } From cdf6efde1ddb9ec6ccbba25b88aad09a6dc4c358 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9D=92=E8=8A=92=E7=A7=91=E6=8A=80?= <67047984+qingmang@users.noreply.github.com> Date: Fri, 24 Jul 2020 22:16:19 +0800 Subject: [PATCH 2/3] Update Users.php --- catch/permissions/model/Users.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/catch/permissions/model/Users.php b/catch/permissions/model/Users.php index d27826f..e66324f 100644 --- a/catch/permissions/model/Users.php +++ b/catch/permissions/model/Users.php @@ -85,7 +85,7 @@ class Users extends CatchModel } /** - * 后台根据用户标识判断用户是否拥有某个权限 + * 后台根据权限标识判断用户是否拥有某个权限 * @param string $permission_mark * @return bool * @throws \think\db\exception\DataNotFoundException From 454fa43f8c6970c1d80ea43970106c84d7b24e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9D=92=E8=8A=92=E7=A7=91=E6=8A=80?= <67047984+qingmang@users.noreply.github.com> Date: Fri, 24 Jul 2020 22:16:34 +0800 Subject: [PATCH 3/3] Update Users.php