refactor(系统设置): 优化权限文件读取

This commit is contained in:
shiziyuan9527 2022-08-08 16:49:29 +08:00 committed by jianxing
parent 75b74bfaa2
commit 79863f3012
4 changed files with 21 additions and 18 deletions

View File

@ -16,6 +16,7 @@ import io.metersphere.controller.request.GroupRequest;
import io.metersphere.controller.request.group.EditGroupRequest;
import io.metersphere.controller.request.group.EditGroupUserRequest;
import io.metersphere.dto.*;
import io.metersphere.i18n.Translator;
import io.metersphere.log.utils.ReflexObjectUtil;
import io.metersphere.log.vo.DetailColumn;
import io.metersphere.log.vo.OperatingLogDetails;
@ -76,6 +77,15 @@ public class GroupService {
put(UserGroupType.PROJECT, "项目");
}};
private static GroupJson groupJson = null;
static {
try (InputStream inputStream = GroupService.class.getResourceAsStream("/permission.json")){
groupJson = JSON.parseObject(inputStream, GroupJson.class);
} catch (IOException e) {
LogUtil.error("load permissions file error. ", e);
}
}
public Pager<List<GroupDTO>> getGroupList(EditGroupRequest request) {
SessionUser user = SessionUtils.getUser();
List<UserGroupDTO> userGroup = extUserGroupMapper.getUserGroup(Objects.requireNonNull(user).getId(), request.getProjectId());
@ -159,29 +169,19 @@ public class GroupService {
userGroupPermissionMapper.deleteByExample(example);
}
public GroupPermissionDTO getGroupResource(Group g) {
public GroupPermissionDTO getGroupResource(Group group) {
GroupPermissionDTO dto = new GroupPermissionDTO();
InputStream permission = getClass().getResourceAsStream("/permission.json");
String type = g.getType();
String id = g.getId();
UserGroupPermissionExample userGroupPermissionExample = new UserGroupPermissionExample();
userGroupPermissionExample.createCriteria().andGroupIdEqualTo(id);
userGroupPermissionExample.createCriteria().andGroupIdEqualTo(group.getId());
List<UserGroupPermission> userGroupPermissions = userGroupPermissionMapper.selectByExample(userGroupPermissionExample);
List<String> permissionList = userGroupPermissions.stream().map(UserGroupPermission::getPermissionId).collect(Collectors.toList());
if (permission == null) {
throw new RuntimeException("读取文件失败!");
} else {
GroupJson group;
try {
group = JSON.parseObject(permission, GroupJson.class);
List<GroupResource> resource = group.getResource();
List<GroupPermission> permissions = group.getPermissions();
List<GroupResourceDTO> dtoPermissions = dto.getPermissions();
dtoPermissions.addAll(getResourcePermission(resource, permissions, type, permissionList));
} catch (IOException e) {
LogUtil.error(e);
}
if (groupJson == null) {
MSException.throwException(Translator.get("read_permission_file_fail"));
}
List<GroupResource> resource = groupJson.getResource();
List<GroupPermission> permissions = groupJson.getPermissions();
List<GroupResourceDTO> dtoPermissions = dto.getPermissions();
dtoPermissions.addAll(getResourcePermission(resource, permissions, group.getType(), permissionList));
return dto;
}

View File

@ -13,6 +13,7 @@ upload_fail=Upload fail
invalid_parameter=Invalid parameter!
name_already_exists=Name already exists
resource_not_exist=The resource does not exist or has been deleted
read_permission_file_fail=Failed to read permission file!
#user related
user_email_already_exists=User email already exists
user_id_is_null=User ID cannot be null

View File

@ -11,6 +11,7 @@ upload_fail=文件上传失败
invalid_parameter=非法的参数
name_already_exists=该名称已经存在
resource_not_exist=资源不存在或已删除
read_permission_file_fail=读取权限文件失败!
#user related
user_email_already_exists=用户邮箱已存在
user_id_is_null=用户ID不能为空

View File

@ -11,6 +11,7 @@ upload_fail=文件上傳失敗
invalid_parameter=非法的參數
name_already_exists=該名稱已經存在
resource_not_exist=資源不存在或已刪除
read_permission_file_fail=讀取權限文件失敗!
#user related
user_email_already_exists=用戶郵箱已存在
user_id_is_null=用戶ID不能為空