菜单绑定系统

This commit is contained in:
2025-07-25 18:10:11 +08:00
parent 32c6e8d425
commit cf6fc7033a
6 changed files with 63 additions and 17 deletions
+9 -5
View File
@@ -4,14 +4,17 @@ namespace app\common\logic;
use app\common\model\User;
use app\common\model\Resource;
use app\common\model\Systems;
class PermLogic
{
/**
* 获取用户所有资源(合并角色+直接分配),并加入公共菜单,排序按sort字段
*/
public function getUserResources(int $userId)
public function getUserResources(int $userId, string $systemCode)
{
$system = Systems::where('code', $systemCode)->first();
// 获取用户及其关联角色资源和直接分配的资源
$user = User::with(['roles.resources', 'resources'])->find($userId);
@@ -24,9 +27,10 @@ class PermLogic
// 合并角色资源,排除不需要权限认证的资源
foreach ($user->roles as $role) {
$all = $all->merge(
$role->resources->filter(function ($resource) {
$role->resources->filter(function ($resource) use($system) {
// var_dump($resource->toArray());
// 只保留需要权限认证的资源(或公共资源)
return !$resource->no_auth;
return !$resource->no_auth && $system->id == $resource->system_id;
})
);
}
@@ -269,9 +273,9 @@ class PermLogic
/**
* 获取用户所有权限code
*/
public function getPermissionCodes(int $userId): array
public function getPermissionCodes(int $userId, string $systemCode): array
{
$resources = $this->getUserResources($userId);
$resources = $this->getUserResources($userId, $systemCode);
return $resources
->filter(fn($res) => $res->type === 'permission')
->pluck('code')