修复端口映射管理bug

This commit is contained in:
Yohanes
2023-03-21 18:07:45 +08:00
parent 8e1dfc48ca
commit 559c6bd5f5
6 changed files with 148 additions and 98 deletions
@@ -56,6 +56,11 @@ public class LicenseController {
return licenseService.list(req);
}
@Mapping("/auth-list")
public List<LicenseListRes> queryCurUserLicense(LicenseListReq req) {
return licenseService.queryCurUserLicense(req);
}
@Post
@Mapping("/create")
@Authorization(onlyAdmin = true)
@@ -1,16 +1,16 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -39,83 +39,88 @@ import java.util.Set;
@Mapper
public interface LicenseMapper extends BaseMapper<LicenseDO> {
default List<LicenseDO> listAll() {
return this.selectList(new LambdaQueryWrapper<>());
}
default List<LicenseDO> listAll() {
return this.selectList(new LambdaQueryWrapper<>());
}
default List<LicenseDO> listByUserId(Integer userId) {
return this.selectList(new LambdaQueryWrapper<LicenseDO>()
.eq(LicenseDO::getUserId, userId)
);
}
default List<LicenseDO> listByUserId(Integer userId) {
return this.selectList(new LambdaQueryWrapper<LicenseDO>()
.eq(LicenseDO::getUserId, userId)
);
}
default void updateEnableStatus(Integer id, Integer enable, Date updateTime) {
this.update(null, new LambdaUpdateWrapper<LicenseDO>()
.eq(LicenseDO::getId, id)
.set(LicenseDO::getEnable, enable)
.set(LicenseDO::getUpdateTime, updateTime)
);
}
default LicenseDO queryById(Integer licenseId) {
return this.selectOne(new LambdaQueryWrapper<LicenseDO>()
.eq(LicenseDO::getId, licenseId));
}
default void updateOnlineStatus(Integer id, Integer isOnline, Date updateTime) {
this.update(null, new LambdaUpdateWrapper<LicenseDO>()
.eq(LicenseDO::getId, id)
.set(LicenseDO::getIsOnline, isOnline)
.set(LicenseDO::getUpdateTime, updateTime)
);
}
default void updateEnableStatus(Integer id, Integer enable, Date updateTime) {
this.update(null, new LambdaUpdateWrapper<LicenseDO>()
.eq(LicenseDO::getId, id)
.set(LicenseDO::getEnable, enable)
.set(LicenseDO::getUpdateTime, updateTime)
);
}
default void updateOnlineStatus(Integer isOnline, Date updateTime) {
this.update(null, new LambdaUpdateWrapper<LicenseDO>()
.set(LicenseDO::getIsOnline, updateTime)
.set(LicenseDO::getUpdateTime, updateTime)
);
}
default void updateOnlineStatus(Integer id, Integer isOnline, Date updateTime) {
this.update(null, new LambdaUpdateWrapper<LicenseDO>()
.eq(LicenseDO::getId, id)
.set(LicenseDO::getIsOnline, isOnline)
.set(LicenseDO::getUpdateTime, updateTime)
);
}
default void reset(Integer id, String key, Date updateTime) {
this.update(null, new LambdaUpdateWrapper<LicenseDO>()
.eq(LicenseDO::getId, id)
.set(LicenseDO::getKey, key)
.set(LicenseDO::getUpdateTime, updateTime)
);
}
default void updateOnlineStatus(Integer isOnline, Date updateTime) {
this.update(null, new LambdaUpdateWrapper<LicenseDO>()
.set(LicenseDO::getIsOnline, updateTime)
.set(LicenseDO::getUpdateTime, updateTime)
);
}
default LicenseDO findById(Integer id) {
return this.selectById(id);
}
default void reset(Integer id, String key, Date updateTime) {
this.update(null, new LambdaUpdateWrapper<LicenseDO>()
.eq(LicenseDO::getId, id)
.set(LicenseDO::getKey, key)
.set(LicenseDO::getUpdateTime, updateTime)
);
}
default void update(Integer id, String name, Date updateTime) {
this.update(null, new LambdaUpdateWrapper<LicenseDO>()
.eq(LicenseDO::getId, id)
.set(LicenseDO::getName, name)
.set(LicenseDO::getUpdateTime, updateTime)
);
}
default LicenseDO findById(Integer id) {
return this.selectById(id);
}
default List<LicenseDO> findByIds(Set<Integer> ids) {
return selectBatchIds(ids);
}
default void update(Integer id, String name, Date updateTime) {
this.update(null, new LambdaUpdateWrapper<LicenseDO>()
.eq(LicenseDO::getId, id)
.set(LicenseDO::getName, name)
.set(LicenseDO::getUpdateTime, updateTime)
);
}
default LicenseDO checkRepeat(Integer userId, String name) {
return this.selectOne(new LambdaQueryWrapper<LicenseDO>()
.eq(LicenseDO::getUserId, userId)
.eq(LicenseDO::getName, name)
.last("limit 1")
);
}
default List<LicenseDO> findByIds(Set<Integer> ids) {
return selectBatchIds(ids);
}
default LicenseDO checkRepeat(Integer userId, String name, Set<Integer> excludeIds) {
return this.selectOne(new LambdaQueryWrapper<LicenseDO>()
.eq(LicenseDO::getUserId, userId)
.eq(LicenseDO::getName, name)
.notIn(LicenseDO::getId, excludeIds)
.last("limit 1")
);
}
default LicenseDO checkRepeat(Integer userId, String name) {
return this.selectOne(new LambdaQueryWrapper<LicenseDO>()
.eq(LicenseDO::getUserId, userId)
.eq(LicenseDO::getName, name)
.last("limit 1")
);
}
default LicenseDO findByKey(String licenseKey) {
return selectOne(new LambdaQueryWrapper<LicenseDO>()
.eq(LicenseDO::getKey, licenseKey)
);
}
default LicenseDO checkRepeat(Integer userId, String name, Set<Integer> excludeIds) {
return this.selectOne(new LambdaQueryWrapper<LicenseDO>()
.eq(LicenseDO::getUserId, userId)
.eq(LicenseDO::getName, name)
.notIn(LicenseDO::getId, excludeIds)
.last("limit 1")
);
}
default LicenseDO findByKey(String licenseKey) {
return selectOne(new LambdaQueryWrapper<LicenseDO>()
.eq(LicenseDO::getKey, licenseKey)
);
}
}
@@ -24,6 +24,7 @@ import fun.asgc.neutrino.proxy.server.dal.entity.UserDO;
import fun.asgc.neutrino.proxy.server.util.ParamCheckUtil;
import ma.glasnost.orika.MapperFacade;
import org.apache.ibatis.solon.annotation.Db;
import org.jetbrains.annotations.Nullable;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.Lifecycle;
@@ -79,6 +80,12 @@ public class LicenseService implements Lifecycle {
List<LicenseDO> list = licenseMapper.selectList(new LambdaQueryWrapper<LicenseDO>()
.eq(LicenseDO::getEnable, EnableStatusEnum.ENABLE.getStatus())
);
List<LicenseListRes> licenseList = assembleConvertLicenses(list);
return licenseList;
}
@Nullable
private List<LicenseListRes> assembleConvertLicenses(List<LicenseDO> list) {
List<LicenseListRes> licenseList = mapperFacade.mapAsList(list, LicenseListRes.class);
if (!CollectionUtil.isEmpty(licenseList)) {
Set<Integer> userIds = licenseList.stream().map(LicenseListRes::getUserId).collect(Collectors.toSet());
@@ -220,4 +227,20 @@ public class LicenseService implements Lifecycle {
public void stop() throws Throwable {
licenseMapper.updateOnlineStatus(OnlineStatusEnum.OFFLINE.getStatus(), new Date());
}
/**
* 查询当前角色下的license,若为管理员 则返回全部license
*/
public List<LicenseListRes> queryCurUserLicense(LicenseListReq req) {
if(SystemContextHolder.isAdmin()){
return this.list(req);
}
List<LicenseDO> list = licenseMapper.selectList(new LambdaQueryWrapper<LicenseDO>()
.eq(LicenseDO::getEnable, EnableStatusEnum.ENABLE.getStatus())
.eq(LicenseDO::getUserId,SystemContextHolder.getUserId())
);
List<LicenseListRes> licenseList = assembleConvertLicenses(list);
return licenseList;
}
}
@@ -1,16 +1,16 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -33,13 +33,11 @@ import fun.asgc.neutrino.proxy.server.constant.EnableStatusEnum;
import fun.asgc.neutrino.proxy.server.constant.ExceptionConstant;
import fun.asgc.neutrino.proxy.server.controller.req.*;
import fun.asgc.neutrino.proxy.server.controller.res.*;
import fun.asgc.neutrino.proxy.server.dal.LicenseMapper;
import fun.asgc.neutrino.proxy.server.dal.PortGroupMapper;
import fun.asgc.neutrino.proxy.server.dal.PortMappingMapper;
import fun.asgc.neutrino.proxy.server.dal.PortPoolMapper;
import fun.asgc.neutrino.proxy.server.dal.entity.PortGroupDO;
import fun.asgc.neutrino.proxy.server.dal.entity.PortMappingDO;
import fun.asgc.neutrino.proxy.server.dal.entity.PortPoolDO;
import fun.asgc.neutrino.proxy.server.dal.entity.UserDO;
import fun.asgc.neutrino.proxy.server.dal.entity.*;
import fun.asgc.neutrino.proxy.server.util.ParamCheckUtil;
import ma.glasnost.orika.MapperFacade;
import org.apache.ibatis.solon.annotation.Db;
@@ -68,8 +66,10 @@ public class PortPoolService {
@Db
private PortGroupMapper portGroupMapper;
@Db
private PortMappingMapper portMappingMapper;
@Db
private PortMappingMapper portMappingMapper;
@Db
private LicenseMapper licenseMapper;
public PageInfo<PortPoolListRes> page(PageQuery pageQuery, PortPoolListReq req) {
Page<PortPoolListRes> result = PageHelper.startPage(pageQuery.getCurrent(), pageQuery.getSize());
@@ -82,19 +82,20 @@ public class PortPoolService {
List<PortPoolDO> list = portPoolMapper.selectList(new LambdaQueryWrapper<PortPoolDO>()
.eq(PortPoolDO::getEnable, EnableStatusEnum.ENABLE.getStatus())
);
return mapperFacade.mapAsList(this.filterUsedPorts(list), PortPoolListRes.class);
return mapperFacade.mapAsList(this.filterUsedPorts(list), PortPoolListRes.class);
}
private List<PortPoolDO> filterUsedPorts(List<PortPoolDO> list) {
//Gets the used ports
List<PortMappingDO> usePorts = portMappingMapper.selectList(new LambdaQueryWrapper<PortMappingDO>().orderByAsc(PortMappingDO::getId));
List<Integer> serverPorts = usePorts.stream().map(item -> item.getServerPort()).collect(Collectors.toList());
private List<PortPoolDO> filterUsedPorts(List<PortPoolDO> list) {
//Gets the used ports
List<PortMappingDO> usePorts = portMappingMapper.selectList(new LambdaQueryWrapper<PortMappingDO>().orderByAsc(PortMappingDO::getId));
return list.stream().filter(item -> !serverPorts.contains(item.getPort())).collect(Collectors.toList());
}
List<Integer> serverPorts = usePorts.stream().map(item -> item.getServerPort()).collect(Collectors.toList());
return list.stream().filter(item -> !serverPorts.contains(item.getPort())).collect(Collectors.toList());
}
public PortPoolCreateRes create(PortPoolCreateReq req) {
public PortPoolCreateRes create(PortPoolCreateReq req) {
PortPoolDO oldPortPoolDO = portPoolMapper.findByPort(req.getPort());
ParamCheckUtil.checkMustNull(oldPortPoolDO, ExceptionConstant.PORT_CANNOT_REPEAT);
PortGroupDO portGroupDO = portGroupMapper.selectById(req.getGroupId());
@@ -155,8 +156,13 @@ public class PortPoolService {
return new PortPoolUpdateGroupRes();
}
/**
* 管理员: 全局端口 + 当前选择的用户独占端口 + 当前选择license独占端口
* 游客:全局端口 + 当前选择用户独占端口 + 当前选择license独占端口
* 非管理员身份时:下拉选择license,只能选当前用户下的LICENSE
*/
public List<PortPoolListRes> getAvailablePortList(AvailablePortListReq req) {
UserDO user = SystemContextHolder.getUser();
return portPoolMapper.getAvailablePortList(req.getLicenseId(), user.getId());
LicenseDO licenseDO = licenseMapper.queryById(req.getLicenseId());
return portPoolMapper.getAvailablePortList(req.getLicenseId(), licenseDO.getUserId());
}
}