From 94efd8d80ef4dbfc0f71982b311047a268a8a44c Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Wed, 16 Jun 2021 16:20:34 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):?= =?UTF-8?q?=20=E7=94=A8=E6=88=B7=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/constants/UserGroupConstants.java | 3 + .../request/member/UserRequest.java | 4 +- .../excel/domain/UserExcelData.java | 10 +- .../excel/domain/UserExcelDataCn.java | 40 +++++- .../excel/domain/UserExcelDataTw.java | 40 +++++- .../excel/domain/UserExcelDataUs.java | 33 ++++- .../excel/listener/UserDataListener.java | 128 ++++++++++++------ .../io/metersphere/service/UserService.java | 106 ++++++++++++--- .../components/settings/system/User.vue | 5 +- .../settings/system/components/UserImport.vue | 17 ++- 10 files changed, 311 insertions(+), 75 deletions(-) diff --git a/backend/src/main/java/io/metersphere/commons/constants/UserGroupConstants.java b/backend/src/main/java/io/metersphere/commons/constants/UserGroupConstants.java index cca5d60bac..4ac69fdfd8 100644 --- a/backend/src/main/java/io/metersphere/commons/constants/UserGroupConstants.java +++ b/backend/src/main/java/io/metersphere/commons/constants/UserGroupConstants.java @@ -1,5 +1,8 @@ package io.metersphere.commons.constants; +/** + * 系统内置用户组常量 + */ public class UserGroupConstants { public static final String ADMIN = "admin"; public static final String ORG_ADMIN = "org_admin"; diff --git a/backend/src/main/java/io/metersphere/controller/request/member/UserRequest.java b/backend/src/main/java/io/metersphere/controller/request/member/UserRequest.java index 4dd6870a2b..03f356ea85 100644 --- a/backend/src/main/java/io/metersphere/controller/request/member/UserRequest.java +++ b/backend/src/main/java/io/metersphere/controller/request/member/UserRequest.java @@ -11,7 +11,9 @@ import java.util.Map; @Getter @Setter public class UserRequest extends User { - + /** + * 弃用 + */ private List> roles = new ArrayList<>(); private List> groups = new ArrayList<>(); diff --git a/backend/src/main/java/io/metersphere/excel/domain/UserExcelData.java b/backend/src/main/java/io/metersphere/excel/domain/UserExcelData.java index 5678ccbdde..61f301853d 100644 --- a/backend/src/main/java/io/metersphere/excel/domain/UserExcelData.java +++ b/backend/src/main/java/io/metersphere/excel/domain/UserExcelData.java @@ -37,8 +37,16 @@ public class UserExcelData { @ExcelIgnore private String testerWorkspace; @ExcelIgnore + private String userIsProjectAdmin; + @ExcelIgnore + private String proAdminProject; + @ExcelIgnore + private String userIsProjectMember; + @ExcelIgnore + private String proMemberProject; + @ExcelIgnore private String userIsViewer; @ExcelIgnore - private String viewerWorkspace; + private String viewerProject; } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/excel/domain/UserExcelDataCn.java b/backend/src/main/java/io/metersphere/excel/domain/UserExcelDataCn.java index dd3d365fc6..abf6ad57e5 100644 --- a/backend/src/main/java/io/metersphere/excel/domain/UserExcelDataCn.java +++ b/backend/src/main/java/io/metersphere/excel/domain/UserExcelDataCn.java @@ -18,6 +18,7 @@ public class UserExcelDataCn extends UserExcelData { @NotBlank(message = "{cannot_be_null}") @Length(max = 255) + @ColumnWidth(20) @ExcelProperty("姓名") private String name; @@ -37,14 +38,17 @@ public class UserExcelDataCn extends UserExcelData { @ExcelProperty("电话") @Length(max = 11) + @ColumnWidth(20) @Pattern(regexp = "^1(3|4|5|6|7|8|9)\\d{9}$", message = "{user_import_format_wrong}") private String phone; @NotBlank(message = "{cannot_be_null}") + @ColumnWidth(30) @ExcelProperty("是否是系统管理员(是/否)") private String userIsAdmin; @NotBlank(message = "{cannot_be_null}") + @ColumnWidth(30) @ExcelProperty("是否是组织管理员(是/否)") private String userIsOrgAdmin; @@ -55,6 +59,7 @@ public class UserExcelDataCn extends UserExcelData { @NotBlank(message = "{cannot_be_null}") @ExcelProperty("是否是组织成员(是/否)") + @ColumnWidth(30) private String userIsOrgMember; @Length(max = 100) @@ -63,29 +68,52 @@ public class UserExcelDataCn extends UserExcelData { private String orgMemberOrganization; @NotBlank(message = "{cannot_be_null}") - @ExcelProperty("是否是测试经理(是/否)") + @ExcelProperty("是否是工作空间管理员(是/否)") + @ColumnWidth(30) private String userIsTestManager; @Length(max = 100) @ColumnWidth(30) - @ExcelProperty("测试经理工作空间") + @ExcelProperty("工作空间管理员应用工作空间") private String testManagerWorkspace; @NotBlank(message = "{cannot_be_null}") - @ExcelProperty("是否是测试成员(是/否)") + @ExcelProperty("是否是工作空间成员(是/否)") + @ColumnWidth(30) private String userIsTester; @Length(max = 100) @ColumnWidth(30) - @ExcelProperty("测试成员工作空间") + @ExcelProperty("工作空间成员应用工作空间") private String testerWorkspace; @NotBlank(message = "{cannot_be_null}") + @ExcelProperty("是否是项目管理员(是/否)") + @ColumnWidth(30) + private String userIsProjectAdmin; + + @Length(max = 100) + @ColumnWidth(30) + @ExcelProperty("项目管理员应用项目") + private String proAdminProject; + + @NotBlank(message = "{cannot_be_null}") + @ExcelProperty("是否是项目成员(是/否)") + @ColumnWidth(30) + private String userIsProjectMember; + + @Length(max = 100) + @ColumnWidth(30) + @ExcelProperty("项目成员应用项目") + private String proMemberProject; + + @NotBlank(message = "{cannot_be_null}") + @ColumnWidth(30) @ExcelProperty("是否是只读用戶(是/否)") private String userIsViewer; @Length(max = 100) @ColumnWidth(30) - @ExcelProperty("只读用户工作空间") - private String viewerWorkspace; + @ExcelProperty("只读用戶应用项目") + private String viewerProject; } diff --git a/backend/src/main/java/io/metersphere/excel/domain/UserExcelDataTw.java b/backend/src/main/java/io/metersphere/excel/domain/UserExcelDataTw.java index 7b17ff2fef..be6621ceae 100644 --- a/backend/src/main/java/io/metersphere/excel/domain/UserExcelDataTw.java +++ b/backend/src/main/java/io/metersphere/excel/domain/UserExcelDataTw.java @@ -18,6 +18,7 @@ public class UserExcelDataTw extends TestCaseExcelData { @NotBlank(message = "{cannot_be_null}") @Length(max = 255) + @ColumnWidth(20) @ExcelProperty("姓名") private String name; @@ -37,14 +38,17 @@ public class UserExcelDataTw extends TestCaseExcelData { @ExcelProperty("電話") @Length(max = 11) + @ColumnWidth(30) @Pattern(regexp = "^1(3|4|5|6|7|8|9)\\d{9}$", message = "{user_import_format_wrong}") private String phone; @NotBlank(message = "{cannot_be_null}") + @ColumnWidth(30) @ExcelProperty("是否是系統管理員(是/否)") private String userIsAdmin; @NotBlank(message = "{cannot_be_null}") + @ColumnWidth(30) @ExcelProperty("是否是組織管理員(是/否)") private String userIsOrgAdmin; @@ -54,6 +58,7 @@ public class UserExcelDataTw extends TestCaseExcelData { private String orgAdminOrganization; @NotBlank(message = "{cannot_be_null}") + @ColumnWidth(30) @ExcelProperty("是否是組織成員(是/否)") private String userIsOrgMember; @@ -63,29 +68,52 @@ public class UserExcelDataTw extends TestCaseExcelData { private String orgMemberOrganization; @NotBlank(message = "{cannot_be_null}") - @ExcelProperty("是否是測試經理(是/否)") + @ColumnWidth(30) + @ExcelProperty("是否是工作空間管理員(是/否)") private String userIsTestManager; @Length(max = 100) @ColumnWidth(30) - @ExcelProperty("測試經理工作空間") + @ExcelProperty("工作空間管理員應用工作空間") private String testManagerWorkspace; @NotBlank(message = "{cannot_be_null}") - @ExcelProperty("是否是測試成員(是/否)") + @ColumnWidth(30) + @ExcelProperty("是否是工作空間成員(是/否)") private String userIsTester; @Length(max = 100) @ColumnWidth(30) - @ExcelProperty("測試成員工作空間") + @ExcelProperty("工作空間成員應用工作空間") private String testerWorkspace; @NotBlank(message = "{cannot_be_null}") + @ColumnWidth(30) + @ExcelProperty("是否是項目管理員(是/否)") + private String userIsProjectAdmin; + + @Length(max = 100) + @ColumnWidth(30) + @ExcelProperty("項目管理員應用項目") + private String proAdminProject; + + @NotBlank(message = "{cannot_be_null}") + @ColumnWidth(30) + @ExcelProperty("是否是項目成員(是/否)") + private String userIsProjectMember; + + @Length(max = 100) + @ColumnWidth(30) + @ExcelProperty("項目成員應用項目") + private String proMemberProject; + + @NotBlank(message = "{cannot_be_null}") + @ColumnWidth(30) @ExcelProperty("是否是只讀用戶(是/否)") private String userIsViewer; @Length(max = 100) @ColumnWidth(30) - @ExcelProperty("只讀用戶工作空間") - private String viewerWorkspace; + @ExcelProperty("只讀用戶應用項目") + private String viewerProject; } diff --git a/backend/src/main/java/io/metersphere/excel/domain/UserExcelDataUs.java b/backend/src/main/java/io/metersphere/excel/domain/UserExcelDataUs.java index a64e92e63d..f11e329d0e 100644 --- a/backend/src/main/java/io/metersphere/excel/domain/UserExcelDataUs.java +++ b/backend/src/main/java/io/metersphere/excel/domain/UserExcelDataUs.java @@ -1,6 +1,5 @@ package io.metersphere.excel.domain; -import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.write.style.ColumnWidth; import lombok.Data; @@ -20,6 +19,7 @@ public class UserExcelDataUs extends UserExcelData { @NotBlank(message = "{cannot_be_null}") @Length(max = 255) + @ColumnWidth(20) @ExcelProperty("Name") private String name; @@ -39,14 +39,17 @@ public class UserExcelDataUs extends UserExcelData { @ExcelProperty("Phone") @Length(max = 11) + @ColumnWidth(30) @Pattern(regexp = "^1(3|4|5|6|7|8|9)\\d{9}$", message = "{user_import_format_wrong}") private String phone; @NotBlank(message = "{cannot_be_null}") + @ColumnWidth(30) @ExcelProperty("User is administrator(Yes/No)") private String userIsAdmin; @NotBlank(message = "{cannot_be_null}") + @ColumnWidth(30) @ExcelProperty("User is organization manager(Yes/No)") private String userIsOrgAdmin; @@ -56,6 +59,7 @@ public class UserExcelDataUs extends UserExcelData { private String orgAdminOrganization; @NotBlank(message = "{cannot_be_null}") + @ColumnWidth(30) @ExcelProperty("User is organization member(Yes/No)") private String userIsOrgMember; @@ -65,6 +69,7 @@ public class UserExcelDataUs extends UserExcelData { private String orgMemberOrganization; @NotBlank(message = "{cannot_be_null}") + @ColumnWidth(30) @ExcelProperty("User is test manager(Yes/No)") private String userIsTestManager; @@ -74,6 +79,7 @@ public class UserExcelDataUs extends UserExcelData { private String testManagerWorkspace; @NotBlank(message = "{cannot_be_null}") + @ColumnWidth(30) @ExcelProperty("User is tester(Yes/No)") private String userIsTester; @@ -83,11 +89,32 @@ public class UserExcelDataUs extends UserExcelData { private String testerWorkspace; @NotBlank(message = "{cannot_be_null}") + @ColumnWidth(30) + @ExcelProperty("User is project admin(Yes/No)") + private String userIsProjectAdmin; + + @Length(max = 100) + @ColumnWidth(30) + @ExcelProperty("Project admin of project admin") + private String proAdminProject; + + @NotBlank(message = "{cannot_be_null}") + @ColumnWidth(30) + @ExcelProperty("User is project member(Yes/No)") + private String userIsProjectMember; + + @Length(max = 100) + @ColumnWidth(30) + @ExcelProperty("Project member of project member") + private String proMemberProject; + + @NotBlank(message = "{cannot_be_null}") + @ColumnWidth(30) @ExcelProperty("User is read-only user(Yes/No)") private String userIsViewer; @Length(max = 100) @ColumnWidth(30) - @ExcelProperty("Workspace of read-only user") - private String viewerWorkspace; + @ExcelProperty("Project of read-only user") + private String viewerProject; } diff --git a/backend/src/main/java/io/metersphere/excel/listener/UserDataListener.java b/backend/src/main/java/io/metersphere/excel/listener/UserDataListener.java index 5b764db614..b8e98b0ce3 100644 --- a/backend/src/main/java/io/metersphere/excel/listener/UserDataListener.java +++ b/backend/src/main/java/io/metersphere/excel/listener/UserDataListener.java @@ -1,6 +1,7 @@ package io.metersphere.excel.listener; import io.metersphere.commons.constants.RoleConstants; +import io.metersphere.commons.constants.UserGroupConstants; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.controller.request.member.UserRequest; @@ -20,6 +21,10 @@ public class UserDataListener extends EasyExcelListener { Map workspaceNameMap; //key:Organization.name value:id Map orgNameMap; + /** + * key:project.name value.id + */ + Map projectNameMap; //已经保存的用户ID List savedUserId; @@ -42,10 +47,11 @@ public class UserDataListener extends EasyExcelListener { return this.names; } - public UserDataListener(Class clazz, Map workspaceNameMap, Map orgNameMap) { + public UserDataListener(Class clazz, Map workspaceNameMap, Map orgNameMap, Map projectNameMap) { this.clazz = clazz; this.workspaceNameMap = workspaceNameMap; this.orgNameMap = orgNameMap; + this.projectNameMap = projectNameMap; this.userService = (UserService) CommonBeanFactory.getBean("userService"); savedUserId = userService.selectAllId(); } @@ -54,30 +60,40 @@ public class UserDataListener extends EasyExcelListener { public String validate(UserExcelData data, String errMsg) { StringBuilder stringBuilder = new StringBuilder(errMsg); - //判断组织管理员组织 + //判断组织管理员组织(系统) String orgManagerOrgCheck = this.checkOrganization(data.getUserIsOrgAdmin(), data.getOrgAdminOrganization()); if (orgManagerOrgCheck != null) { stringBuilder.append(orgManagerOrgCheck); } - //判断组织成员组织 + //判断组织成员组织(系统) String orgMemberOrgCheck = this.checkOrganization(data.getUserIsOrgMember(), data.getOrgMemberOrganization()); if (orgMemberOrgCheck != null) { stringBuilder.append(orgMemberOrgCheck); } - //判断测试经理工作空间 + //判断测试经理工作空间(系统) String testManagerWorkspaceCheck = this.checkWorkSpace(data.getUserIsTestManager(), data.getTestManagerWorkspace()); if (testManagerWorkspaceCheck != null) { stringBuilder.append(testManagerWorkspaceCheck); } - //判断测试人员工作空间 + //判断测试人员工作空间(系统) String testerWorkspaceCheck = this.checkWorkSpace(data.getUserIsTester(), data.getTesterWorkspace()); if (testerWorkspaceCheck != null) { stringBuilder.append(testerWorkspaceCheck); } - //判断只读用户工作空间 - String viewerWorkspaceCheck = this.checkWorkSpace(data.getUserIsViewer(), data.getViewerWorkspace()); - if (viewerWorkspaceCheck != null) { - stringBuilder.append(viewerWorkspaceCheck); + // 判断项目管理员(系统) + String proAdminProjectCheck = this.checkProject(data.getUserIsProjectAdmin(), data.getProAdminProject()); + if (proAdminProjectCheck != null) { + stringBuilder.append(proAdminProjectCheck); + } + // 判断项目成员(系统) + String proMemberProjectCheck = this.checkProject(data.getUserIsProjectMember(), data.getProMemberProject()); + if (proMemberProjectCheck != null) { + stringBuilder.append(proMemberProjectCheck); + } + //判断只读用户工作空间(系统) + String viewerProjectCheck = this.checkWorkSpace(data.getUserIsViewer(), data.getViewerProject()); + if (viewerProjectCheck != null) { + stringBuilder.append(viewerProjectCheck); } return stringBuilder.toString(); } @@ -95,7 +111,7 @@ public class UserDataListener extends EasyExcelListener { return; } Collections.reverse(list); - List result = list.stream().map(item -> this.convert2UserRequest(item)).collect(Collectors.toList()); + List result = list.stream().map(this::convert2UserRequest).collect(Collectors.toList()); List ids = new LinkedList<>(); List names = new LinkedList<>(); for (UserRequest userRequest : result) { @@ -104,10 +120,10 @@ public class UserDataListener extends EasyExcelListener { names.add(userRequest.getName()); if (savedUserId.contains(id)) { //已经在数据库内的,走更新逻辑 - userService.updateUserRole(userRequest); + userService.updateImportUserGroup(userRequest); } else { //不再数据库中的走新建逻辑 - userService.insert(userRequest); + userService.saveImportUser(userRequest); } } this.setIds(ids); @@ -164,6 +180,29 @@ public class UserDataListener extends EasyExcelListener { return result; } + /** + * 检查项目 + * @param userGroupInExcel excel表中用户组填写信息 + * @param projectInfoInExcel excel表中用户项目填写信息 + * @return 报错信息 + */ + private String checkProject(String userGroupInExcel, String projectInfoInExcel) { + String result = null; + if (StringUtils.equalsAnyIgnoreCase(Translator.get("options_yes"), userGroupInExcel)) { + String[] projectNameArr = projectInfoInExcel.split("\n"); + for (String projectName : projectNameArr) { + if (!projectNameMap.containsKey(projectName)) { + if (result == null) { + result = Translator.get("user_import_organization_not_fond") + ":" + projectName + "; "; + } else { + result += Translator.get("user_import_organization_not_fond") + ":" + projectName + "; "; + } + } + } + } + return result; + } + /** * 通过excel的信息,以及id字典对象,获取相对应的ID * @@ -196,63 +235,76 @@ public class UserDataListener extends EasyExcelListener { //这里的password要加密 request.setPassword(data.getPassword()); - List> roleMapList = new ArrayList<>(); + List> groupMapList = new ArrayList<>(); //判断是否是Admin if (StringUtils.equalsIgnoreCase(Translator.get("options_yes"), data.getUserIsAdmin())) { List adminIdList = new ArrayList<>(); - adminIdList.add("adminSourceId"); - Map adminRoleMap = this.genRoleMap(RoleConstants.ADMIN, adminIdList); - roleMapList.add(adminRoleMap); + adminIdList.add("system"); + Map adminRoleMap = this.genGroupMap(UserGroupConstants.ADMIN, adminIdList); + groupMapList.add(adminRoleMap); } //判断组织管理员 List orgManagerOrdIdList = this.getIdByExcelInfoAndIdDic(data.getUserIsOrgAdmin(), data.getOrgAdminOrganization(), orgNameMap); if (!orgManagerOrdIdList.isEmpty()) { - Map orgAdminRoleMap = this.genRoleMap(RoleConstants.ORG_ADMIN, orgManagerOrdIdList); - roleMapList.add(orgAdminRoleMap); + Map orgAdminRoleMap = this.genGroupMap(UserGroupConstants.ORG_ADMIN, orgManagerOrdIdList); + groupMapList.add(orgAdminRoleMap); } //判断组织成员 List orgMemberOrdIdList = this.getIdByExcelInfoAndIdDic(data.getUserIsOrgMember(), data.getOrgMemberOrganization(), orgNameMap); if (!orgMemberOrdIdList.isEmpty()) { - Map orgMemberRoleMap = this.genRoleMap(RoleConstants.ORG_MEMBER, orgMemberOrdIdList); - roleMapList.add(orgMemberRoleMap); + Map orgMemberRoleMap = this.genGroupMap(UserGroupConstants.ORG_MEMBER, orgMemberOrdIdList); + groupMapList.add(orgMemberRoleMap); } //判断测试经理 List testManagerWorkspaceIdList = this.getIdByExcelInfoAndIdDic(data.getUserIsTestManager(), data.getTestManagerWorkspace(), workspaceNameMap); if (!testManagerWorkspaceIdList.isEmpty()) { - Map testManagerRoleMap = this.genRoleMap(RoleConstants.TEST_MANAGER, testManagerWorkspaceIdList); - roleMapList.add(testManagerRoleMap); + Map testManagerRoleMap = this.genGroupMap(UserGroupConstants.WS_ADMIN, testManagerWorkspaceIdList); + groupMapList.add(testManagerRoleMap); } //判断测试人员 List testgerWorkspaceIdList = this.getIdByExcelInfoAndIdDic(data.getUserIsTester(), data.getTesterWorkspace(), workspaceNameMap); if (!testgerWorkspaceIdList.isEmpty()) { - Map testerRoleMap = this.genRoleMap(RoleConstants.TEST_USER, testgerWorkspaceIdList); - roleMapList.add(testerRoleMap); + Map testerRoleMap = this.genGroupMap(UserGroupConstants.WS_MEMBER, testgerWorkspaceIdList); + groupMapList.add(testerRoleMap); + } + // 判断项目管理员 + List proAdminProjectIdList = this.getIdByExcelInfoAndIdDic(data.getUserIsProjectAdmin(), data.getProAdminProject(), projectNameMap); + if (!proAdminProjectIdList.isEmpty()) { + Map proAdminGroupMap = this.genGroupMap(UserGroupConstants.PROJECT_ADMIN, proAdminProjectIdList); + groupMapList.add(proAdminGroupMap); + } + // 判断项目成员 + List proMemberProjectIdList = this.getIdByExcelInfoAndIdDic(data.getUserIsProjectMember(), data.getProMemberProject(), projectNameMap); + if (!proMemberProjectIdList.isEmpty()) { + Map proMemberGroupMap = this.genGroupMap(UserGroupConstants.PROJECT_MEMBER, proMemberProjectIdList); + groupMapList.add(proMemberGroupMap); } //判断只读用户 - List viewerWorkspaceIdList = this.getIdByExcelInfoAndIdDic(data.getUserIsViewer(), data.getViewerWorkspace(), workspaceNameMap); - if (!viewerWorkspaceIdList.isEmpty()) { - Map testViewerRoleMap = this.genRoleMap(RoleConstants.TEST_VIEWER, viewerWorkspaceIdList); - roleMapList.add(testViewerRoleMap); + List readOnlyProjectIdList = this.getIdByExcelInfoAndIdDic(data.getUserIsViewer(), data.getViewerProject(), projectNameMap); + if (!readOnlyProjectIdList.isEmpty()) { + Map testViewerRoleMap = this.genGroupMap(UserGroupConstants.READ_ONLY, readOnlyProjectIdList); + groupMapList.add(testViewerRoleMap); } - request.setRoles(roleMapList); + + request.setGroups(groupMapList); return request; } /** * 封装用户权限数据格式 * - * @param roleName 权限名称 - * @param roleIdList 对应的权限ID + * @param groupName 权限名称 + * @param groupIdList 对应的权限ID * @return 保存用户时,对应的数据权限的数据格式 */ - private Map genRoleMap(String roleName, List roleIdList) { - Map roleMap = new HashMap<>(); - if (roleName == null || roleIdList == null) { - return roleMap; + private Map genGroupMap(String groupName, List groupIdList) { + Map groupMap = new HashMap<>(); + if (groupName == null || groupIdList == null) { + return groupMap; } - roleMap.put("id", roleName); - roleMap.put("ids", roleIdList); - return roleMap; + groupMap.put("id", groupName); + groupMap.put("ids", groupIdList); + return groupMap; } /** diff --git a/backend/src/main/java/io/metersphere/service/UserService.java b/backend/src/main/java/io/metersphere/service/UserService.java index c95ac2eca7..5f6f65a3b4 100644 --- a/backend/src/main/java/io/metersphere/service/UserService.java +++ b/backend/src/main/java/io/metersphere/service/UserService.java @@ -887,37 +887,28 @@ public class UserService { private List generateExportTemplate() { List list = new ArrayList<>(); - List types = TestCaseConstants.Type.getValues(); - List methods = TestCaseConstants.Method.getValues(); - SessionUser user = SessionUtils.getUser(); for (int i = 1; i <= 2; i++) { UserExcelData data = new UserExcelData(); data.setId("user_id_" + i); data.setName(Translator.get("user") + i); - data.setPassword(Translator.get("required") + ";" + Translator.get("password_format_is_incorrect")); data.setEmail(Translator.get("required")); - String workspace = ""; - for (int workspaceIndex = 1; workspaceIndex <= i; workspaceIndex++) { - if (workspaceIndex == 1) { - workspace = "workspace" + workspaceIndex; - } else { - workspace = workspace + "\n" + "workspace" + workspaceIndex; - } - } data.setUserIsAdmin(Translator.get("options_no")); data.setUserIsTester(Translator.get("options_no")); data.setUserIsOrgMember(Translator.get("options_no")); data.setUserIsViewer(Translator.get("options_no")); data.setUserIsTestManager(Translator.get("options_no")); + data.setUserIsProjectAdmin(Translator.get("options_no")); + data.setUserIsProjectMember(Translator.get("options_no")); data.setUserIsOrgAdmin(Translator.get("options_yes")); - data.setOrgAdminOrganization(workspace); + data.setOrgAdminOrganization("默认组织"); list.add(data); } - list.add(new UserExcelData()); UserExcelData explain = new UserExcelData(); + explain.setId("ID不支持中文"); explain.setName(Translator.get("do_not_modify_header_order")); - explain.setOrgAdminOrganization("多个工作空间请换行展示"); + explain.setOrgAdminOrganization("多个组织请换行展示"); + explain.setPassword(Translator.get("required") + ";" + Translator.get("password_format_is_incorrect")); list.add(explain); return list; } @@ -935,6 +926,7 @@ public class UserService { Map orgNameMap = new HashMap<>(); Map workspaceNameMap = new HashMap<>(); + Map projectNameMap = new HashMap<>(); List organizationList = extOrganizationMapper.findIdAndNameByOrganizationId("All"); for (OrganizationMemberDTO model : organizationList) { @@ -944,7 +936,12 @@ public class UserService { for (WorkspaceDTO model : workspaceList) { workspaceNameMap.put(model.getName(), model.getId()); } - EasyExcelListener easyExcelListener = new UserDataListener(clazz, workspaceNameMap, orgNameMap); + List projectList = projectMapper.selectByExample(new ProjectExample()); + for (Project pro : projectList) { + projectNameMap.put(pro.getName(), pro.getId()); + } + + EasyExcelListener easyExcelListener = new UserDataListener(clazz, workspaceNameMap, orgNameMap, projectNameMap); EasyExcelFactory.read(multipartFile.getInputStream(), clazz, easyExcelListener).sheet().doRead(); if (CollectionUtils.isNotEmpty(((UserDataListener) easyExcelListener).getNames())) { request.setAttribute("ms-req-title", String.join(",", ((UserDataListener) easyExcelListener).getNames())); @@ -1351,4 +1348,81 @@ public class UserService { userExample.createCriteria().andIdIn(userIds); return userMapper.selectByExample(userExample); } + + + public void updateImportUserGroup(UserRequest user) { + UserGroupExample userGroupExample = new UserGroupExample(); + userGroupExample.createCriteria().andUserIdEqualTo(user.getId()); + List userGroups = userGroupMapper.selectByExample(userGroupExample); + List list = userGroups.stream().map(UserGroup::getSourceId).collect(Collectors.toList()); + + if (!CollectionUtils.isEmpty(list)) { + if (list.contains(user.getLastWorkspaceId()) || list.contains(user.getLastOrganizationId())) { + user.setLastOrganizationId(""); + user.setLastWorkspaceId(""); + userMapper.updateByPrimaryKeySelective(user); + } + } + + userGroupMapper.deleteByExample(userGroupExample); + List> groups = user.getGroups(); + saveImportUserGroup(groups, user.getId()); + + UserExample example = new UserExample(); + UserExample.Criteria criteria = example.createCriteria(); + criteria.andEmailEqualTo(user.getEmail()); + criteria.andIdNotEqualTo(user.getId()); + if (userMapper.countByExample(example) > 0) { + MSException.throwException(Translator.get("user_email_already_exists")); + } + user.setUpdateTime(System.currentTimeMillis()); + user.setPassword(null); + userMapper.updateByPrimaryKeySelective(user); + } + + public void saveImportUser(UserRequest userRequest) { + checkUserParam(userRequest); + String userId = userRequest.getId(); + User user = userMapper.selectByPrimaryKey(userId); + if (user != null) { + MSException.throwException(Translator.get("user_id_already_exists")); + } else { + createUser(userRequest); + } + List> groups = userRequest.getGroups(); + saveImportUserGroup(groups, userId); + } + + private void saveImportUserGroup(List> groups, String userId) { + if (!groups.isEmpty()) { + for (Map map : groups) { + String groupId = (String) map.get("id"); + if (StringUtils.equals(groupId, UserGroupConstants.ADMIN)) { + UserGroup userGroup = new UserGroup(); + userGroup.setId(UUID.randomUUID().toString()); + userGroup.setUserId(userId); + userGroup.setGroupId(groupId); + userGroup.setSourceId("system"); + userGroup.setCreateTime(System.currentTimeMillis()); + userGroup.setUpdateTime(System.currentTimeMillis()); + userGroupMapper.insertSelective(userGroup); + } else { + List ids = (List) map.get("ids"); + SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); + UserGroupMapper mapper = sqlSession.getMapper(UserGroupMapper.class); + for (String id : ids) { + UserGroup userGroup = new UserGroup(); + userGroup.setId(UUID.randomUUID().toString()); + userGroup.setUserId(userId); + userGroup.setGroupId(groupId); + userGroup.setSourceId(id); + userGroup.setCreateTime(System.currentTimeMillis()); + userGroup.setUpdateTime(System.currentTimeMillis()); + mapper.insertSelective(userGroup); + } + sqlSession.flushStatements(); + } + } + } + } } diff --git a/frontend/src/business/components/settings/system/User.vue b/frontend/src/business/components/settings/system/User.vue index 622019d3b0..1007396979 100644 --- a/frontend/src/business/components/settings/system/User.vue +++ b/frontend/src/business/components/settings/system/User.vue @@ -3,8 +3,9 @@ diff --git a/frontend/src/business/components/settings/system/components/UserImport.vue b/frontend/src/business/components/settings/system/components/UserImport.vue index 8be1ff9fa9..7c8cc522ab 100644 --- a/frontend/src/business/components/settings/system/components/UserImport.vue +++ b/frontend/src/business/components/settings/system/components/UserImport.vue @@ -1,6 +1,15 @@