From 5d5c5bf84fb1dc5717696226d4a0dc7b5c44277f Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Thu, 17 Aug 2023 17:47:00 +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=E7=AE=A1=E7=90=86=E6=89=B9=E9=87=8F=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E5=A2=9E=E5=8A=A0=E6=B7=BB=E5=8A=A0=E5=88=B0=E7=BB=84?= =?UTF-8?q?=E7=BB=87=E5=92=8C=E9=A1=B9=E7=9B=AE=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GlobalUserRoleRelationController.java | 2 +- .../system/controller/UserController.java | 86 +++- .../system/dto/UserRoleOption.java | 11 - .../response/UserBatchProcessResponse.java | 9 - .../mapper/ExtUserRoleRelationMapper.java | 4 +- .../request/ProjectAddMemberBatchRequest.java | 10 +- .../request/ProjectAddMemberRequest.java | 9 +- .../{dto => }/request/user/BaseCondition.java | 2 +- .../request/user/UserAndRoleBatchRequest.java | 5 +- .../user}/UserBaseBatchRequest.java | 3 +- .../user}/UserChangeEnableRequest.java | 2 +- .../user}/UserEditRequest.java | 2 +- .../user/UserRoleBatchRelationRequest.java | 24 + .../response/user/BatchProcessResponse.java | 12 + .../user}/UserImportResponse.java | 2 +- .../response/user/UserSelectOption.java | 16 + .../user}/UserTableResponse.java | 2 +- .../response/user/UserTreeSelectOption.java | 18 + .../GlobalUserRoleRelationLogService.java | 2 +- .../GlobalUserRoleRelationService.java | 8 +- .../system/service/GlobalUserRoleService.java | 8 +- .../system/service/OrganizationService.java | 23 + .../system/service/SystemProjectService.java | 1 - .../service/UserRoleRelationService.java | 4 +- .../system/service/UserService.java | 24 +- .../system/utils/TreeNodeParseUtils.java | 28 ++ .../UserControllerNonePermissionTests.java | 12 +- .../controller/user/UserControllerTests.java | 451 ++++++++++++------ .../system/utils/user/UserParamUtils.java | 32 +- .../system/utils/user/UserRequestUtils.java | 6 + .../resources/dml/init_user_org_project.sql | 47 ++ 31 files changed, 609 insertions(+), 256 deletions(-) delete mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/dto/UserRoleOption.java delete mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/dto/response/UserBatchProcessResponse.java rename backend/services/system-setting/src/main/java/io/metersphere/system/{dto => }/request/user/BaseCondition.java (89%) rename backend/services/system-setting/src/main/java/io/metersphere/system/{dto => }/request/user/UserAndRoleBatchRequest.java (92%) rename backend/services/system-setting/src/main/java/io/metersphere/system/{dto/request => request/user}/UserBaseBatchRequest.java (90%) rename backend/services/system-setting/src/main/java/io/metersphere/system/{dto/request => request/user}/UserChangeEnableRequest.java (88%) rename backend/services/system-setting/src/main/java/io/metersphere/system/{dto/request => request/user}/UserEditRequest.java (94%) create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/request/user/UserRoleBatchRelationRequest.java create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/response/user/BatchProcessResponse.java rename backend/services/system-setting/src/main/java/io/metersphere/system/{dto/response => response/user}/UserImportResponse.java (95%) create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/response/user/UserSelectOption.java rename backend/services/system-setting/src/main/java/io/metersphere/system/{dto/response => response/user}/UserTableResponse.java (93%) create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/response/user/UserTreeSelectOption.java create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/utils/TreeNodeParseUtils.java create mode 100644 backend/services/system-setting/src/test/resources/dml/init_user_org_project.sql diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/GlobalUserRoleRelationController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/GlobalUserRoleRelationController.java index a93fc4350c..6f7c85a961 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/GlobalUserRoleRelationController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/GlobalUserRoleRelationController.java @@ -11,7 +11,7 @@ import io.metersphere.sdk.util.PageUtils; import io.metersphere.sdk.util.Pager; import io.metersphere.sdk.util.SessionUtils; import io.metersphere.system.dto.request.GlobalUserRoleRelationQueryRequest; -import io.metersphere.system.dto.request.user.UserAndRoleBatchRequest; +import io.metersphere.system.request.user.UserAndRoleBatchRequest; import io.metersphere.system.service.GlobalUserRoleRelationLogService; import io.metersphere.system.service.GlobalUserRoleRelationService; import io.metersphere.validation.groups.Created; 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 2973ce7b2a..e81375c26e 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 @@ -3,30 +3,27 @@ package io.metersphere.system.controller; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; +import io.metersphere.project.domain.Project; import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.constants.UserSourceEnum; import io.metersphere.sdk.dto.BasePageRequest; +import io.metersphere.sdk.dto.OptionDTO; import io.metersphere.sdk.dto.UserDTO; import io.metersphere.sdk.log.annotation.Log; import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.util.PageUtils; import io.metersphere.sdk.util.Pager; import io.metersphere.sdk.util.SessionUtils; +import io.metersphere.system.domain.Organization; import io.metersphere.system.domain.User; import io.metersphere.system.dto.UserBatchCreateDTO; import io.metersphere.system.dto.UserExtend; -import io.metersphere.system.dto.UserRoleOption; -import io.metersphere.system.dto.request.UserBaseBatchRequest; -import io.metersphere.system.dto.request.UserChangeEnableRequest; -import io.metersphere.system.dto.request.UserEditRequest; -import io.metersphere.system.dto.request.user.UserAndRoleBatchRequest; -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.GlobalUserRoleRelationLogService; -import io.metersphere.system.service.GlobalUserRoleRelationService; -import io.metersphere.system.service.GlobalUserRoleService; -import io.metersphere.system.service.UserService; +import io.metersphere.system.request.OrganizationMemberBatchRequest; +import io.metersphere.system.request.ProjectAddMemberBatchRequest; +import io.metersphere.system.request.user.*; +import io.metersphere.system.response.user.*; +import io.metersphere.system.service.*; +import io.metersphere.system.utils.TreeNodeParseUtils; import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Updated; import io.swagger.v3.oas.annotations.Operation; @@ -41,6 +38,7 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.util.List; +import java.util.Map; @RestController @RequestMapping("/system/user") @@ -51,6 +49,10 @@ public class UserController { private GlobalUserRoleService globalUserRoleService; @Resource private GlobalUserRoleRelationService globalUserRoleRelationService; + @Resource + private OrganizationService organizationService; + @Resource + private SystemProjectService systemProjectService; @GetMapping("/get/{email}") @Operation(summary = "通过email查找用户") @@ -59,13 +61,6 @@ public class UserController { return userService.getUserDTOByEmail(email); } - @GetMapping("/get/global/system/role") - @Operation(summary = "查找系统级用户权限") - @RequiresPermissions(PermissionConstants.SYSTEM_USER_ROLE_READ) - public List getGlobalSystemRole() { - return globalUserRoleService.getGlobalSystemRoleList(); - } - @PostMapping("/add") @Operation(summary = "添加用户") @RequiresPermissions(PermissionConstants.SYSTEM_USER_READ_ADD) @@ -94,7 +89,7 @@ public class UserController { @Operation(summary = "启用/禁用用户") @RequiresPermissions(PermissionConstants.SYSTEM_USER_READ_UPDATE) @Log(type = OperationLogType.UPDATE, expression = "#msClass.batchUpdateLog(#request)", msClass = UserService.class) - public UserBatchProcessResponse updateUserEnable(@Validated @RequestBody UserChangeEnableRequest request) { + public BatchProcessResponse updateUserEnable(@Validated @RequestBody UserChangeEnableRequest request) { return userService.updateUserEnable(request, SessionUtils.getSessionId()); } @@ -109,7 +104,7 @@ public class UserController { @Operation(summary = "删除用户") @Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#request)", msClass = UserService.class) @RequiresPermissions(PermissionConstants.SYSTEM_USER_READ_DELETE) - public UserBatchProcessResponse deleteUser(@Validated @RequestBody UserBaseBatchRequest request) { + public BatchProcessResponse deleteUser(@Validated @RequestBody UserBaseBatchRequest request) { return userService.deleteUser(request, SessionUtils.getUserId()); } @@ -124,7 +119,7 @@ public class UserController { @Operation(summary = "重置用户密码") @RequiresPermissions(PermissionConstants.SYSTEM_USER_READ_UPDATE) @Log(type = OperationLogType.UPDATE, expression = "#msClass.resetPasswordLog(#request)", msClass = UserService.class) - public UserBatchProcessResponse resetPassword(@Validated @RequestBody UserBaseBatchRequest request) { + public BatchProcessResponse resetPassword(@Validated @RequestBody UserBaseBatchRequest request) { return userService.resetPassword(request, SessionUtils.getUserId()); } @@ -140,7 +135,52 @@ public class UserController { @Operation(summary = "批量添加用户到多个用户组中") @RequiresPermissions(PermissionConstants.SYSTEM_USER_READ_UPDATE) @Log(type = OperationLogType.ADD, expression = "#msClass.batchAddLog(#request)", msClass = GlobalUserRoleRelationLogService.class) - public UserBatchProcessResponse batchAdd(@Validated({Created.class}) @RequestBody UserAndRoleBatchRequest request) { + public BatchProcessResponse batchAdd(@Validated({Created.class}) @RequestBody UserAndRoleBatchRequest request) { return globalUserRoleRelationService.batchAdd(request, SessionUtils.getUserId()); } + + @GetMapping("/get/global/system/role") + @Operation(summary = "查找系统级用户权限") + @RequiresPermissions(PermissionConstants.SYSTEM_USER_ROLE_READ) + public List getGlobalSystemRole() { + return globalUserRoleService.getGlobalSystemRoleList(); + } + + @GetMapping("/get/organization") + @Operation(summary = "用户批量操作-查找组织") + @RequiresPermissions(value = {PermissionConstants.SYSTEM_USER_ROLE_READ, PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ}, logical = Logical.AND) + public List getOrganization() { + return organizationService.listAll(); + } + + @GetMapping("/get/project") + @Operation(summary = "用户批量操作-查找项目") + @RequiresPermissions(value = {PermissionConstants.SYSTEM_USER_ROLE_READ, PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ}, logical = Logical.AND) + public List getProject() { + Map> orgProjectMap = organizationService.getOrgProjectMap(); + return TreeNodeParseUtils.parseOrgProjectMap(orgProjectMap); + } + + + @PostMapping("/add-project-member") + //todo 这里权限有更改 权限待定 + @Operation(summary = "添加用户到项目") + // @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_UPDATE) + public void addProjectMember(@Validated @RequestBody UserRoleBatchRelationRequest userRoleBatchRelationRequest) { + ProjectAddMemberBatchRequest request = new ProjectAddMemberBatchRequest(); + request.setProjectIds(userRoleBatchRelationRequest.getRoleIds()); + request.setUserIds(userRoleBatchRelationRequest.getUserIds()); + systemProjectService.addProjectMember(request, SessionUtils.getUserId()); + } + + @PostMapping("/add-org-member") + @Operation(summary = "添加用户到组织") + //todo 这里权限有更改 权限待定 + // @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_UPDATE) + public void addMember(@Validated @RequestBody UserRoleBatchRelationRequest userRoleBatchRelationRequest) { + OrganizationMemberBatchRequest request = new OrganizationMemberBatchRequest(); + request.setOrganizationIds(userRoleBatchRelationRequest.getRoleIds()); + request.setMemberIds(userRoleBatchRelationRequest.getUserIds()); + organizationService.addMemberBySystem(request, SessionUtils.getUserId()); + } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/UserRoleOption.java b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/UserRoleOption.java deleted file mode 100644 index 3d7be46c1a..0000000000 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/UserRoleOption.java +++ /dev/null @@ -1,11 +0,0 @@ -package io.metersphere.system.dto; - -import lombok.Data; - -@Data -public class UserRoleOption { - private String id; - private String name; - private boolean selected = false; - private boolean closeable = true; -} 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 deleted file mode 100644 index e4dcdaed50..0000000000 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/response/UserBatchProcessResponse.java +++ /dev/null @@ -1,9 +0,0 @@ -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/mapper/ExtUserRoleRelationMapper.java b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtUserRoleRelationMapper.java index 49e05f74d5..9f60ac5839 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtUserRoleRelationMapper.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtUserRoleRelationMapper.java @@ -1,7 +1,7 @@ package io.metersphere.system.mapper; -import io.metersphere.system.domain.UserRoleRelation; import io.metersphere.sdk.dto.UserRoleRelationUserDTO; +import io.metersphere.system.domain.UserRoleRelation; import io.metersphere.system.dto.request.GlobalUserRoleRelationQueryRequest; import org.apache.ibatis.annotations.Param; @@ -12,5 +12,5 @@ public interface ExtUserRoleRelationMapper { List selectGlobalRoleByUserId(String userId); - List listGlobal(@Param("request") GlobalUserRoleRelationQueryRequest request); + List listGlobal(@Param("request") GlobalUserRoleRelationQueryRequest request); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/request/ProjectAddMemberBatchRequest.java b/backend/services/system-setting/src/main/java/io/metersphere/system/request/ProjectAddMemberBatchRequest.java index bf30952345..66d86cae75 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/request/ProjectAddMemberBatchRequest.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/request/ProjectAddMemberBatchRequest.java @@ -1,6 +1,9 @@ package io.metersphere.system.request; +import io.metersphere.validation.groups.Created; +import io.metersphere.validation.groups.Updated; import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.Valid; import jakarta.validation.constraints.NotBlank; import lombok.Data; @@ -9,7 +12,8 @@ import java.util.List; @Data public class ProjectAddMemberBatchRequest extends ProjectAddMemberRequest{ @Schema(description = "项目ID集合", requiredMode = Schema.RequiredMode.REQUIRED) - @NotBlank(message = "{project.id.not_blank}") - private List projectIds; - + @Valid + private List< + @NotBlank(message = "{project.id.not_blank}", groups = {Created.class, Updated.class}) + String> projectIds; } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/request/ProjectAddMemberRequest.java b/backend/services/system-setting/src/main/java/io/metersphere/system/request/ProjectAddMemberRequest.java index 3e1faada17..664a1d4ec6 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/request/ProjectAddMemberRequest.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/request/ProjectAddMemberRequest.java @@ -1,6 +1,9 @@ package io.metersphere.system.request; +import io.metersphere.validation.groups.Created; +import io.metersphere.validation.groups.Updated; import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.Valid; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotEmpty; import lombok.Data; @@ -15,6 +18,8 @@ public class ProjectAddMemberRequest { @Schema(description = "用户ID集合", requiredMode = Schema.RequiredMode.REQUIRED) @NotEmpty(message = "{user.ids.not_blank}") - private List userIds; - + @Valid + private List< + @NotBlank(message = "{user_role_relation.user_id.not_blank}", groups = {Created.class, Updated.class}) + String> userIds; } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/user/BaseCondition.java b/backend/services/system-setting/src/main/java/io/metersphere/system/request/user/BaseCondition.java similarity index 89% rename from backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/user/BaseCondition.java rename to backend/services/system-setting/src/main/java/io/metersphere/system/request/user/BaseCondition.java index fdce359be0..f1ba3c6e03 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/user/BaseCondition.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/request/user/BaseCondition.java @@ -1,4 +1,4 @@ -package io.metersphere.system.dto.request.user; +package io.metersphere.system.request.user; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/user/UserAndRoleBatchRequest.java b/backend/services/system-setting/src/main/java/io/metersphere/system/request/user/UserAndRoleBatchRequest.java similarity index 92% rename from backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/user/UserAndRoleBatchRequest.java rename to backend/services/system-setting/src/main/java/io/metersphere/system/request/user/UserAndRoleBatchRequest.java index c712f70656..a6c6bab92d 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/user/UserAndRoleBatchRequest.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/request/user/UserAndRoleBatchRequest.java @@ -1,6 +1,5 @@ -package io.metersphere.system.dto.request.user; +package io.metersphere.system.request.user; -import io.metersphere.system.dto.request.UserBaseBatchRequest; import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Updated; import io.swagger.v3.oas.annotations.media.Schema; @@ -9,10 +8,12 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.Size; import lombok.Data; +import lombok.EqualsAndHashCode; import java.util.List; @Data +@EqualsAndHashCode(callSuper = false) public class UserAndRoleBatchRequest extends UserBaseBatchRequest { @Schema(description = "组ID", requiredMode = Schema.RequiredMode.REQUIRED) diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/UserBaseBatchRequest.java b/backend/services/system-setting/src/main/java/io/metersphere/system/request/user/UserBaseBatchRequest.java similarity index 90% rename from backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/UserBaseBatchRequest.java rename to backend/services/system-setting/src/main/java/io/metersphere/system/request/user/UserBaseBatchRequest.java index 905da2df24..69481b6629 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/UserBaseBatchRequest.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/request/user/UserBaseBatchRequest.java @@ -1,6 +1,5 @@ -package io.metersphere.system.dto.request; +package io.metersphere.system.request.user; -import io.metersphere.system.dto.request.user.BaseCondition; import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Updated; import io.swagger.v3.oas.annotations.media.Schema; 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/request/user/UserChangeEnableRequest.java similarity index 88% rename from backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/UserChangeEnableRequest.java rename to backend/services/system-setting/src/main/java/io/metersphere/system/request/user/UserChangeEnableRequest.java index 6e0031bc67..91150ca655 100644 --- 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/request/user/UserChangeEnableRequest.java @@ -1,4 +1,4 @@ -package io.metersphere.system.dto.request; +package io.metersphere.system.request.user; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/UserEditRequest.java b/backend/services/system-setting/src/main/java/io/metersphere/system/request/user/UserEditRequest.java similarity index 94% rename from backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/UserEditRequest.java rename to backend/services/system-setting/src/main/java/io/metersphere/system/request/user/UserEditRequest.java index f87594dc08..6b87732401 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/UserEditRequest.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/request/user/UserEditRequest.java @@ -1,4 +1,4 @@ -package io.metersphere.system.dto.request; +package io.metersphere.system.request.user; import io.metersphere.system.dto.UserCreateInfo; import io.metersphere.validation.groups.Created; diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/request/user/UserRoleBatchRelationRequest.java b/backend/services/system-setting/src/main/java/io/metersphere/system/request/user/UserRoleBatchRelationRequest.java new file mode 100644 index 0000000000..58350f837d --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/request/user/UserRoleBatchRelationRequest.java @@ -0,0 +1,24 @@ +package io.metersphere.system.request.user; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotEmpty; +import lombok.Data; + +import java.util.List; + +@Data +public class UserRoleBatchRelationRequest { + /** + * 权限ID集合 + */ + @Schema(description = "权限ID集合", requiredMode = Schema.RequiredMode.REQUIRED) + @NotEmpty(message = "{organization.id.not_blank}") + private List roleIds; + + /** + * 成员ID集合 + */ + @Schema(description = "成员ID集合", requiredMode = Schema.RequiredMode.REQUIRED) + @NotEmpty(message = "{user.id.not_blank}") + private List userIds; +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/response/user/BatchProcessResponse.java b/backend/services/system-setting/src/main/java/io/metersphere/system/response/user/BatchProcessResponse.java new file mode 100644 index 0000000000..684d0bd0e1 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/response/user/BatchProcessResponse.java @@ -0,0 +1,12 @@ +package io.metersphere.system.response.user; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +@Data +public class BatchProcessResponse { + @Schema(description = "全部数量") + private long totalCount; + @Schema(description = "成功数量") + private long successCount; +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/response/UserImportResponse.java b/backend/services/system-setting/src/main/java/io/metersphere/system/response/user/UserImportResponse.java similarity index 95% rename from backend/services/system-setting/src/main/java/io/metersphere/system/dto/response/UserImportResponse.java rename to backend/services/system-setting/src/main/java/io/metersphere/system/response/user/UserImportResponse.java index 8efe1fed03..c6bdfb5dbc 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/response/UserImportResponse.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/response/user/UserImportResponse.java @@ -1,4 +1,4 @@ -package io.metersphere.system.dto.response; +package io.metersphere.system.response.user; import io.metersphere.sdk.dto.ExcelParseDTO; import io.metersphere.system.dto.excel.UserExcelRowDTO; diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/response/user/UserSelectOption.java b/backend/services/system-setting/src/main/java/io/metersphere/system/response/user/UserSelectOption.java new file mode 100644 index 0000000000..977d2d0f6e --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/response/user/UserSelectOption.java @@ -0,0 +1,16 @@ +package io.metersphere.system.response.user; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +@Data +public class UserSelectOption { + @Schema(description = "节点唯一ID") + private String id; + @Schema(description = "节点名称") + private String name; + @Schema(description = "是否选中") + private boolean selected = false; + @Schema(description = "是否允许关闭") + private boolean closeable = true; +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/response/UserTableResponse.java b/backend/services/system-setting/src/main/java/io/metersphere/system/response/user/UserTableResponse.java similarity index 93% rename from backend/services/system-setting/src/main/java/io/metersphere/system/dto/response/UserTableResponse.java rename to backend/services/system-setting/src/main/java/io/metersphere/system/response/user/UserTableResponse.java index 1978f0b553..df0f8e61b5 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/response/UserTableResponse.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/response/user/UserTableResponse.java @@ -1,4 +1,4 @@ -package io.metersphere.system.dto.response; +package io.metersphere.system.response.user; import io.metersphere.system.domain.Organization; import io.metersphere.system.domain.User; diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/response/user/UserTreeSelectOption.java b/backend/services/system-setting/src/main/java/io/metersphere/system/response/user/UserTreeSelectOption.java new file mode 100644 index 0000000000..c81f230742 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/response/user/UserTreeSelectOption.java @@ -0,0 +1,18 @@ +package io.metersphere.system.response.user; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +public class UserTreeSelectOption extends UserSelectOption { + @Schema(description = "父节点ID") + private String parentId; + + public UserTreeSelectOption(String id, String name, String parentId) { + this.setId(id); + this.setName(name); + this.setParentId(parentId); + } +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationLogService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationLogService.java index 07029e906d..4cb1069bbf 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationLogService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationLogService.java @@ -12,9 +12,9 @@ import io.metersphere.sdk.util.JSON; import io.metersphere.system.domain.UserRole; import io.metersphere.system.domain.UserRoleExample; import io.metersphere.system.domain.UserRoleRelation; -import io.metersphere.system.dto.request.user.UserAndRoleBatchRequest; import io.metersphere.system.mapper.UserRoleMapper; import io.metersphere.system.mapper.UserRoleRelationMapper; +import io.metersphere.system.request.user.UserAndRoleBatchRequest; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationService.java index d7e5c9055f..234abe8c0b 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationService.java @@ -11,9 +11,9 @@ import io.metersphere.system.domain.UserRole; import io.metersphere.system.domain.UserRoleRelation; import io.metersphere.system.domain.UserRoleRelationExample; import io.metersphere.system.dto.request.GlobalUserRoleRelationQueryRequest; -import io.metersphere.system.dto.request.user.UserAndRoleBatchRequest; -import io.metersphere.system.dto.response.UserBatchProcessResponse; import io.metersphere.system.mapper.ExtUserRoleRelationMapper; +import io.metersphere.system.request.user.UserAndRoleBatchRequest; +import io.metersphere.system.response.user.BatchProcessResponse; import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Updated; import jakarta.annotation.Resource; @@ -85,7 +85,7 @@ public class GlobalUserRoleRelationService extends BaseUserRoleRelationService { return userRoleRelationMapper.selectByExample(example); } - public UserBatchProcessResponse batchAdd(@Validated({Created.class, Updated.class}) UserAndRoleBatchRequest request, String operator) { + public BatchProcessResponse batchAdd(@Validated({Created.class, Updated.class}) UserAndRoleBatchRequest request, String operator) { //检查角色的合法性 this.checkGlobalSystemUserRoleLegality(request.getRoleIds()); //获取本次处理的用户 @@ -116,7 +116,7 @@ public class GlobalUserRoleRelationService extends BaseUserRoleRelationService { if (CollectionUtils.isNotEmpty(saveList)) { userRoleRelationMapper.batchInsert(saveList); } - UserBatchProcessResponse response = new UserBatchProcessResponse(); + BatchProcessResponse response = new BatchProcessResponse(); response.setTotalCount(request.getUserIds().size()); response.setSuccessCount(saveList.size()); return response; diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleService.java index 1e5ef2be2a..5c7e4f1286 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleService.java @@ -7,9 +7,9 @@ import io.metersphere.sdk.service.BaseUserRoleService; import io.metersphere.sdk.util.Translator; import io.metersphere.system.domain.UserRole; import io.metersphere.system.domain.UserRoleExample; -import io.metersphere.system.dto.UserRoleOption; import io.metersphere.system.mapper.ExtUserRoleMapper; import io.metersphere.system.mapper.UserRoleMapper; +import io.metersphere.system.response.user.UserSelectOption; import jakarta.annotation.Resource; import jakarta.validation.Valid; import jakarta.validation.constraints.NotEmpty; @@ -109,12 +109,12 @@ public class GlobalUserRoleService extends BaseUserRoleService { } } - public List getGlobalSystemRoleList() { + public List getGlobalSystemRoleList() { UserRoleExample example = new UserRoleExample(); example.createCriteria().andScopeIdEqualTo(GLOBAL_SCOPE).andTypeEqualTo(SYSTEM_TYPE); - List returnList = new ArrayList<>(); + List returnList = new ArrayList<>(); userRoleMapper.selectByExample(example).forEach(userRole -> { - UserRoleOption userRoleOption = new UserRoleOption(); + UserSelectOption userRoleOption = new UserSelectOption(); userRoleOption.setId(userRole.getId()); userRoleOption.setName(userRole.getName()); userRoleOption.setSelected(StringUtils.equals(userRole.getId(), MEMBER.getValue())); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationService.java index a15c47a986..1115bd6afb 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationService.java @@ -815,6 +815,29 @@ public class OrganizationService { return extOrganizationMapper.getOptionsByIds(orgIds); } + public LinkedHashMap> getOrgProjectMap() { + ProjectExample projectExample = new ProjectExample(); + projectExample.setOrderByClause("name asc"); + List allProject = projectMapper.selectByExample(projectExample); + + OrganizationExample orgExample = new OrganizationExample(); + orgExample.createCriteria().andIdIn(allProject.stream().map(Project::getOrganizationId).distinct().collect(Collectors.toList())); + orgExample.setOrderByClause("name asc"); + List orgList = organizationMapper.selectByExample(orgExample); + + LinkedHashMap> returnMap = new LinkedHashMap<>(); + for (Organization org : orgList) { + List projectsInOrg = new ArrayList<>(); + for (Project project : allProject) { + if (StringUtils.equals(project.getOrganizationId(), org.getId())) { + projectsInOrg.add(project); + } + } + allProject.remove(projectsInOrg); + returnMap.put(org, projectsInOrg); + } + return returnMap; + } public Map getTotal(String organizationId) { Map total = new HashMap<>(); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectService.java index 135ca229e1..dc961f1517 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectService.java @@ -94,7 +94,6 @@ public class SystemProjectService { commonProjectService.deleteProject(projects); } - public List getProjectOptions() { return extSystemProjectMapper.selectProjectOptions(); } 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 5c8dcb4658..9507678b34 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 @@ -8,11 +8,11 @@ import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.log.service.OperationLogService; import io.metersphere.sdk.util.JSON; import io.metersphere.system.domain.*; -import io.metersphere.system.dto.response.UserTableResponse; import io.metersphere.system.mapper.ExtUserRoleRelationMapper; import io.metersphere.system.mapper.OrganizationMapper; import io.metersphere.system.mapper.UserRoleMapper; import io.metersphere.system.mapper.UserRoleRelationMapper; +import io.metersphere.system.response.user.UserTableResponse; import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Updated; import jakarta.annotation.Resource; @@ -162,7 +162,7 @@ public class UserRoleRelationService { List deleteRoleList = new ArrayList<>(); List saveList = new ArrayList<>(); List userRoleRelationList = this.selectGlobalRoleByUserId(user.getId()); - List userSavedRoleIdList = userRoleRelationList.stream().map(UserRoleRelation::getRoleId).collect(Collectors.toList()); + List userSavedRoleIdList = userRoleRelationList.stream().map(UserRoleRelation::getRoleId).toList(); //获取要移除的权限 for (String userSavedRoleId : userSavedRoleIdList) { if (!roleList.contains(userSavedRoleId)) { 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 ea9dde4acc..51ad2d0545 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,14 +20,14 @@ import io.metersphere.system.dto.UserCreateInfo; import io.metersphere.system.dto.UserExtend; import io.metersphere.system.dto.excel.UserExcel; import io.metersphere.system.dto.excel.UserExcelRowDTO; -import io.metersphere.system.dto.request.UserBaseBatchRequest; -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.ExtUserMapper; import io.metersphere.system.mapper.UserMapper; +import io.metersphere.system.request.user.UserBaseBatchRequest; +import io.metersphere.system.request.user.UserChangeEnableRequest; +import io.metersphere.system.request.user.UserEditRequest; +import io.metersphere.system.response.user.BatchProcessResponse; +import io.metersphere.system.response.user.UserImportResponse; +import io.metersphere.system.response.user.UserTableResponse; import io.metersphere.system.utils.UserImportEventListener; import jakarta.annotation.Resource; import jakarta.validation.Valid; @@ -197,10 +197,10 @@ public class UserService { return userEditRequest; } - public UserBatchProcessResponse updateUserEnable(UserChangeEnableRequest request, String operator) { + public BatchProcessResponse updateUserEnable(UserChangeEnableRequest request, String operator) { request.setUserIds(this.getBatchUserIds(request)); this.checkUserInDb(request.getUserIds()); - UserBatchProcessResponse response = new UserBatchProcessResponse(); + BatchProcessResponse response = new BatchProcessResponse(); response.setTotalCount(request.getUserIds().size()); UserExample userExample = new UserExample(); userExample.createCriteria().andIdIn( @@ -282,7 +282,7 @@ public class UserService { } - public UserBatchProcessResponse deleteUser(@Valid UserBaseBatchRequest request, String operator) { + public BatchProcessResponse deleteUser(@Valid UserBaseBatchRequest request, String operator) { List userIdList = this.getBatchUserIds(request); this.checkUserInDb(userIdList); //检查是否含有Admin @@ -290,7 +290,7 @@ public class UserService { UserExample userExample = new UserExample(); userExample.createCriteria().andIdIn(userIdList); //更新删除标志位 - UserBatchProcessResponse response = new UserBatchProcessResponse(); + BatchProcessResponse response = new BatchProcessResponse(); response.setTotalCount(userIdList.size()); response.setSuccessCount(this.deleteUserByList(userIdList, operator)); //删除用户角色关系 @@ -422,7 +422,7 @@ public class UserService { return extUserMapper.getMemberOption(sourceId); } - public UserBatchProcessResponse resetPassword(UserBaseBatchRequest request, String operator) { + public BatchProcessResponse resetPassword(UserBaseBatchRequest request, String operator) { request.setUserIds(this.getBatchUserIds(request)); this.checkUserInDb(request.getUserIds()); @@ -450,7 +450,7 @@ public class UserService { sqlSession.flushStatements(); SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); - UserBatchProcessResponse response = new UserBatchProcessResponse(); + BatchProcessResponse response = new BatchProcessResponse(); response.setTotalCount(request.getUserIds().size()); response.setSuccessCount(request.getUserIds().size()); return response; diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/utils/TreeNodeParseUtils.java b/backend/services/system-setting/src/main/java/io/metersphere/system/utils/TreeNodeParseUtils.java new file mode 100644 index 0000000000..cdf4ba6880 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/utils/TreeNodeParseUtils.java @@ -0,0 +1,28 @@ +package io.metersphere.system.utils; + +import io.metersphere.project.domain.Project; +import io.metersphere.system.domain.Organization; +import io.metersphere.system.response.user.UserTreeSelectOption; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class TreeNodeParseUtils { + public static List parseOrgProjectMap(Map> orgProjectMap) { + List userTreeSelectOptions = new ArrayList<>(); + for (Map.Entry> entry : orgProjectMap.entrySet()) { + Organization organization = entry.getKey(); + List projects = entry.getValue(); + + UserTreeSelectOption orgNode = new UserTreeSelectOption(organization.getId(), organization.getName(), null); + userTreeSelectOptions.add(orgNode); + + for (Project project : projects) { + UserTreeSelectOption projectNode = new UserTreeSelectOption(project.getId(), project.getName(), organization.getId()); + userTreeSelectOptions.add(projectNode); + } + } + return userTreeSelectOptions; + } +} diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/user/UserControllerNonePermissionTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/user/UserControllerNonePermissionTests.java index b068127699..d9b880914a 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/user/UserControllerNonePermissionTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/user/UserControllerNonePermissionTests.java @@ -2,10 +2,10 @@ package io.metersphere.system.controller.user; import com.jayway.jsonpath.JsonPath; import io.metersphere.system.dto.UserCreateInfo; -import io.metersphere.system.dto.UserRoleOption; -import io.metersphere.system.dto.request.UserBaseBatchRequest; -import io.metersphere.system.dto.request.UserChangeEnableRequest; -import io.metersphere.system.dto.request.user.UserAndRoleBatchRequest; +import io.metersphere.system.request.user.UserAndRoleBatchRequest; +import io.metersphere.system.request.user.UserBaseBatchRequest; +import io.metersphere.system.request.user.UserChangeEnableRequest; +import io.metersphere.system.response.user.UserSelectOption; import io.metersphere.system.utils.user.UserParamUtils; import io.metersphere.system.utils.user.UserRequestUtils; import jakarta.annotation.Resource; @@ -58,9 +58,9 @@ public class UserControllerNonePermissionTests { setName("tianyang.no.permission.email"); setEmail("tianyang.no.permission.email@126.com"); }}; - List paramRoleList = new ArrayList<>() {{ + List paramRoleList = new ArrayList<>() {{ this.add( - new UserRoleOption() {{ + new UserSelectOption() {{ this.setId("member"); this.setName("member"); }}); diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/user/UserControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/user/UserControllerTests.java index 166f8b9653..1e37fded43 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/user/UserControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/user/UserControllerTests.java @@ -1,5 +1,7 @@ package io.metersphere.system.controller.user; +import io.metersphere.project.domain.Project; +import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.sdk.base.BaseTest; import io.metersphere.sdk.controller.handler.ResultHolder; import io.metersphere.sdk.dto.BasePageRequest; @@ -12,18 +14,14 @@ 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.domain.UserRoleRelationExample; 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.UserBaseBatchRequest; -import io.metersphere.system.dto.request.UserChangeEnableRequest; -import io.metersphere.system.dto.request.UserEditRequest; -import io.metersphere.system.dto.request.user.UserAndRoleBatchRequest; -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.mapper.UserRoleRelationMapper; +import io.metersphere.system.request.user.*; +import io.metersphere.system.response.user.*; import io.metersphere.system.service.GlobalUserRoleRelationService; import io.metersphere.system.service.UserService; import io.metersphere.system.utils.user.UserParamUtils; @@ -66,7 +64,9 @@ public class UserControllerTests extends BaseTest { private static final ResultMatcher ERROR_REQUEST_MATCHER = status().is5xxServerError(); //测试过程中需要用到的数据 private static final List USER_LIST = new ArrayList<>(); - private static final List USER_ROLE_LIST = new ArrayList<>(); + private static final List USER_ROLE_LIST = new ArrayList<>(); + private static final List ORG_LIST = new ArrayList<>(); + private static final List PROJECT_LIST = new ArrayList<>(); //默认数据 public static final String USER_DEFAULT_NAME = "tianyang.no.1"; public static final String USER_DEFAULT_EMAIL = "tianyang.no.1@126.com"; @@ -151,116 +151,7 @@ public class UserControllerTests extends BaseTest { this.addUser2List(mvcResult); } - @Test - @Order(1) - public void testAddError() throws Exception { - if (CollectionUtils.isEmpty(USER_ROLE_LIST)) { - this.testGetGlobalSystemUserRoleSuccess(); - } - UserBatchCreateDTO userMaintainRequest; - List errorUserList = new ArrayList<>() {{ - add(new UserCreateInfo() {{ - setName("tianyang.error.1"); - setEmail("tianyang.error.name.1@126.com"); - }}); - add(new UserCreateInfo() {{ - setName("tianyang.error.2"); - setEmail("tianyang.error.name.2@126.com"); - }}); - }}; - - /* - * 校验参数不合法的反例 - * 每一次校验,使用getErrorUserCreateDTO方法重新获取参数,避免上一步的参数干扰 - */ - //所有参数都为空 - userMaintainRequest = UserParamUtils.getUserCreateDTO(null, null); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); - //用户组ID为空 - userMaintainRequest = UserParamUtils.getUserCreateDTO( - null, - errorUserList); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); - //没有用户 - userMaintainRequest = UserParamUtils.getUserCreateDTO( - USER_ROLE_LIST, - null); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); - //用户组含有null - userMaintainRequest = UserParamUtils.getUserCreateDTO( - USER_ROLE_LIST, - errorUserList); - userMaintainRequest.getUserRoleIdList().add(null); - userMaintainRequest.getUserRoleIdList().add(""); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); - //含有用户名称为空的数据 - userMaintainRequest = UserParamUtils.getUserCreateDTO( - USER_ROLE_LIST, - new ArrayList<>() - ); - userMaintainRequest.getUserInfoList().add(new UserCreateInfo() {{ - setEmail("tianyang.name.empty@126.com"); - }}); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); - //含有用户邮箱为空的数据 - userMaintainRequest = UserParamUtils.getUserCreateDTO( - USER_ROLE_LIST, - new ArrayList<>() - ); - userMaintainRequest.getUserInfoList().add(new UserCreateInfo() {{ - setName("tianyang.email.empty"); - }}); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); - //用户邮箱不符合标准 - userMaintainRequest = UserParamUtils.getUserCreateDTO( - USER_ROLE_LIST, - new ArrayList<>() - ); - userMaintainRequest.getUserInfoList().add(new UserCreateInfo() {{ - setName("用户邮箱放飞自我"); - setEmail("用户邮箱放飞自我"); - }}); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); - /* - * 校验业务判断出错的反例 (500 error) - * 需要保证数据库有正常数据 - */ - this.checkUserList(); - //含有非法用户组 - userMaintainRequest = UserParamUtils.getUserCreateDTO( - new ArrayList<>() {{ - this.add(new UserRoleOption() {{ - this.setId("not system global user role id"); - }}); - }}, - errorUserList); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, ERROR_REQUEST_MATCHER); - //含有重复的用户邮箱 - userMaintainRequest = UserParamUtils.getUserCreateDTO( - USER_ROLE_LIST, - errorUserList - ); - String firstUserEmail = userMaintainRequest.getUserInfoList().get(0).getEmail(); - userMaintainRequest.getUserInfoList().add(new UserCreateInfo() {{ - setName("tianyang.no.error4"); - setEmail(firstUserEmail); - }}); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, ERROR_REQUEST_MATCHER); - //测试请求参数中含有数据库中已存在的邮箱情况 - userMaintainRequest = UserParamUtils.getUserCreateDTO( - USER_ROLE_LIST, - errorUserList - ); - userMaintainRequest.setUserInfoList( - new ArrayList<>() {{ - add(new UserCreateInfo() {{ - setName("tianyang.repeat.email.db"); - setEmail(USER_DEFAULT_EMAIL); - }}); - }} - ); - this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, ERROR_REQUEST_MATCHER); - } + public final String URL_GET_ORGANIZATION = "/system/user/get/organization"; @Test @Order(2) @@ -579,6 +470,135 @@ public class UserControllerTests extends BaseTest { } } + public final String URL_GET_PROJECT = "/system/user/get/project"; + public final String URL_ADD_PROJECT_MEMBER = "/system/user/add-project-member"; + public final String URL_ADD_ORGANIZATION_MEMBER = "/system/user/add-org-member"; + @Resource + ProjectMapper projectMapper; + @Resource + private UserRoleRelationMapper userRoleRelationMapper; + + @Test + @Order(8) + public void testUserResetPasswordError() throws Exception { + //用户不存在 + { + UserBaseBatchRequest request = new UserBaseBatchRequest(); + request.setUserIds(Collections.singletonList("none user")); + this.requestPostAndReturn(userRequestUtils.URL_USER_RESET_PASSWORD, request, ERROR_REQUEST_MATCHER); + } + } + + @Test + @Order(1) + public void testAddError() throws Exception { + if (CollectionUtils.isEmpty(USER_ROLE_LIST)) { + this.testGetGlobalSystemUserRoleSuccess(); + } + UserBatchCreateDTO userMaintainRequest; + List errorUserList = new ArrayList<>() {{ + add(new UserCreateInfo() {{ + setName("tianyang.error.1"); + setEmail("tianyang.error.name.1@126.com"); + }}); + add(new UserCreateInfo() {{ + setName("tianyang.error.2"); + setEmail("tianyang.error.name.2@126.com"); + }}); + }}; + + /* + * 校验参数不合法的反例 + * 每一次校验,使用getErrorUserCreateDTO方法重新获取参数,避免上一步的参数干扰 + */ + //所有参数都为空 + userMaintainRequest = UserParamUtils.getUserCreateDTO(null, null); + this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); + //用户组ID为空 + userMaintainRequest = UserParamUtils.getUserCreateDTO( + null, + errorUserList); + this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); + //没有用户 + userMaintainRequest = UserParamUtils.getUserCreateDTO( + USER_ROLE_LIST, + null); + this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); + //用户组含有null + userMaintainRequest = UserParamUtils.getUserCreateDTO( + USER_ROLE_LIST, + errorUserList); + userMaintainRequest.getUserRoleIdList().add(null); + userMaintainRequest.getUserRoleIdList().add(""); + this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); + //含有用户名称为空的数据 + userMaintainRequest = UserParamUtils.getUserCreateDTO( + USER_ROLE_LIST, + new ArrayList<>() + ); + userMaintainRequest.getUserInfoList().add(new UserCreateInfo() {{ + setEmail("tianyang.name.empty@126.com"); + }}); + this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); + //含有用户邮箱为空的数据 + userMaintainRequest = UserParamUtils.getUserCreateDTO( + USER_ROLE_LIST, + new ArrayList<>() + ); + userMaintainRequest.getUserInfoList().add(new UserCreateInfo() {{ + setName("tianyang.email.empty"); + }}); + this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); + //用户邮箱不符合标准 + userMaintainRequest = UserParamUtils.getUserCreateDTO( + USER_ROLE_LIST, + new ArrayList<>() + ); + userMaintainRequest.getUserInfoList().add(new UserCreateInfo() {{ + setName("用户邮箱放飞自我"); + setEmail("用户邮箱放飞自我"); + }}); + this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, BAD_REQUEST_MATCHER); + /* + * 校验业务判断出错的反例 (500 error) + * 需要保证数据库有正常数据 + */ + this.checkUserList(); + //含有非法用户组 + userMaintainRequest = UserParamUtils.getUserCreateDTO( + new ArrayList<>() {{ + this.add(new UserSelectOption() {{ + this.setId("not system global user role id"); + }}); + }}, + errorUserList); + this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, ERROR_REQUEST_MATCHER); + //含有重复的用户邮箱 + userMaintainRequest = UserParamUtils.getUserCreateDTO( + USER_ROLE_LIST, + errorUserList + ); + String firstUserEmail = userMaintainRequest.getUserInfoList().get(0).getEmail(); + userMaintainRequest.getUserInfoList().add(new UserCreateInfo() {{ + setName("tianyang.no.error4"); + setEmail(firstUserEmail); + }}); + this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, ERROR_REQUEST_MATCHER); + //测试请求参数中含有数据库中已存在的邮箱情况 + userMaintainRequest = UserParamUtils.getUserCreateDTO( + USER_ROLE_LIST, + errorUserList + ); + userMaintainRequest.setUserInfoList( + new ArrayList<>() {{ + add(new UserCreateInfo() {{ + setName("tianyang.repeat.email.db"); + setEmail(USER_DEFAULT_EMAIL); + }}); + }} + ); + this.requestPost(userRequestUtils.URL_USER_CREATE, userMaintainRequest, ERROR_REQUEST_MATCHER); + } @Test @Order(8) @@ -590,7 +610,7 @@ public class UserControllerTests extends BaseTest { request.setUserIds(Collections.singletonList("admin")); userRequestUtils.parseObjectFromMvcResult( this.requestPostAndReturn(userRequestUtils.URL_USER_RESET_PASSWORD, request), - UserBatchProcessResponse.class + BatchProcessResponse.class ); //检查数据库 UserExample userExample = new UserExample(); @@ -607,9 +627,9 @@ public class UserControllerTests extends BaseTest { Assertions.assertEquals(1, userMapper.updateByPrimaryKeySelective(paramUser)); UserBaseBatchRequest request = new UserBaseBatchRequest(); request.setUserIds(Collections.singletonList(userId)); - UserBatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult( + BatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult( this.requestPostAndReturn(userRequestUtils.URL_USER_RESET_PASSWORD, request), - UserBatchProcessResponse.class + BatchProcessResponse.class ); Assertions.assertEquals(response.getTotalCount(), response.getSuccessCount(), 1); List userList = userService.selectByIdList(Collections.singletonList(userId)); @@ -625,9 +645,9 @@ public class UserControllerTests extends BaseTest { UserBaseBatchRequest request = new UserBaseBatchRequest(); request.setSkipIds(Collections.singletonList("admin")); request.setSelectAll(true); - UserBatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult( + BatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult( this.requestPostAndReturn(userRequestUtils.URL_USER_RESET_PASSWORD, request), - UserBatchProcessResponse.class + BatchProcessResponse.class ); UserExample example = new UserExample(); example.createCriteria().andIdNotEqualTo("admin"); @@ -646,17 +666,6 @@ public class UserControllerTests extends BaseTest { } } - @Test - @Order(8) - public void testUserResetPasswordError() throws Exception { - //用户不存在 - { - UserBaseBatchRequest request = new UserBaseBatchRequest(); - request.setUserIds(Collections.singletonList("none user")); - this.requestPostAndReturn(userRequestUtils.URL_USER_RESET_PASSWORD, request, ERROR_REQUEST_MATCHER); - } - } - @Test @Order(9) public void testUserRoleRelationSuccess() throws Exception { @@ -668,7 +677,7 @@ public class UserControllerTests extends BaseTest { //测试添加角色权限。 预期数据:每个用户都会增加对应的权限 UserAndRoleBatchRequest request = new UserAndRoleBatchRequest(); request.setUserIds(last50Users.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); - request.setRoleIds(USER_ROLE_LIST.stream().map(UserRoleOption::getId).collect(Collectors.toList())); + request.setRoleIds(USER_ROLE_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); userRequestUtils.requestPost(userRequestUtils.URL_USER_ROLE_RELATION, request, null); //检查有权限的数据量是否一致 Assertions.assertEquals( @@ -676,7 +685,7 @@ public class UserControllerTests extends BaseTest { request.getUserIds().size() * request.getRoleIds().size() ); //检查日志 - for (UserRoleOption option : USER_ROLE_LIST) { + for (UserSelectOption option : USER_ROLE_LIST) { this.checkLog(option.getId(), OperationLogType.ADD); } @@ -688,7 +697,7 @@ public class UserControllerTests extends BaseTest { request.getUserIds().size() * request.getRoleIds().size() ); //检查日志 - for (UserRoleOption option : USER_ROLE_LIST) { + for (UserSelectOption option : USER_ROLE_LIST) { this.checkLog(option.getId(), OperationLogType.ADD); } } @@ -704,7 +713,7 @@ public class UserControllerTests extends BaseTest { // 用户ID为空 UserAndRoleBatchRequest request = new UserAndRoleBatchRequest(); request.setUserIds(new ArrayList<>()); - request.setRoleIds(USER_ROLE_LIST.stream().map(UserRoleOption::getId).collect(Collectors.toList())); + request.setRoleIds(USER_ROLE_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); userRequestUtils.requestPost(userRequestUtils.URL_USER_ROLE_RELATION, request, BAD_REQUEST_MATCHER); // 角色id为空 request = new UserAndRoleBatchRequest(); @@ -714,17 +723,171 @@ public class UserControllerTests extends BaseTest { // 用户ID含有不存在的 request = new UserAndRoleBatchRequest(); request.setUserIds(last50Users.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); - request.setRoleIds(USER_ROLE_LIST.stream().map(UserRoleOption::getId).collect(Collectors.toList())); + request.setRoleIds(USER_ROLE_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); request.getUserIds().add("none user"); userRequestUtils.requestPost(userRequestUtils.URL_USER_ROLE_RELATION, request, ERROR_REQUEST_MATCHER); // 角色ID含有不存在的 request = new UserAndRoleBatchRequest(); request.setUserIds(last50Users.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); - request.setRoleIds(USER_ROLE_LIST.stream().map(UserRoleOption::getId).collect(Collectors.toList())); + request.setRoleIds(USER_ROLE_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); request.getRoleIds().add("none role"); userRequestUtils.requestPost(userRequestUtils.URL_USER_ROLE_RELATION, request, ERROR_REQUEST_MATCHER); } + @Test + @Order(10) + @Sql(scripts = {"/dml/init_user_org_project.sql"}, + config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED), + executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) + public void testGetProjectAndOrganization() throws Exception { + String str = userRequestUtils.responseGet(userRequestUtils.URL_GET_PROJECT).getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder rh = JSON.parseObject(str, ResultHolder.class); + List userTreeSelectOptions = JSON.parseArray( + JSON.toJSONString(rh.getData()), + UserTreeSelectOption.class); + //返回值不为空 + Assertions.assertTrue(CollectionUtils.isNotEmpty(userTreeSelectOptions)); + PROJECT_LIST.addAll(userTreeSelectOptions); + + + List userSelectOptions = JSON.parseArray( + JSON.toJSONString( + JSON.parseObject( + userRequestUtils.responseGet(userRequestUtils.URL_GET_ORGANIZATION).getResponse().getContentAsString(StandardCharsets.UTF_8), + ResultHolder.class).getData()), + UserSelectOption.class); + ORG_LIST.addAll(userSelectOptions); + } + + @Test + @Order(11) + public void testAddProjectMember() throws Exception { + //UserList中的部分角色是没有添加到某权限中的 + if (CollectionUtils.isEmpty(USER_LIST)) { + this.testAddSuccess(); + } + if (CollectionUtils.isEmpty(PROJECT_LIST) || CollectionUtils.isEmpty(ORG_LIST)) { + this.testGetProjectAndOrganization(); + } + + List last50Users = USER_LIST.subList(USER_LIST.size() - 50, USER_LIST.size()); + UserRoleBatchRelationRequest request = new UserRoleBatchRelationRequest(); + request.setUserIds(last50Users.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); + request.setRoleIds(PROJECT_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); + //排除树结构中的组织ID + request.getRoleIds().removeAll(ORG_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); + this.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, request); + //检查有权限的数据量是否一致 + UserRoleRelationExample checkExample = new UserRoleRelationExample(); + + for (String projectId : request.getRoleIds()) { + Project project = projectMapper.selectByPrimaryKey(projectId); + String orgId = project.getOrganizationId(); + for (String userId : request.getUserIds()) { + checkExample.createCriteria().andUserIdEqualTo(userId).andSourceIdEqualTo(projectId); + //检查是否在对应的项目下 + Assertions.assertEquals( + userRoleRelationMapper.countByExample(checkExample), 1 + ); + checkExample.clear(); + //检查是否在对应的组织下 + checkExample.createCriteria().andUserIdEqualTo(userId).andSourceIdEqualTo(orgId); + Assertions.assertEquals( + userRoleRelationMapper.countByExample(checkExample), 1 + ); + } + } + //检查日志 + for (UserSelectOption option : USER_ROLE_LIST) { + this.checkLog(option.getId(), OperationLogType.ADD); + } + } + + + @Test + @Order(13) + public void testAddOrganization() throws Exception { + //UserList中的部分角色是没有添加到某权限中的 + if (CollectionUtils.isEmpty(USER_LIST)) { + this.testAddSuccess(); + } + if (CollectionUtils.isEmpty(PROJECT_LIST) || CollectionUtils.isEmpty(ORG_LIST)) { + this.testGetProjectAndOrganization(); + } + + List last50Users = USER_LIST.subList(USER_LIST.size() - 50, USER_LIST.size()); + UserRoleBatchRelationRequest request = new UserRoleBatchRelationRequest(); + request.setUserIds(last50Users.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); + request.setRoleIds(ORG_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); + this.requestPost(userRequestUtils.URL_ADD_ORGANIZATION_MEMBER, request); + //检查有权限的数据量是否一致 + UserRoleRelationExample checkExample = new UserRoleRelationExample(); + for (String orgId : request.getRoleIds()) { + for (String userId : request.getUserIds()) { + checkExample.clear(); + //检查是否在对应的组织下 + checkExample.createCriteria().andUserIdEqualTo(userId).andSourceIdEqualTo(orgId); + Assertions.assertEquals( + userRoleRelationMapper.countByExample(checkExample), 1 + ); + } + } + //检查日志 + for (UserSelectOption option : USER_ROLE_LIST) { + this.checkLog(option.getId(), OperationLogType.ADD); + } + } + + @Test + @Order(11) + public void testAddToOrgOrProjectError() throws Exception { + if (CollectionUtils.isEmpty(PROJECT_LIST) || CollectionUtils.isEmpty(ORG_LIST)) { + this.testGetProjectAndOrganization(); + } + // 用户ID为空 + UserRoleBatchRelationRequest addToProjectRequest = new UserRoleBatchRelationRequest(); + addToProjectRequest.setUserIds(new ArrayList<>()); + addToProjectRequest.setRoleIds(PROJECT_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); + userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, addToProjectRequest, BAD_REQUEST_MATCHER); + // 项目为空 + addToProjectRequest = new UserRoleBatchRelationRequest(); + addToProjectRequest.setUserIds(USER_LIST.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); + addToProjectRequest.setRoleIds(new ArrayList<>()); + userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, addToProjectRequest, BAD_REQUEST_MATCHER); + // 用户ID含有不存在的 + addToProjectRequest = new UserRoleBatchRelationRequest(); + addToProjectRequest.setUserIds(Collections.singletonList("none user")); + addToProjectRequest.setRoleIds(PROJECT_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); + userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, addToProjectRequest, ERROR_REQUEST_MATCHER); + // 项目ID含有不存在的 + addToProjectRequest = new UserRoleBatchRelationRequest(); + addToProjectRequest.setUserIds(USER_LIST.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); + addToProjectRequest.setRoleIds(Collections.singletonList("none role")); + userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, addToProjectRequest, ERROR_REQUEST_MATCHER); + + + // 用户ID为空 + UserRoleBatchRelationRequest orgRequest = new UserRoleBatchRelationRequest(); + orgRequest.setUserIds(new ArrayList<>()); + orgRequest.setRoleIds(ORG_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); + userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, orgRequest, BAD_REQUEST_MATCHER); + // 项目为空 + orgRequest = new UserRoleBatchRelationRequest(); + orgRequest.setUserIds(USER_LIST.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); + orgRequest.setRoleIds(new ArrayList<>()); + userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, orgRequest, BAD_REQUEST_MATCHER); + // 用户ID含有不存在的 + orgRequest = new UserRoleBatchRelationRequest(); + orgRequest.setUserIds(Collections.singletonList("none user")); + orgRequest.setRoleIds(ORG_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList())); + userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, orgRequest, ERROR_REQUEST_MATCHER); + // 项目ID含有不存在的 + orgRequest = new UserRoleBatchRelationRequest(); + orgRequest.setUserIds(USER_LIST.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); + orgRequest.setRoleIds(Collections.singletonList("none role")); + userRequestUtils.requestPost(userRequestUtils.URL_ADD_PROJECT_MEMBER, orgRequest, ERROR_REQUEST_MATCHER); + } + //本测试类中会用到很多次用户数据。所以测试删除的方法放于最后 @Test @Order(99) @@ -735,7 +898,7 @@ public class UserControllerTests extends BaseTest { UserCreateInfo deleteUser = USER_LIST.get(0); UserBaseBatchRequest request = new UserBaseBatchRequest(); request.setUserIds(Collections.singletonList(deleteUser.getId())); - UserBatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(userRequestUtils.URL_USER_DELETE, request), UserBatchProcessResponse.class); + BatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(userRequestUtils.URL_USER_DELETE, request), BatchProcessResponse.class); Assertions.assertEquals(request.getUserIds().size(), response.getTotalCount()); Assertions.assertEquals(request.getUserIds().size(), response.getSuccessCount()); //检查数据库 @@ -749,7 +912,7 @@ public class UserControllerTests extends BaseTest { UserBaseBatchRequest request = new UserBaseBatchRequest(); request.setUserIds(USER_LIST.stream().map(UserCreateInfo::getId).collect(Collectors.toList())); request.setSkipIds(Collections.singletonList("admin")); - UserBatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(userRequestUtils.URL_USER_DELETE, request), UserBatchProcessResponse.class); + BatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(userRequestUtils.URL_USER_DELETE, request), BatchProcessResponse.class); Assertions.assertEquals(request.getUserIds().size(), response.getTotalCount(), response.getSuccessCount()); //检查数据库 UserExample example = new UserExample(); @@ -798,7 +961,7 @@ public class UserControllerTests extends BaseTest { ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); //返回请求正常 Assertions.assertNotNull(resultHolder); - List userRoleList = JSON.parseArray(JSON.toJSONString(resultHolder.getData()), UserRoleOption.class); + List userRoleList = JSON.parseArray(JSON.toJSONString(resultHolder.getData()), UserSelectOption.class); //返回值不为空 Assertions.assertTrue(CollectionUtils.isNotEmpty(userRoleList)); USER_ROLE_LIST.addAll(userRoleList); diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/utils/user/UserParamUtils.java b/backend/services/system-setting/src/test/java/io/metersphere/system/utils/user/UserParamUtils.java index 96e27407c0..b223caf434 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/utils/user/UserParamUtils.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/utils/user/UserParamUtils.java @@ -1,44 +1,40 @@ package io.metersphere.system.utils.user; -import io.metersphere.sdk.controller.handler.ResultHolder; import io.metersphere.sdk.dto.BasePageRequest; import io.metersphere.sdk.dto.UserDTO; import io.metersphere.sdk.service.BaseUserRoleService; import io.metersphere.sdk.util.BeanUtils; -import io.metersphere.sdk.util.JSON; import io.metersphere.system.domain.UserRole; import io.metersphere.system.dto.UserBatchCreateDTO; import io.metersphere.system.dto.UserCreateInfo; -import io.metersphere.system.dto.UserRoleOption; -import io.metersphere.system.dto.request.UserEditRequest; -import io.metersphere.system.dto.response.UserImportResponse; +import io.metersphere.system.request.user.UserEditRequest; +import io.metersphere.system.response.user.UserImportResponse; +import io.metersphere.system.response.user.UserSelectOption; import io.metersphere.system.service.GlobalUserRoleService; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.Assertions; -import org.springframework.test.web.servlet.MvcResult; import java.io.File; import java.io.FileInputStream; -import java.nio.charset.StandardCharsets; import java.util.List; import java.util.stream.Collectors; public class UserParamUtils { public static UserBatchCreateDTO getUserCreateDTO( - List userRoleList, + List userRoleList, List userInfoList) { UserBatchCreateDTO userMaintainRequest = new UserBatchCreateDTO(); if (CollectionUtils.isNotEmpty(userRoleList)) { userMaintainRequest.setUserRoleIdList( - userRoleList.stream().map(UserRoleOption::getId).collect(Collectors.toList())); + userRoleList.stream().map(UserSelectOption::getId).collect(Collectors.toList())); } userMaintainRequest.setUserInfoList(userInfoList); return userMaintainRequest; } - public static UserEditRequest getUserUpdateDTO(UserCreateInfo user, List userRoleList) { + public static UserEditRequest getUserUpdateDTO(UserCreateInfo user, List userRoleList) { UserEditRequest returnDTO = new UserEditRequest(); if (user.getPhone() == null) { user.setPhone(""); @@ -46,7 +42,7 @@ public class UserParamUtils { BeanUtils.copyBean(returnDTO, user); if (CollectionUtils.isNotEmpty(userRoleList)) { returnDTO.setUserRoleIdList( - userRoleList.stream().map(UserRoleOption::getId).collect(Collectors.toList())); + userRoleList.stream().map(UserSelectOption::getId).collect(Collectors.toList())); } return returnDTO; } @@ -61,23 +57,15 @@ public class UserParamUtils { public static byte[] getFileBytes(String filePath) { File file = new File(filePath); byte[] buffer = new byte[0]; - FileInputStream fi = null; - try { - fi = new FileInputStream(file); + try (FileInputStream fi = new FileInputStream(file)) { buffer = new byte[(int) file.length()]; int offset = 0; - int numRead = 0; + int numRead; while (offset < buffer.length && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) { offset += numRead; } } catch (Exception ignore) { - } finally { - try { - fi.close(); - } catch (Exception ignore) { - } - } return buffer; } @@ -100,7 +88,7 @@ public class UserParamUtils { = selectUserDTO.getUserRoles().stream() .filter(item -> (StringUtils.equals(item.getType(), BaseUserRoleService.SYSTEM_TYPE) && StringUtils.equals(item.getScopeId(), GlobalUserRoleService.GLOBAL_SCOPE))) - .map(UserRole::getId).collect(Collectors.toList()); + .map(UserRole::getId).toList(); Assertions.assertTrue( editRequest.getUserRoleIdList().containsAll(selectUserSystemRoleId) && selectUserSystemRoleId.containsAll(editRequest.getUserRoleIdList())); diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/utils/user/UserRequestUtils.java b/backend/services/system-setting/src/test/java/io/metersphere/system/utils/user/UserRequestUtils.java index 73d6c13f5a..e3b95b7612 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/utils/user/UserRequestUtils.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/utils/user/UserRequestUtils.java @@ -31,6 +31,12 @@ public class UserRequestUtils { public final String URL_USER_RESET_PASSWORD = "/system/user/reset/password"; public final String URL_USER_ROLE_RELATION = "/system/user/add/batch/user-role"; + //查找组织、项目 + public final String URL_GET_ORGANIZATION = "/system/user/get/organization"; + public final String URL_GET_PROJECT = "/system/user/get/project"; + public final String URL_ADD_PROJECT_MEMBER = "/system/user/add-project-member"; + public final String URL_ADD_ORGANIZATION_MEMBER = "/system/user/add-org-member"; + private final MockMvc mockMvc; private final String sessionId; private final String csrfToken; diff --git a/backend/services/system-setting/src/test/resources/dml/init_user_org_project.sql b/backend/services/system-setting/src/test/resources/dml/init_user_org_project.sql new file mode 100644 index 0000000000..706642ade7 --- /dev/null +++ b/backend/services/system-setting/src/test/resources/dml/init_user_org_project.sql @@ -0,0 +1,47 @@ +# 组织列表数据准备 +INSERT INTO organization(id,num, name, description, create_time, update_time, create_user, update_user, delete_user, delete_time) VALUE + ('tianyang-organization-1',12138, 'tianyang-organization-1', 'XXX-1', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'admin', null, null); +INSERT INTO organization(id, num, name, description, create_time, update_time, create_user, update_user, delete_user, delete_time) VALUE + ('tianyang-organization-2',12139, 'tianyang-organization-2', 'XXX-2', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'admin', null, null); +INSERT INTO organization(id, num, name, description, create_time, update_time, create_user, update_user, delete_user, delete_time) VALUE + ('tianyang-organization-3',12140, 'tianyang-organization-3', 'XXX-3', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'admin', null, null); +INSERT INTO organization(id, num, name, description, create_time, update_time, create_user, update_user, delete_user, delete_time) VALUE + ('tianyang-organization-4',12141, 'tianyang-organization-4', 'XXX-4', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'admin', null, null); +INSERT INTO organization(id,num, name, description, create_time, update_time, create_user, update_user, delete_user, delete_time) VALUE + ('tianyang-organization-5',12142, 'tianyang-organization-5', 'XXX-5', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'admin', null, null); +INSERT INTO organization(id, num, name, description, create_time, update_time, create_user, update_user, delete_user, delete_time) VALUE + ('tianyang-organization-6',12143, 'tianyang-organization-6', 'XXX-6', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'admin', null, null); + + +INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES + ('tianyang-projectId-1', 12138, 'tianyang-organization-1', 'tianyang-projectId-1', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES + ('tianyang-projectId-2', 12139, 'tianyang-organization-1', 'tianyang-projectId-2', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES + ('tianyang-projectId-3', 12140, 'tianyang-organization-1', 'tianyang-projectId-3', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES + ('tianyang-projectId-4', 12141, 'tianyang-organization-1', 'tianyang-projectId-4', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES + ('tianyang-projectId-5', 12142, 'tianyang-organization-1', 'tianyang-projectId-5', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES + ('tianyang-projectId-6', 12143, 'tianyang-organization-1', 'tianyang-projectId-6', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES + ('tianyang-projectId-7', 12144, 'tianyang-organization-1', 'tianyang-projectId-7', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES + ('tianyang-projectId-8', 12145, 'tianyang-organization-1', 'tianyang-projectId-8', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES + ('tianyang-projectId-9', 12146, 'tianyang-organization-1', 'tianyang-projectId-9', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES + ('tianyang-projectId-10', 12147, 'tianyang-organization-1', 'tianyang-projectId-10', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); + +INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES + ('tianyang-projectId-20', 12148, 'tianyang-organization-2', 'tianyang-projectId-20', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES + ('tianyang-projectId-30', 12149, 'tianyang-organization-3', 'tianyang-projectId-30', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES + ('tianyang-projectId-40', 12150, 'tianyang-organization-4', 'tianyang-projectId-40', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES + ('tianyang-projectId-50', 12151, 'tianyang-organization-5', 'tianyang-projectId-50', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES + ('tianyang-projectId-60', 12152, 'tianyang-organization-6', 'tianyang-projectId-60', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +