refactor: 修改缓存加载逻辑.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
### 查询当前用户已授权的主机分组及主机
|
||||
GET {{baseUrl}}/asset/authorized-data/current-host-group
|
||||
### 查询当前用户已授权的主机
|
||||
GET {{baseUrl}}/asset/authorized-data/current-host
|
||||
Authorization: {{token}}
|
||||
|
||||
### 查询当前用户已授权的主机秘钥
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.orion.ops.framework.log.core.annotation.IgnoreLog;
|
||||
import com.orion.ops.framework.log.core.enums.IgnoreLogMode;
|
||||
import com.orion.ops.framework.security.core.utils.SecurityUtils;
|
||||
import com.orion.ops.framework.web.core.annotation.RestWrapper;
|
||||
import com.orion.ops.module.asset.entity.vo.AuthorizedHostGroupWrapperVO;
|
||||
import com.orion.ops.module.asset.entity.vo.AuthorizedHostWrapperVO;
|
||||
import com.orion.ops.module.asset.entity.vo.HostIdentityVO;
|
||||
import com.orion.ops.module.asset.entity.vo.HostKeyVO;
|
||||
import com.orion.ops.module.asset.service.AssetAuthorizedDataService;
|
||||
@@ -38,9 +38,9 @@ public class AssetAuthorizedDataServiceController {
|
||||
private AssetAuthorizedDataService assetAuthorizedDataService;
|
||||
|
||||
@IgnoreLog(IgnoreLogMode.RET)
|
||||
@GetMapping("/current-host-group")
|
||||
@Operation(summary = "查询当前用户已授权的主机分组及主机")
|
||||
public AuthorizedHostGroupWrapperVO getCurrentAuthorizedHostGroup() {
|
||||
@GetMapping("/current-host")
|
||||
@Operation(summary = "查询当前用户已授权的主机")
|
||||
public AuthorizedHostWrapperVO getCurrentAuthorizedHostGroup() {
|
||||
return assetAuthorizedDataService.getUserAuthorizedHostGroup(SecurityUtils.getLoginUserId());
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,8 @@ import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 已授权的主机分组 视图响应对象
|
||||
@@ -20,7 +22,7 @@ import java.util.List;
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Schema(name = "AuthorizedHostGroupWrapperVO", description = "已授权的主机分组 视图响应对象")
|
||||
public class AuthorizedHostGroupWrapperVO {
|
||||
public class AuthorizedHostWrapperVO {
|
||||
|
||||
@Schema(description = "授权的主机分组")
|
||||
private List<HostGroupTreeVO> groupTree;
|
||||
@@ -28,4 +30,7 @@ public class AuthorizedHostGroupWrapperVO {
|
||||
@Schema(description = "授权的主机列表")
|
||||
private List<HostVO> hostList;
|
||||
|
||||
@Schema(description = "分组树节点映射 'groupId':hostIdList")
|
||||
private Map<String, Set<Long>> treeNodes;
|
||||
|
||||
}
|
||||
@@ -44,7 +44,4 @@ public class HostGroupTreeVO implements TreeNode<HostGroupTreeVO>, Serializable
|
||||
@Schema(description = "子节点")
|
||||
private List<HostGroupTreeVO> children;
|
||||
|
||||
@Schema(description = "分组内主机")
|
||||
private List<HostVO> hostList;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.orion.ops.module.asset.service;
|
||||
|
||||
import com.orion.ops.module.asset.entity.request.asset.AssetAuthorizedDataQueryRequest;
|
||||
import com.orion.ops.module.asset.entity.vo.AuthorizedHostGroupWrapperVO;
|
||||
import com.orion.ops.module.asset.entity.vo.AuthorizedHostWrapperVO;
|
||||
import com.orion.ops.module.asset.entity.vo.HostIdentityVO;
|
||||
import com.orion.ops.module.asset.entity.vo.HostKeyVO;
|
||||
import com.orion.ops.module.infra.enums.DataPermissionTypeEnum;
|
||||
@@ -27,12 +27,12 @@ public interface AssetAuthorizedDataService {
|
||||
List<Long> getAuthorizedDataRelId(DataPermissionTypeEnum type, AssetAuthorizedDataQueryRequest request);
|
||||
|
||||
/**
|
||||
* 查询用户已授权的主机分组和主机
|
||||
* 查询用户已授权的主机主机
|
||||
*
|
||||
* @param userId userId
|
||||
* @return group
|
||||
*/
|
||||
AuthorizedHostGroupWrapperVO getUserAuthorizedHostGroup(Long userId);
|
||||
AuthorizedHostWrapperVO getUserAuthorizedHostGroup(Long userId);
|
||||
|
||||
/**
|
||||
* 查询用户已授权的主机秘钥
|
||||
|
||||
@@ -74,7 +74,7 @@ public class AssetAuthorizedDataServiceImpl implements AssetAuthorizedDataServic
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthorizedHostGroupWrapperVO getUserAuthorizedHostGroup(Long userId) {
|
||||
public AuthorizedHostWrapperVO getUserAuthorizedHostGroup(Long userId) {
|
||||
if (systemUserApi.isAdminUser(userId)) {
|
||||
// 管理员查询所有
|
||||
return this.buildUserAuthorizedHostGroup(null);
|
||||
@@ -83,7 +83,7 @@ public class AssetAuthorizedDataServiceImpl implements AssetAuthorizedDataServic
|
||||
List<Long> authorizedIdList = dataPermissionApi.getUserAuthorizedRelIdList(DataPermissionTypeEnum.HOST_GROUP, userId);
|
||||
if (authorizedIdList.isEmpty()) {
|
||||
// 无数据
|
||||
return AuthorizedHostGroupWrapperVO.builder()
|
||||
return AuthorizedHostWrapperVO.builder()
|
||||
.groupTree(Lists.empty())
|
||||
.hostList(Lists.empty())
|
||||
.build();
|
||||
@@ -142,60 +142,103 @@ public class AssetAuthorizedDataServiceImpl implements AssetAuthorizedDataServic
|
||||
* @param authorizedGroupIdList authorizedGroupIdList
|
||||
* @return tree
|
||||
*/
|
||||
private AuthorizedHostGroupWrapperVO buildUserAuthorizedHostGroup(List<Long> authorizedGroupIdList) {
|
||||
private AuthorizedHostWrapperVO buildUserAuthorizedHostGroup(List<Long> authorizedGroupIdList) {
|
||||
final boolean allData = Lists.isEmpty(authorizedGroupIdList);
|
||||
AuthorizedHostGroupWrapperVO wrapper = new AuthorizedHostGroupWrapperVO();
|
||||
// 查询主机列表
|
||||
List<HostVO> hosts = hostService.getHostListByCache();
|
||||
Map<Long, HostVO> hostMap = hosts.stream()
|
||||
.collect(Collectors.toMap(HostVO::getId, Function.identity(), Functions.right()));
|
||||
// 查询分组引用
|
||||
Map<Long, Set<Long>> groupRel = dataGroupRelApi.getGroupRelList(DataGroupTypeEnum.HOST);
|
||||
AuthorizedHostWrapperVO wrapper = new AuthorizedHostWrapperVO();
|
||||
// TODO async get 最近连接
|
||||
// TODO async get 我的收藏
|
||||
// 查询分组
|
||||
List<DataGroupDTO> dataGroup = dataGroupApi.getDataGroupList(DataGroupTypeEnum.HOST);
|
||||
// 过滤分组
|
||||
// 查询分组引用
|
||||
Map<Long, Set<Long>> dataGroupRel = dataGroupRelApi.getGroupRelList(DataGroupTypeEnum.HOST);
|
||||
// 过滤已经授权的分组
|
||||
if (!allData) {
|
||||
// 构建已授权的分组
|
||||
List<DataGroupDTO> relNodes = new ArrayList<>();
|
||||
TreeUtils.getAllNodes(dataGroup, authorizedGroupIdList, relNodes);
|
||||
dataGroup = new ArrayList<>(new HashSet<>(relNodes));
|
||||
}
|
||||
// 设置组内数据
|
||||
// 设置主机分组树
|
||||
wrapper.setGroupTree(this.getAuthorizedHostGroupTree(dataGroup));
|
||||
// 设置主机分组下的主机
|
||||
wrapper.setTreeNodes(this.getAuthorizedHostGroupNodes(allData, dataGroup, dataGroupRel, authorizedGroupIdList));
|
||||
// 设置已授权的所有主机
|
||||
wrapper.setHostList(this.getAuthorizedHostList(allData, dataGroup, dataGroupRel, authorizedGroupIdList));
|
||||
// TODO set 最近连接
|
||||
// TODO set 我的收藏
|
||||
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建主机分组树
|
||||
*
|
||||
* @param dataGroup dataGroup
|
||||
* @return tree
|
||||
*/
|
||||
private List<HostGroupTreeVO> getAuthorizedHostGroupTree(List<DataGroupDTO> dataGroup) {
|
||||
List<HostGroupTreeVO> groupList = HostGroupConvert.MAPPER.toList(dataGroup);
|
||||
groupList.stream()
|
||||
// 因为可能父菜单没有授权 这里需要判断组
|
||||
.filter(s -> allData || authorizedGroupIdList.contains(s.getId()))
|
||||
.forEach(s -> {
|
||||
List<HostVO> groupHosts = Lists.stream(groupRel.get(s.getId()))
|
||||
.map(hostMap::get)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
s.setHostList(groupHosts);
|
||||
});
|
||||
// 构建主机树
|
||||
HostGroupTreeVO rootNode = HostGroupTreeVO.builder()
|
||||
.id(Const.ROOT_PARENT_ID)
|
||||
.sort(Const.DEFAULT_SORT)
|
||||
.build();
|
||||
TreeUtils.buildGroupTree(rootNode, groupList);
|
||||
wrapper.setGroupTree(rootNode.getChildren());
|
||||
// 设置授权的主机
|
||||
return rootNode.getChildren();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取主机分组树 主机节点映射
|
||||
*
|
||||
* @param allData allData
|
||||
* @param dataGroup dataGroup
|
||||
* @param dataGroupRel dataGroupRel
|
||||
* @param authorizedGroupIdList authorizedGroupIdList
|
||||
* @return hostGroupId:hostIdList
|
||||
*/
|
||||
private Map<String, Set<Long>> getAuthorizedHostGroupNodes(boolean allData,
|
||||
List<DataGroupDTO> dataGroup,
|
||||
Map<Long, Set<Long>> dataGroupRel,
|
||||
List<Long> authorizedGroupIdList) {
|
||||
Map<String, Set<Long>> result = new HashMap<>();
|
||||
dataGroup.stream()
|
||||
.map(DataGroupDTO::getId)
|
||||
// 因为可能父菜单没有授权 这里需要判断分组权限
|
||||
.filter(id -> allData || authorizedGroupIdList.contains(id))
|
||||
.forEach(s -> result.put(String.valueOf(s), dataGroupRel.get(s)));
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询已授权的所有主机
|
||||
*
|
||||
* @param allData allData
|
||||
* @param dataGroup dataGroup
|
||||
* @param dataGroupRel dataGroupRel
|
||||
* @param authorizedGroupIdList authorizedGroupIdList
|
||||
* @return hosts
|
||||
*/
|
||||
private List<HostVO> getAuthorizedHostList(boolean allData,
|
||||
List<DataGroupDTO> dataGroup,
|
||||
Map<Long, Set<Long>> dataGroupRel,
|
||||
List<Long> authorizedGroupIdList) {
|
||||
// 查询主机列表
|
||||
List<HostVO> hosts = hostService.getHostListByCache();
|
||||
// 全部数据直接返回
|
||||
if (allData) {
|
||||
// 设置全部数据
|
||||
wrapper.setHostList(hosts);
|
||||
} else {
|
||||
// 仅设置已授权的数据
|
||||
List<HostVO> groupHosts = groupList.stream()
|
||||
.filter(s -> authorizedGroupIdList.contains(s.getId()))
|
||||
.map(s -> groupRel.get(s.getId()))
|
||||
.filter(Lists::isNoneEmpty)
|
||||
.flatMap(Collection::stream)
|
||||
.map(hostMap::get)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
wrapper.setHostList(groupHosts);
|
||||
return hosts;
|
||||
}
|
||||
return wrapper;
|
||||
Map<Long, HostVO> hostMap = hosts.stream()
|
||||
.collect(Collectors.toMap(HostVO::getId, Function.identity(), Functions.right()));
|
||||
// 仅设置已授权的数据
|
||||
return dataGroup.stream()
|
||||
.map(DataGroupDTO::getId)
|
||||
.filter(authorizedGroupIdList::contains)
|
||||
.map(dataGroupRel::get)
|
||||
.filter(Lists::isNoneEmpty)
|
||||
.flatMap(Collection::stream)
|
||||
.map(hostMap::get)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user