From 0ee62d507cbae04354105ffa1a79ae4361d6cd49 Mon Sep 17 00:00:00 2001 From: chenlan <18339827479@163.com> Date: Mon, 14 Jul 2025 14:08:15 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/logic/PermLogic.php | 36 ++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/app/common/logic/PermLogic.php b/app/common/logic/PermLogic.php index b4fe819..d14ddd3 100644 --- a/app/common/logic/PermLogic.php +++ b/app/common/logic/PermLogic.php @@ -9,18 +9,50 @@ class PermLogic /** * 获取用户所有资源(合并角色+直接分配) */ + // public function getUserResources(int $userId) + // { + // $user = User::with(['roles.resources', 'resources'])->find($userId); + // if (!$user) { + // return collect(); + // } + + // $all = collect(); + // foreach ($user->roles as $role) { + // $all = $all->merge($role->resources); + // } + // $all = $all->merge($user->resources); + // return $all->unique('id')->values(); + // } public function getUserResources(int $userId) { + // 获取用户及其关联角色资源和直接分配的资源 $user = User::with(['roles.resources', 'resources'])->find($userId); + if (!$user) { return collect(); } $all = collect(); + + // 合并角色资源,排除不需要权限认证的资源 foreach ($user->roles as $role) { - $all = $all->merge($role->resources); + $all = $all->merge( + $role->resources->filter(function ($resource) { + // 只保留需要权限认证的资源 + return !$resource->no_auth; + }) + ); } - $all = $all->merge($user->resources); + + // 合并用户直接分配的资源,排除不需要权限认证的资源 + $all = $all->merge( + $user->resources->filter(function ($resource) { + // 只保留需要权限认证的资源 + return !$resource->no_auth; + }) + ); + + // 返回唯一的资源列表,并按 id 排序 return $all->unique('id')->values(); } From 373236b92f8437f8ce8140f68edcb528b80f48d0 Mon Sep 17 00:00:00 2001 From: mzl Date: Mon, 14 Jul 2025 14:22:57 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/logic/PermLogic.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/common/logic/PermLogic.php b/app/common/logic/PermLogic.php index d14ddd3..eca4b25 100644 --- a/app/common/logic/PermLogic.php +++ b/app/common/logic/PermLogic.php @@ -3,6 +3,7 @@ namespace app\common\logic; use app\common\model\User; +use app\common\model\Resource; class PermLogic { @@ -23,6 +24,9 @@ class PermLogic // $all = $all->merge($user->resources); // return $all->unique('id')->values(); // } + /** + * 获取用户所有资源(合并角色+直接分配),并加入公共菜单 + */ public function getUserResources(int $userId) { // 获取用户及其关联角色资源和直接分配的资源 @@ -38,7 +42,7 @@ class PermLogic foreach ($user->roles as $role) { $all = $all->merge( $role->resources->filter(function ($resource) { - // 只保留需要权限认证的资源 + // 只保留需要权限认证的资源(或公共资源) return !$resource->no_auth; }) ); @@ -47,15 +51,20 @@ class PermLogic // 合并用户直接分配的资源,排除不需要权限认证的资源 $all = $all->merge( $user->resources->filter(function ($resource) { - // 只保留需要权限认证的资源 + // 只保留需要权限认证的资源(或公共资源) return !$resource->no_auth; }) ); + // 加入公共菜单,公共菜单不管是否授权,所有人都能访问 + $publicResources = Resource::where('no_auth', 1)->get(); // 获取所有公共菜单 + $all = $all->merge($publicResources); + // 返回唯一的资源列表,并按 id 排序 return $all->unique('id')->values(); } + /** * 构建目录/菜单树,节点包含权限与打开方式,拼接完整路径,并对子级菜单component处理 * @param \Illuminate\Support\Collection $resources