From 921a07657273d3bbf1ed6c135774f1f2e0c896d4 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Mon, 3 Jul 2023 19:30:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):=20?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=88=A0=E9=99=A4=E5=8A=9F=E8=83=BD=E5=BC=80?= =?UTF-8?q?=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/system/domain/User.java | 5 ++ .../system/domain/UserExample.java | 60 ++++++++++++++++++ .../metersphere/system/mapper/UserMapper.xml | 27 ++++++-- .../3.0.0/ddl/V3.0.0_11__system_setting.sql | 2 + .../migration/3.0.0/dml/V3.0.0_11_1__data.sql | 4 +- .../sdk/mapper/BaseUserMapper.java | 2 +- .../metersphere/sdk/mapper/BaseUserMapper.xml | 13 ++-- .../system/controller/UserController.java | 12 +++- ...uest.java => UserBatchProcessRequest.java} | 7 +-- .../dto/request/UserChangeEnableRequest.java | 8 +++ .../response/UserBatchProcessResponse.java | 9 +++ .../service/UserRoleRelationService.java | 6 ++ .../system/service/UserService.java | 31 ++++++++-- .../SystemProjectControllerTests.java | 9 ++- .../UserControllerNonePermissionTests.java | 32 ++++++++-- .../controller/UserControllerTests.java | 62 +++++++++++++++++-- .../system/utils/UserTestUtils.java | 1 + ...t_user_controller_none_permission_test.sql | 9 ++- .../dml/init_user_controller_test.sql | 4 +- .../src/test/resources/dml/init_user_test.sql | 4 +- 20 files changed, 261 insertions(+), 46 deletions(-) rename backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/{UserEditEnableRequest.java => UserBatchProcessRequest.java} (73%) create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/UserChangeEnableRequest.java create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/dto/response/UserBatchProcessResponse.java diff --git a/backend/framework/domain/src/main/java/io/metersphere/system/domain/User.java b/backend/framework/domain/src/main/java/io/metersphere/system/domain/User.java index 1ec61e3b7b..ea15bea5bd 100644 --- a/backend/framework/domain/src/main/java/io/metersphere/system/domain/User.java +++ b/backend/framework/domain/src/main/java/io/metersphere/system/domain/User.java @@ -4,6 +4,7 @@ import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Updated; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; import lombok.Data; @@ -61,5 +62,9 @@ public class User implements Serializable { @Schema(title = "修改人") private String updateUser; + @Schema(title = "是否删除", requiredMode = Schema.RequiredMode.REQUIRED) + @NotNull(message = "{user.deleted.not_blank}", groups = {Created.class}) + private Boolean deleted; + private static final long serialVersionUID = 1L; } \ No newline at end of file diff --git a/backend/framework/domain/src/main/java/io/metersphere/system/domain/UserExample.java b/backend/framework/domain/src/main/java/io/metersphere/system/domain/UserExample.java index 74777e54ff..d3cf382104 100644 --- a/backend/framework/domain/src/main/java/io/metersphere/system/domain/UserExample.java +++ b/backend/framework/domain/src/main/java/io/metersphere/system/domain/UserExample.java @@ -1053,6 +1053,66 @@ public class UserExample { addCriterion("update_user not between", value1, value2, "updateUser"); return (Criteria) this; } + + public Criteria andDeletedIsNull() { + addCriterion("deleted is null"); + return (Criteria) this; + } + + public Criteria andDeletedIsNotNull() { + addCriterion("deleted is not null"); + return (Criteria) this; + } + + public Criteria andDeletedEqualTo(Boolean value) { + addCriterion("deleted =", value, "deleted"); + return (Criteria) this; + } + + public Criteria andDeletedNotEqualTo(Boolean value) { + addCriterion("deleted <>", value, "deleted"); + return (Criteria) this; + } + + public Criteria andDeletedGreaterThan(Boolean value) { + addCriterion("deleted >", value, "deleted"); + return (Criteria) this; + } + + public Criteria andDeletedGreaterThanOrEqualTo(Boolean value) { + addCriterion("deleted >=", value, "deleted"); + return (Criteria) this; + } + + public Criteria andDeletedLessThan(Boolean value) { + addCriterion("deleted <", value, "deleted"); + return (Criteria) this; + } + + public Criteria andDeletedLessThanOrEqualTo(Boolean value) { + addCriterion("deleted <=", value, "deleted"); + return (Criteria) this; + } + + public Criteria andDeletedIn(List values) { + addCriterion("deleted in", values, "deleted"); + return (Criteria) this; + } + + public Criteria andDeletedNotIn(List values) { + addCriterion("deleted not in", values, "deleted"); + return (Criteria) this; + } + + public Criteria andDeletedBetween(Boolean value1, Boolean value2) { + addCriterion("deleted between", value1, value2, "deleted"); + return (Criteria) this; + } + + public Criteria andDeletedNotBetween(Boolean value1, Boolean value2) { + addCriterion("deleted not between", value1, value2, "deleted"); + return (Criteria) this; + } } public static class Criteria extends GeneratedCriteria { diff --git a/backend/framework/domain/src/main/java/io/metersphere/system/mapper/UserMapper.xml b/backend/framework/domain/src/main/java/io/metersphere/system/mapper/UserMapper.xml index 441117ea4d..0945bd3a28 100644 --- a/backend/framework/domain/src/main/java/io/metersphere/system/mapper/UserMapper.xml +++ b/backend/framework/domain/src/main/java/io/metersphere/system/mapper/UserMapper.xml @@ -16,6 +16,7 @@ + @@ -77,7 +78,7 @@ id, `name`, email, `password`, `enable`, create_time, update_time, `language`, last_organization_id, - phone, `source`, last_project_id, create_user, update_user + phone, `source`, last_project_id, create_user, update_user, deleted @@ -263,6 +272,9 @@ update_user = #{record.updateUser,jdbcType=VARCHAR}, + + deleted = #{record.deleted,jdbcType=BIT}, + @@ -283,7 +295,8 @@ `source` = #{record.source,jdbcType=VARCHAR}, last_project_id = #{record.lastProjectId,jdbcType=VARCHAR}, create_user = #{record.createUser,jdbcType=VARCHAR}, - update_user = #{record.updateUser,jdbcType=VARCHAR} + update_user = #{record.updateUser,jdbcType=VARCHAR}, + deleted = #{record.deleted,jdbcType=BIT} @@ -330,6 +343,9 @@ update_user = #{updateUser,jdbcType=VARCHAR}, + + deleted = #{deleted,jdbcType=BIT}, + where id = #{id,jdbcType=VARCHAR} @@ -347,7 +363,8 @@ `source` = #{source,jdbcType=VARCHAR}, last_project_id = #{lastProjectId,jdbcType=VARCHAR}, create_user = #{createUser,jdbcType=VARCHAR}, - update_user = #{updateUser,jdbcType=VARCHAR} + update_user = #{updateUser,jdbcType=VARCHAR}, + deleted = #{deleted,jdbcType=BIT} where id = #{id,jdbcType=VARCHAR} \ No newline at end of file diff --git a/backend/framework/domain/src/main/resources/migration/3.0.0/ddl/V3.0.0_11__system_setting.sql b/backend/framework/domain/src/main/resources/migration/3.0.0/ddl/V3.0.0_11__system_setting.sql index f323e4fc50..cc7c573fd0 100644 --- a/backend/framework/domain/src/main/resources/migration/3.0.0/ddl/V3.0.0_11__system_setting.sql +++ b/backend/framework/domain/src/main/resources/migration/3.0.0/ddl/V3.0.0_11__system_setting.sql @@ -295,12 +295,14 @@ CREATE TABLE IF NOT EXISTS user `last_project_id` VARCHAR(50) COMMENT '当前项目ID', `create_user` VARCHAR(50) NOT NULL COMMENT '创建人', `update_user` VARCHAR(50) NOT NULL COMMENT '修改人', + `deleted` BIT NOT NULL DEFAULT 0 COMMENT '是否删除' , PRIMARY KEY (id) ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '用户'; + CREATE INDEX idx_name ON user (`name`); CREATE UNIQUE INDEX idx_email ON user (`email`); CREATE INDEX idx_create_time ON user (`create_time`); diff --git a/backend/framework/domain/src/main/resources/migration/3.0.0/dml/V3.0.0_11_1__data.sql b/backend/framework/domain/src/main/resources/migration/3.0.0/dml/V3.0.0_11_1__data.sql index 7400578591..2801463725 100644 --- a/backend/framework/domain/src/main/resources/migration/3.0.0/dml/V3.0.0_11_1__data.sql +++ b/backend/framework/domain/src/main/resources/migration/3.0.0/dml/V3.0.0_11_1__data.sql @@ -7,8 +7,8 @@ INSERT INTO organization (id, num, name, description, create_user, update_user, INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES (uuid(), 100001, (SELECT id FROM organization WHERE name LIKE '默认组织'), '默认项目', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); -- 初始化用户 -insert into user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source, last_project_id, create_user, update_user) -VALUES ('admin', 'Administrator', 'admin@metersphere.io', MD5('metersphere'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin'); +insert into user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source, last_project_id, create_user, update_user,deleted) +VALUES ('admin', 'Administrator', 'admin@metersphere.io', MD5('metersphere'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin',false); -- 初始化用户组 INSERT INTO user_role (id, name, description, internal, type, create_time, update_time, create_user, scope_id) VALUES ('admin', '系统管理员', '拥有系统全部组织以及项目的操作权限', 1, 'SYSTEM', 1621224000000, 1621224000000, 'admin', 'GLOBAL'); diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.java index c5bd5b9d37..3c67020e25 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.java @@ -23,5 +23,5 @@ public interface BaseUserMapper { List selectByKeyword(String keyword); - List selectUserIdByIdList(@Param("idList") List userIdList); + List selectUnDeletedUserIdByIdList(@Param("idList") List userIdList); } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.xml b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.xml index db6cf177e0..c9a55ebc0e 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.xml +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.xml @@ -12,7 +12,7 @@ SELECT * FROM user LEFT JOIN user_extend ON user.id = user_extend.id - WHERE user.email = #{email} + WHERE user.email = #{email} AND deleted IS FALSE SELECT * FROM user + WHERE DELETED IS FALSE - WHERE name LIKE CONCAT('%', #{keyword}, '%') + AND name LIKE CONCAT('%', #{keyword}, '%') OR email LIKE CONCAT('%', #{keyword}, '%') - + SELECT * from `user` WHERE deleted IS FALSE AND id IN #{id} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java index 6092ecbdb1..b43a962fac 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java @@ -15,8 +15,9 @@ import io.metersphere.sdk.util.Pager; import io.metersphere.sdk.util.SessionUtils; import io.metersphere.system.dto.UserBatchCreateDTO; import io.metersphere.system.dto.UserRoleOption; -import io.metersphere.system.dto.request.UserEditEnableRequest; +import io.metersphere.system.dto.request.UserChangeEnableRequest; import io.metersphere.system.dto.request.UserEditRequest; +import io.metersphere.system.dto.response.UserBatchProcessResponse; import io.metersphere.system.dto.response.UserImportResponse; import io.metersphere.system.dto.response.UserTableResponse; import io.metersphere.system.service.GlobalUserRoleService; @@ -76,7 +77,7 @@ public class UserController { @PostMapping("/update/enable") @RequiresPermissions(PermissionConstants.SYSTEM_USER_READ_UPDATE) - public UserEditEnableRequest updateUserEnable(@Validated @RequestBody UserEditEnableRequest request) { + public UserBatchProcessResponse updateUserEnable(@Validated @RequestBody UserChangeEnableRequest request) { return userService.updateUserEnable(request, SessionUtils.getSessionId()); } @@ -85,4 +86,11 @@ public class UserController { public UserImportResponse importUser(@RequestPart(value = "file", required = false) MultipartFile excelFile) { return userService.importByExcel(excelFile, UserSourceEnum.LOCAL.name(), SessionUtils.getSessionId()); } + + + @PostMapping("/delete") + @RequiresPermissions(PermissionConstants.SYSTEM_USER_READ_DELETE) + public UserBatchProcessResponse deleteUser(@Validated @RequestBody UserChangeEnableRequest userBatchProcessRequest) { + return userService.deleteUser(userBatchProcessRequest.getUserIdList()); + } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/UserEditEnableRequest.java b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/UserBatchProcessRequest.java similarity index 73% rename from backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/UserEditEnableRequest.java rename to backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/UserBatchProcessRequest.java index d3be028860..c2f5054018 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/UserEditEnableRequest.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/UserBatchProcessRequest.java @@ -4,13 +4,12 @@ import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.NotEmpty; import lombok.Data; +import java.util.ArrayList; import java.util.List; @Data -public class UserEditEnableRequest { +public class UserBatchProcessRequest { @Schema(title = "用户ID", requiredMode = Schema.RequiredMode.REQUIRED) @NotEmpty(message = "{user.not.empty}") - List userIdList; - - boolean enable; + List userIdList = new ArrayList<>(); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/UserChangeEnableRequest.java b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/UserChangeEnableRequest.java new file mode 100644 index 0000000000..0ad6c3b5fd --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/UserChangeEnableRequest.java @@ -0,0 +1,8 @@ +package io.metersphere.system.dto.request; + +import lombok.Data; + +@Data +public class UserChangeEnableRequest extends UserBatchProcessRequest { + boolean enable; +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/response/UserBatchProcessResponse.java b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/response/UserBatchProcessResponse.java new file mode 100644 index 0000000000..e4dcdaed50 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/response/UserBatchProcessResponse.java @@ -0,0 +1,9 @@ +package io.metersphere.system.dto.response; + +import lombok.Data; + +@Data +public class UserBatchProcessResponse { + private long totalCount; + private long successCount; +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserRoleRelationService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserRoleRelationService.java index 9300508ef6..05e5d34895 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserRoleRelationService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserRoleRelationService.java @@ -205,4 +205,10 @@ public class UserRoleRelationService { example.createCriteria().andUserIdEqualTo(id); return userRoleRelationMapper.selectByExample(example); } + + public void deleteByUserIdList(@Valid @NotEmpty List userIdList) { + UserRoleRelationExample example = new UserRoleRelationExample(); + example.createCriteria().andUserIdIn(userIdList); + userRoleRelationMapper.deleteByExample(example); + } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java index dacbfbee8f..4a5acc341e 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java @@ -20,8 +20,9 @@ import io.metersphere.system.dto.UserBatchCreateDTO; import io.metersphere.system.dto.UserCreateInfo; import io.metersphere.system.dto.excel.UserExcel; import io.metersphere.system.dto.excel.UserExcelRowDTO; -import io.metersphere.system.dto.request.UserEditEnableRequest; +import io.metersphere.system.dto.request.UserChangeEnableRequest; import io.metersphere.system.dto.request.UserEditRequest; +import io.metersphere.system.dto.response.UserBatchProcessResponse; import io.metersphere.system.dto.response.UserImportResponse; import io.metersphere.system.dto.response.UserTableResponse; import io.metersphere.system.mapper.UserMapper; @@ -121,6 +122,7 @@ public class UserService { user.setUpdateTime(createTime); user.setPassword(CodingUtil.md5(user.getEmail())); user.setSource(source); + user.setDeleted(false); userMapper.insertSelective(user); saveUserList.add(user); } @@ -176,8 +178,12 @@ public class UserService { return userEditRequest; } - public UserEditEnableRequest updateUserEnable(UserEditEnableRequest request, String operator) { + public UserBatchProcessResponse updateUserEnable(UserChangeEnableRequest request, String operator) { this.checkUserInDb(request.getUserIdList()); + + UserBatchProcessResponse response = new UserBatchProcessResponse(); + response.setTotalCount(request.getUserIdList().size()); + UserExample userExample = new UserExample(); userExample.createCriteria().andIdIn( request.getUserIdList() @@ -186,12 +192,12 @@ public class UserService { updateUser.setEnable(request.isEnable()); updateUser.setUpdateUser(operator); updateUser.setUpdateTime(System.currentTimeMillis()); - userMapper.updateByExampleSelective(updateUser, userExample); - return request; + response.setSuccessCount(userMapper.updateByExampleSelective(updateUser, userExample)); + return response; } private void checkUserInDb(@Valid @NotEmpty List userIdList) { - List userInDb = baseUserMapper.selectUserIdByIdList(userIdList); + List userInDb = baseUserMapper.selectUnDeletedUserIdByIdList(userIdList); if (userIdList.size() != userInDb.size()) { throw new MSException(Translator.get("user.not.exist")); } @@ -258,4 +264,19 @@ public class UserService { } + public UserBatchProcessResponse deleteUser(@Valid @NotEmpty List userIdList) { + this.checkUserInDb(userIdList); + UserBatchProcessResponse response = new UserBatchProcessResponse(); + response.setTotalCount(userIdList.size()); + + UserExample userExample = new UserExample(); + userExample.createCriteria().andIdIn(userIdList); + //更新删除标志位 + response.setSuccessCount(userMapper.updateByExampleSelective(new User() {{ + setDeleted(true); + }}, userExample)); + //删除用户角色关系 + userRoleRelationService.deleteByUserIdList(userIdList); + return response; + } } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemProjectControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemProjectControllerTests.java index 6bb39f1a62..0a2aeca7b1 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemProjectControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemProjectControllerTests.java @@ -239,7 +239,7 @@ public class SystemProjectControllerTests extends BaseTest { @Test @Order(10) - public void testGetProjectListByOrg() throws Exception{ + public void testGetProjectListByOrg() throws Exception { String organizationId = "organizationId"; MvcResult mvcResult = this.responseGet(prefix + "/list/" + organizationId); String returnData = mvcResult.getResponse().getContentAsString(); @@ -251,7 +251,7 @@ public class SystemProjectControllerTests extends BaseTest { @Test @Order(11) - public void testGetProjectListByOrgError() throws Exception{ + public void testGetProjectListByOrgError() throws Exception { String organizationId = "organizationId"; MvcResult mvcResult = this.responseGet(prefix + "/list/" + organizationId); String returnData = mvcResult.getResponse().getContentAsString(); @@ -263,7 +263,7 @@ public class SystemProjectControllerTests extends BaseTest { @Test @Order(12) - public void testRemoveProjectMember() throws Exception{ + public void testRemoveProjectMember() throws Exception { String projectId = "projectId"; String userId = "admin1"; mockMvc.perform(MockMvcRequestBuilders.get(prefix + "/remove-member/" + projectId + "/" + userId) @@ -275,7 +275,7 @@ public class SystemProjectControllerTests extends BaseTest { @Test @Order(13) - public void testAddProjectMember() throws Exception{ + public void testAddProjectMember() throws Exception { ProjectMemberRequest projectMemberRequest = new ProjectMemberRequest(); projectMemberRequest.setProjectId("projectId"); List userIds = new ArrayList<>(); @@ -286,5 +286,4 @@ public class SystemProjectControllerTests extends BaseTest { } - } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerNonePermissionTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerNonePermissionTests.java index fed593f7f3..ec738f1ee7 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerNonePermissionTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerNonePermissionTests.java @@ -5,7 +5,8 @@ import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.util.JSON; import io.metersphere.system.dto.UserCreateInfo; import io.metersphere.system.dto.UserRoleOption; -import io.metersphere.system.dto.request.UserEditEnableRequest; +import io.metersphere.system.dto.request.UserBatchProcessRequest; +import io.metersphere.system.dto.request.UserChangeEnableRequest; import io.metersphere.system.utils.UserTestUtils; import jakarta.annotation.Resource; import org.apache.commons.lang3.StringUtils; @@ -13,6 +14,7 @@ import org.junit.jupiter.api.*; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; +import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.context.jdbc.Sql; import org.springframework.test.context.jdbc.SqlConfig; import org.springframework.test.web.servlet.MockMvc; @@ -23,7 +25,6 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import java.util.ArrayList; import java.util.List; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -81,13 +82,24 @@ public class UserControllerNonePermissionTests { this.requestPost(UserTestUtils.URL_USER_UPDATE, UserTestUtils.getUserUpdateDTO(paramUserInfo, paramRoleList), CHECK_RESULT_MATHER); //校验权限:启用/禁用用户 - UserEditEnableRequest userChangeEnableRequest = new UserEditEnableRequest(); + UserChangeEnableRequest userChangeEnableRequest = new UserChangeEnableRequest(); userChangeEnableRequest.setEnable(false); userChangeEnableRequest.setUserIdList(new ArrayList<>() {{ this.add("testId"); }}); this.requestPost(UserTestUtils.URL_USER_UPDATE_ENABLE, userChangeEnableRequest, CHECK_RESULT_MATHER); + //用户导入 + //导入正常文件 + String filePath = this.getClass().getClassLoader().getResource("file/user_import_success.xlsx").getPath(); + MockMultipartFile file = new MockMultipartFile("file", "userImport.xlsx", MediaType.APPLICATION_OCTET_STREAM_VALUE, UserTestUtils.getFileBytes(filePath)); + this.requestFile(UserTestUtils.URL_USER_IMPORT, file, CHECK_RESULT_MATHER); + //用户删除 + UserBatchProcessRequest request = new UserBatchProcessRequest(); + request.setUserIdList(new ArrayList<>() {{ + this.add("testId"); + }}); + this.requestPost(UserTestUtils.URL_USER_DELETE, request, CHECK_RESULT_MATHER); } @BeforeEach @@ -110,7 +122,17 @@ public class UserControllerNonePermissionTests { .header(SessionConstants.CSRF_TOKEN, csrfToken) .content(JSON.toJSONString(param)) .contentType(MediaType.APPLICATION_JSON)) - .andExpect(resultMatcher).andDo(print()) + .andExpect(resultMatcher) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)); + } + + private void requestFile(String url, MockMultipartFile file, ResultMatcher resultMatcher) throws Exception { + mockMvc.perform(MockMvcRequestBuilders.multipart(url) + .file(file) + .contentType(MediaType.MULTIPART_FORM_DATA_VALUE) + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken)) + .andExpect(resultMatcher) .andExpect(content().contentType(MediaType.APPLICATION_JSON)); } @@ -119,7 +141,7 @@ public class UserControllerNonePermissionTests { .header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.CSRF_TOKEN, csrfToken) .contentType(MediaType.APPLICATION_JSON)) - .andExpect(resultMatcher).andDo(print()) + .andExpect(resultMatcher) .andExpect(content().contentType(MediaType.APPLICATION_JSON)); } } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java index 03748c4039..d2d4e0a8b3 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java @@ -9,14 +9,19 @@ import io.metersphere.sdk.dto.UserDTO; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.Pager; +import io.metersphere.system.domain.User; +import io.metersphere.system.domain.UserExample; import io.metersphere.system.dto.UserBatchCreateDTO; import io.metersphere.system.dto.UserCreateInfo; import io.metersphere.system.dto.UserRoleOption; import io.metersphere.system.dto.excel.UserExcelRowDTO; -import io.metersphere.system.dto.request.UserEditEnableRequest; +import io.metersphere.system.dto.request.UserBatchProcessRequest; +import io.metersphere.system.dto.request.UserChangeEnableRequest; import io.metersphere.system.dto.request.UserEditRequest; +import io.metersphere.system.dto.response.UserBatchProcessResponse; import io.metersphere.system.dto.response.UserImportResponse; import io.metersphere.system.dto.response.UserTableResponse; +import io.metersphere.system.mapper.UserMapper; import io.metersphere.system.service.UserService; import io.metersphere.system.utils.UserTestUtils; import io.metersphere.utils.JsonUtils; @@ -40,6 +45,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; @@ -55,6 +61,8 @@ public class UserControllerTests extends BaseTest { @Resource private UserService userService; + @Resource + private UserMapper userMapper; //失败请求返回编码 private static final ResultMatcher BAD_REQUEST_MATCHER = status().isBadRequest(); @@ -66,6 +74,8 @@ public class UserControllerTests extends BaseTest { public static final String USER_DEFAULT_NAME = "tianyang.no.1"; public static final String USER_DEFAULT_EMAIL = "tianyang.no.1@126.com"; public static final String USER_NONE_ROLE_EMAIL = "tianyang.none.role@163.com"; + //已删除的用户ID + private static final List DELETED_USER_ID_LIST = new ArrayList<>(); //记录查询到的组织信息 private void setDefaultUserRoleList(MvcResult mvcResult) throws Exception { @@ -94,6 +104,13 @@ public class UserControllerTests extends BaseTest { } } + private void checkUserDeleted() throws Exception { + if (CollectionUtils.isEmpty(DELETED_USER_ID_LIST)) { + //测试数据初始化入库 + this.testUserDeleteSuccess(); + } + } + private void requestPost(String url, Object param, ResultMatcher resultMatcher) throws Exception { mockMvc.perform(MockMvcRequestBuilders.post(url) .header(SessionConstants.HEADER_TOKEN, sessionId) @@ -492,7 +509,7 @@ public class UserControllerTests extends BaseTest { this.checkUserList(); //单独修改状态 UserCreateInfo userInfo = USER_LIST.get(0); - UserEditEnableRequest userChangeEnableRequest = new UserEditEnableRequest(); + UserChangeEnableRequest userChangeEnableRequest = new UserChangeEnableRequest(); userChangeEnableRequest.setUserIdList(new ArrayList<>() {{ this.add(userInfo.getId()); }}); @@ -507,7 +524,7 @@ public class UserControllerTests extends BaseTest { public void testUserChangeEnableError() throws Exception { this.checkUserList(); //用户不存在 - UserEditEnableRequest userChangeEnableRequest = new UserEditEnableRequest(); + UserChangeEnableRequest userChangeEnableRequest = new UserChangeEnableRequest(); userChangeEnableRequest.setEnable(false); this.requestPost(UserTestUtils.URL_USER_UPDATE_ENABLE, userChangeEnableRequest, BAD_REQUEST_MATCHER); //含有非法用户 @@ -519,7 +536,7 @@ public class UserControllerTests extends BaseTest { @Test - @Order(6) + @Order(7) public void testUserImportSuccess() throws Exception { this.checkUserList(); //测试用户数据导入。 每个导入文件都有10条数据,不同文件出错的数据不同。 @@ -590,6 +607,43 @@ public class UserControllerTests extends BaseTest { this.checkImportUserInDb(userImportReportDTOByFile);//检查数据已入库 } + @Test + @Order(8) + public void testUserDeleteSuccess() throws Exception { + this.checkUserList(); + //删除已存的所有用户 + UserBatchProcessRequest request = new UserBatchProcessRequest(); + request.setUserIdList(USER_LIST.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); + UserBatchProcessResponse response = UserTestUtils.parseObjectFromMvcResult(this.responsePost(UserTestUtils.URL_USER_DELETE, request), UserBatchProcessResponse.class); + Assertions.assertEquals(request.getUserIdList().size(), response.getTotalCount()); + Assertions.assertEquals(request.getUserIdList().size(), response.getSuccessCount()); + //检查数据库 + UserExample example = new UserExample(); + example.createCriteria().andIdIn(request.getUserIdList()); + List userList = userMapper.selectByExample(example); + for (User user : userList) { + Assertions.assertTrue(user.getDeleted()); + } + //记录已经删除了的用户,用于反例 + DELETED_USER_ID_LIST.clear(); + DELETED_USER_ID_LIST.addAll(request.getUserIdList()); + } + + @Test + @Order(9) + public void testUserDeleteError() throws Exception { + this.checkUserDeleted(); + //参数为空 + UserBatchProcessRequest request = new UserBatchProcessRequest(); + this.requestPost(UserTestUtils.URL_USER_DELETE, request, BAD_REQUEST_MATCHER); + //用户不存在 + request.getUserIdList().add("123456789012345678901234"); + this.requestPost(UserTestUtils.URL_USER_DELETE, request, ERROR_REQUEST_MATCHER); + //用户已经被删除 + request.setUserIdList(DELETED_USER_ID_LIST); + this.requestPost(UserTestUtils.URL_USER_DELETE, request, ERROR_REQUEST_MATCHER); + } + public void checkImportUserInDb(ExcelParseDTO userImportReportDTOByFile) throws Exception { for (UserExcelRowDTO item : userImportReportDTOByFile.getDataList()) { Assertions.assertNotNull(this.getUserByEmail(item.getEmail())); diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/utils/UserTestUtils.java b/backend/services/system-setting/src/test/java/io/metersphere/system/utils/UserTestUtils.java index 92b9965027..54d7ca1dea 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/utils/UserTestUtils.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/utils/UserTestUtils.java @@ -35,6 +35,7 @@ public class UserTestUtils { public static final String URL_GET_GLOBAL_SYSTEM = "/system/user/get/global/system/role"; public static final String URL_USER_UPDATE_ENABLE = "/system/user/update/enable"; public static final String URL_USER_IMPORT = "/system/user/import"; + public static final String URL_USER_DELETE = "/system/user/delete"; public static T parseObjectFromMvcResult(MvcResult mvcResult, Class parseClass) { diff --git a/backend/services/system-setting/src/test/resources/dml/init_user_controller_none_permission_test.sql b/backend/services/system-setting/src/test/resources/dml/init_user_controller_none_permission_test.sql index deb4fcac6e..1197f386aa 100644 --- a/backend/services/system-setting/src/test/resources/dml/init_user_controller_none_permission_test.sql +++ b/backend/services/system-setting/src/test/resources/dml/init_user_controller_none_permission_test.sql @@ -1,4 +1,7 @@ -- 初始化一个没有任何权限的用户 -insert into user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source, last_project_id, create_user, update_user) -VALUES ('tianyang.member.id', 'tianyang.member', 'tianyang.member@163.com', MD5('tianyang.member@163.com'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin'); -INSERT INTO user_role_relation (id, user_id, role_id, source_id, create_time, create_user) VALUES (uuid(), 'tianyang.member.id', 'member', 'system', 1684747668375, 'admin'); \ No newline at end of file +insert into user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source, + last_project_id, create_user, update_user) +VALUES ('tianyang.member.id', 'tianyang.member', 'tianyang.member@163.com', MD5('tianyang.member@163.com'), + UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin'); +INSERT INTO user_role_relation (id, user_id, role_id, source_id, create_time, create_user) +VALUES (uuid(), 'tianyang.member.id', 'member', 'system', 1684747668375, 'admin'); \ No newline at end of file diff --git a/backend/services/system-setting/src/test/resources/dml/init_user_controller_test.sql b/backend/services/system-setting/src/test/resources/dml/init_user_controller_test.sql index 1dc9c146e6..46478d6ebb 100644 --- a/backend/services/system-setting/src/test/resources/dml/init_user_controller_test.sql +++ b/backend/services/system-setting/src/test/resources/dml/init_user_controller_test.sql @@ -1,3 +1,3 @@ -- 初始化一个没有任何权限的用户 -insert into user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source, last_project_id, create_user, update_user) -VALUES (uuid(), 'tianyang.none.role', 'tianyang.none.role@163.com', MD5('tianyang.none.role'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin'); \ No newline at end of file +insert into user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source, last_project_id, create_user, update_user, deleted) +VALUES (uuid(), 'tianyang.none.role', 'tianyang.none.role@163.com', MD5('tianyang.none.role'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin', false); \ No newline at end of file diff --git a/backend/services/system-setting/src/test/resources/dml/init_user_test.sql b/backend/services/system-setting/src/test/resources/dml/init_user_test.sql index 140e14e27c..e4b34d97c4 100644 --- a/backend/services/system-setting/src/test/resources/dml/init_user_test.sql +++ b/backend/services/system-setting/src/test/resources/dml/init_user_test.sql @@ -1,6 +1,6 @@ #测试项目下的成员 INSERT INTO `project` VALUES( 'projectId1', NULL, '3a5b1bd3-05e5-11ee-ad96-0242ac1e0a02', '默认项目1', '系统默认创建的项目', 1686219258000, 1686219258000, 'admin','admin', NULL, b '0', NULL, NULL ); -INSERT INTO `user` VALUES ('admin1', 'Administrator', 'admin1@metersphere.io', '3259a9d7f208ef9690025d1432558c5b', b'1', 1686219258000, 1686219258000, NULL, '3a5b1bd3-05e5-11ee-ad96-0242ac1e0a02', '', 'LOCAL', '3a6dbcc8-05e5-11ee-ad96-0242ac1e0a02', 'admin', 'admin'); -INSERT INTO `user` VALUES ('admin2', 'Administrator', 'admin2@metersphere.io', '3259a9d7f208ef9690025d1432558c5b', b'1', 1686219258000, 1686219258000, NULL, '3a5b1bd3-05e5-11ee-ad96-0242ac1e0a02', '', 'LOCAL', '3a6dbcc8-05e5-11ee-ad96-0242ac1e0a02', 'admin', 'admin'); +INSERT INTO `user` VALUES ('admin1', 'Administrator', 'admin1@metersphere.io', '3259a9d7f208ef9690025d1432558c5b', b'1', 1686219258000, 1686219258000, NULL, '3a5b1bd3-05e5-11ee-ad96-0242ac1e0a02', '', 'LOCAL', '3a6dbcc8-05e5-11ee-ad96-0242ac1e0a02', 'admin', 'admin',false); +INSERT INTO `user` VALUES ('admin2', 'Administrator', 'admin2@metersphere.io', '3259a9d7f208ef9690025d1432558c5b', b'1', 1686219258000, 1686219258000, NULL, '3a5b1bd3-05e5-11ee-ad96-0242ac1e0a02', '', 'LOCAL', '3a6dbcc8-05e5-11ee-ad96-0242ac1e0a02', 'admin', 'admin',false); INSERT INTO user_role_relation VALUES ('c3bb9b4f-46d8-4952-9681-8889974487w','admin1','project_admin','projectId1','1684747668375','1684747668375'); INSERT INTO user_role_relation VALUES ('c3bb9b4f-46d8-4952-9681-8889974487q','admin2','project_admin','projectId1','1684747668321','1684747668336'); \ No newline at end of file