修改获取菜单
This commit is contained in:
@@ -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)
|
public function getUserResources(int $userId)
|
||||||
{
|
{
|
||||||
|
// 获取用户及其关联角色资源和直接分配的资源
|
||||||
$user = User::with(['roles.resources', 'resources'])->find($userId);
|
$user = User::with(['roles.resources', 'resources'])->find($userId);
|
||||||
|
|
||||||
if (!$user) {
|
if (!$user) {
|
||||||
return collect();
|
return collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
$all = collect();
|
$all = collect();
|
||||||
|
|
||||||
|
// 合并角色资源,排除不需要权限认证的资源
|
||||||
foreach ($user->roles as $role) {
|
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();
|
return $all->unique('id')->values();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user