refactor(系统设置): 用户导入

This commit is contained in:
shiziyuan9527 2021-06-16 16:20:34 +08:00 committed by 刘瑞斌
parent 990705be3c
commit 94efd8d80e
10 changed files with 311 additions and 75 deletions

View File

@ -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";

View File

@ -11,7 +11,9 @@ import java.util.Map;
@Getter
@Setter
public class UserRequest extends User {
/**
* 弃用
*/
private List<Map<String, Object>> roles = new ArrayList<>();
private List<Map<String, Object>> groups = new ArrayList<>();

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<UserExcelData> {
Map<String, String> workspaceNameMap;
//key:Organization.name value:id
Map<String, String> orgNameMap;
/**
* key:project.name value.id
*/
Map<String, String> projectNameMap;
//已经保存的用户ID
List<String> savedUserId;
@ -42,10 +47,11 @@ public class UserDataListener extends EasyExcelListener<UserExcelData> {
return this.names;
}
public UserDataListener(Class clazz, Map<String, String> workspaceNameMap, Map<String, String> orgNameMap) {
public UserDataListener(Class clazz, Map<String, String> workspaceNameMap, Map<String, String> orgNameMap, Map<String, String> 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<UserExcelData> {
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<UserExcelData> {
return;
}
Collections.reverse(list);
List<UserRequest> result = list.stream().map(item -> this.convert2UserRequest(item)).collect(Collectors.toList());
List<UserRequest> result = list.stream().map(this::convert2UserRequest).collect(Collectors.toList());
List<String> ids = new LinkedList<>();
List<String> names = new LinkedList<>();
for (UserRequest userRequest : result) {
@ -104,10 +120,10 @@ public class UserDataListener extends EasyExcelListener<UserExcelData> {
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<UserExcelData> {
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<UserExcelData> {
//这里的password要加密
request.setPassword(data.getPassword());
List<Map<String, Object>> roleMapList = new ArrayList<>();
List<Map<String, Object>> groupMapList = new ArrayList<>();
//判断是否是Admin
if (StringUtils.equalsIgnoreCase(Translator.get("options_yes"), data.getUserIsAdmin())) {
List<String> adminIdList = new ArrayList<>();
adminIdList.add("adminSourceId");
Map<String, Object> adminRoleMap = this.genRoleMap(RoleConstants.ADMIN, adminIdList);
roleMapList.add(adminRoleMap);
adminIdList.add("system");
Map<String, Object> adminRoleMap = this.genGroupMap(UserGroupConstants.ADMIN, adminIdList);
groupMapList.add(adminRoleMap);
}
//判断组织管理员
List<String> orgManagerOrdIdList = this.getIdByExcelInfoAndIdDic(data.getUserIsOrgAdmin(), data.getOrgAdminOrganization(), orgNameMap);
if (!orgManagerOrdIdList.isEmpty()) {
Map<String, Object> orgAdminRoleMap = this.genRoleMap(RoleConstants.ORG_ADMIN, orgManagerOrdIdList);
roleMapList.add(orgAdminRoleMap);
Map<String, Object> orgAdminRoleMap = this.genGroupMap(UserGroupConstants.ORG_ADMIN, orgManagerOrdIdList);
groupMapList.add(orgAdminRoleMap);
}
//判断组织成员
List<String> orgMemberOrdIdList = this.getIdByExcelInfoAndIdDic(data.getUserIsOrgMember(), data.getOrgMemberOrganization(), orgNameMap);
if (!orgMemberOrdIdList.isEmpty()) {
Map<String, Object> orgMemberRoleMap = this.genRoleMap(RoleConstants.ORG_MEMBER, orgMemberOrdIdList);
roleMapList.add(orgMemberRoleMap);
Map<String, Object> orgMemberRoleMap = this.genGroupMap(UserGroupConstants.ORG_MEMBER, orgMemberOrdIdList);
groupMapList.add(orgMemberRoleMap);
}
//判断测试经理
List<String> testManagerWorkspaceIdList = this.getIdByExcelInfoAndIdDic(data.getUserIsTestManager(), data.getTestManagerWorkspace(), workspaceNameMap);
if (!testManagerWorkspaceIdList.isEmpty()) {
Map<String, Object> testManagerRoleMap = this.genRoleMap(RoleConstants.TEST_MANAGER, testManagerWorkspaceIdList);
roleMapList.add(testManagerRoleMap);
Map<String, Object> testManagerRoleMap = this.genGroupMap(UserGroupConstants.WS_ADMIN, testManagerWorkspaceIdList);
groupMapList.add(testManagerRoleMap);
}
//判断测试人员
List<String> testgerWorkspaceIdList = this.getIdByExcelInfoAndIdDic(data.getUserIsTester(), data.getTesterWorkspace(), workspaceNameMap);
if (!testgerWorkspaceIdList.isEmpty()) {
Map<String, Object> testerRoleMap = this.genRoleMap(RoleConstants.TEST_USER, testgerWorkspaceIdList);
roleMapList.add(testerRoleMap);
Map<String, Object> testerRoleMap = this.genGroupMap(UserGroupConstants.WS_MEMBER, testgerWorkspaceIdList);
groupMapList.add(testerRoleMap);
}
// 判断项目管理员
List<String> proAdminProjectIdList = this.getIdByExcelInfoAndIdDic(data.getUserIsProjectAdmin(), data.getProAdminProject(), projectNameMap);
if (!proAdminProjectIdList.isEmpty()) {
Map<String, Object> proAdminGroupMap = this.genGroupMap(UserGroupConstants.PROJECT_ADMIN, proAdminProjectIdList);
groupMapList.add(proAdminGroupMap);
}
// 判断项目成员
List<String> proMemberProjectIdList = this.getIdByExcelInfoAndIdDic(data.getUserIsProjectMember(), data.getProMemberProject(), projectNameMap);
if (!proMemberProjectIdList.isEmpty()) {
Map<String, Object> proMemberGroupMap = this.genGroupMap(UserGroupConstants.PROJECT_MEMBER, proMemberProjectIdList);
groupMapList.add(proMemberGroupMap);
}
//判断只读用户
List<String> viewerWorkspaceIdList = this.getIdByExcelInfoAndIdDic(data.getUserIsViewer(), data.getViewerWorkspace(), workspaceNameMap);
if (!viewerWorkspaceIdList.isEmpty()) {
Map<String, Object> testViewerRoleMap = this.genRoleMap(RoleConstants.TEST_VIEWER, viewerWorkspaceIdList);
roleMapList.add(testViewerRoleMap);
List<String> readOnlyProjectIdList = this.getIdByExcelInfoAndIdDic(data.getUserIsViewer(), data.getViewerProject(), projectNameMap);
if (!readOnlyProjectIdList.isEmpty()) {
Map<String, Object> 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<String, Object> genRoleMap(String roleName, List<String> roleIdList) {
Map<String, Object> roleMap = new HashMap<>();
if (roleName == null || roleIdList == null) {
return roleMap;
private Map<String, Object> genGroupMap(String groupName, List<String> groupIdList) {
Map<String, Object> 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;
}
/**

View File

@ -887,37 +887,28 @@ public class UserService {
private List<UserExcelData> generateExportTemplate() {
List<UserExcelData> list = new ArrayList<>();
List<String> types = TestCaseConstants.Type.getValues();
List<String> 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<String, String> orgNameMap = new HashMap<>();
Map<String, String> workspaceNameMap = new HashMap<>();
Map<String, String> projectNameMap = new HashMap<>();
List<OrganizationMemberDTO> 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<Project> 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<UserGroup> userGroups = userGroupMapper.selectByExample(userGroupExample);
List<String> 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<Map<String, Object>> 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<Map<String, Object>> groups = userRequest.getGroups();
saveImportUserGroup(groups, userId);
}
private void saveImportUserGroup(List<Map<String, Object>> groups, String userId) {
if (!groups.isEmpty()) {
for (Map<String, Object> 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<String> ids = (List<String>) 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();
}
}
}
}
}

View File

@ -3,8 +3,9 @@
<el-card class="table-card">
<template v-slot:header>
<ms-table-header :create-permission="['SYSTEM_USER:READ+CREATE']" :condition.sync="condition" @search="search" @create="create"
:create-tip="$t('user.create')" :title="$t('commons.user')"/>
<ms-table-header :create-permission="['SYSTEM_USER:READ+CREATE']" :condition.sync="condition" @search="search"
@import="importUserDialogOpen" :show-import="true" :import-tip="$t('commons.import_user')"
@create="create" :create-tip="$t('user.create')" :title="$t('commons.user')"/>
</template>

View File

@ -1,6 +1,15 @@
<template>
<el-dialog class="user-import" :title="$t('commons.import_user')" :visible.sync="dialogVisible"
@close="close">
<el-row>
<el-alert
title="用户组只支持系统默认用户组"
type="info"
show-icon
:closable="false"
>
</el-alert>
</el-row>
<el-row>
<el-link type="primary" class="download-template"
@click="downloadTemplate"
@ -124,13 +133,17 @@ export default {
<style scoped>
.download-template {
padding-top: 0px;
padding-bottom: 10px;
padding-top: 10px;
padding-bottom: 5px;
}
.user-import >>> .el-dialog {
width: 400px;
}
.user-import >>> .el-dialog__body {
padding: 5px 20px;
}
</style>