From 14355b0562bc4fbd568455b9f7a394d1c72bbf14 Mon Sep 17 00:00:00 2001 From: song-cc-rock Date: Mon, 17 Jul 2023 18:27:28 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BB=84=E7=BB=87-=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=BB=84=E6=9D=83=E9=99=90=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sdk/log/constants/OperationLogModule.java | 1 + .../OrganizationUserRoleController.java | 9 ++ .../request/OrganizationEditRequest.java | 7 +- .../system/service/OrganizationService.java | 4 - .../OrganizationUserRoleLogService.java | 126 ++++++++++++++++++ .../OrganizationControllerTests.java | 2 - 6 files changed, 141 insertions(+), 8 deletions(-) create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationUserRoleLogService.java diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/constants/OperationLogModule.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/constants/OperationLogModule.java index 0faeb75432..515c350dda 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/constants/OperationLogModule.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/constants/OperationLogModule.java @@ -9,6 +9,7 @@ public class OperationLogModule { public static final String SYSTEM_USER_ROLE = "SYSTEM_USER_ROLE"; public static final String SYSTEM_USER_ROLE_RELATION = "SYSTEM_USER_ROLE_RELATION"; public static final String SYSTEM_ORGANIZATION = "SYSTEM_ORGANIZATION"; + public static final String ORGANIZATION_USER_ROLE = "ORGANIZATION_USER_ROLE"; public static final String ORGANIZATION_TEMPLATE_SETTINGS = "ORGANIZATION_TEMPLATE_SETTINGS"; public static final String ORGANIZATION_MESSAGE_SETTINGS = "ORGANIZATION_MESSAGE_SETTINGS"; public static final String ORGANIZATION_TEMPLATE_SETTINGS_FIELD = "ORGANIZATION_TEMPLATE_SETTINGS_FIELD"; diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationUserRoleController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationUserRoleController.java index 22c07a8500..7cc8bb820a 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationUserRoleController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationUserRoleController.java @@ -5,6 +5,8 @@ import com.github.pagehelper.PageHelper; import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.dto.PermissionDefinitionItem; import io.metersphere.sdk.dto.request.PermissionSettingUpdateRequest; +import io.metersphere.sdk.log.annotation.Log; +import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.PageUtils; import io.metersphere.sdk.util.Pager; @@ -14,6 +16,7 @@ import io.metersphere.system.domain.UserRole; import io.metersphere.system.request.OrganizationUserRoleEditRequest; import io.metersphere.system.request.OrganizationUserRoleMemberEditRequest; import io.metersphere.system.request.OrganizationUserRoleMemberRequest; +import io.metersphere.system.service.OrganizationUserRoleLogService; import io.metersphere.system.service.OrganizationUserRoleService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -45,6 +48,7 @@ public class OrganizationUserRoleController { @PostMapping("/add") @Operation(summary = "添加组织用户组") @RequiresPermissions(PermissionConstants.ORGANIZATION_USER_ROLE_READ_ADD) + @Log(type = OperationLogType.ADD, expression = "#msClass.addLog(#request)", msClass = OrganizationUserRoleLogService.class) public UserRole add(@Validated @RequestBody OrganizationUserRoleEditRequest request) { UserRole userRole = new UserRole(); userRole.setCreateUser(SessionUtils.getUserId()); @@ -55,6 +59,7 @@ public class OrganizationUserRoleController { @PostMapping("/update") @Operation(summary = "修改组织用户组") @RequiresPermissions(PermissionConstants.ORGANIZATION_USER_ROLE_READ_UPDATE) + @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#request)", msClass = OrganizationUserRoleLogService.class) public UserRole update(@Validated @RequestBody OrganizationUserRoleEditRequest request) { UserRole userRole = new UserRole(); BeanUtils.copyBean(userRole, request); @@ -64,6 +69,7 @@ public class OrganizationUserRoleController { @GetMapping("/delete/{id}") @Operation(summary = "删除组织用户组") @RequiresPermissions(PermissionConstants.ORGANIZATION_USER_ROLE_READ_DELETE) + @Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#id)", msClass = OrganizationUserRoleLogService.class) public void delete(@PathVariable String id) { organizationUserRoleService.delete(id); } @@ -78,6 +84,7 @@ public class OrganizationUserRoleController { @PostMapping("/permission/update") @Operation(summary = "编辑组织用户组对应的权限配置") @RequiresPermissions(PermissionConstants.ORGANIZATION_USER_ROLE_READ_UPDATE) + @Log(type = OperationLogType.UPDATE, expression = "#msClass.updatePermissionSettingLog(#request)", msClass = OrganizationUserRoleLogService.class) public void updatePermissionSetting(@Validated @RequestBody PermissionSettingUpdateRequest request) { organizationUserRoleService.updatePermissionSetting(request); } @@ -93,6 +100,7 @@ public class OrganizationUserRoleController { @PostMapping("/add-member") @Operation(summary = "添加组织用户组成员") @RequiresPermissions(PermissionConstants.ORGANIZATION_USER_ROLE_READ_UPDATE) + @Log(type = OperationLogType.UPDATE, expression = "#msClass.editMemberLog(#request)", msClass = OrganizationUserRoleLogService.class) public void addMember(@Validated @RequestBody OrganizationUserRoleMemberEditRequest request) { organizationUserRoleService.addMember(request, SessionUtils.getUserId()); } @@ -100,6 +108,7 @@ public class OrganizationUserRoleController { @PostMapping("/remove-member") @Operation(summary = "删除组织用户组成员") @RequiresPermissions(PermissionConstants.ORGANIZATION_USER_ROLE_READ_UPDATE) + @Log(type = OperationLogType.UPDATE, expression = "#msClass.editMemberLog(#request)", msClass = OrganizationUserRoleLogService.class) public void removeMember(@Validated @RequestBody OrganizationUserRoleMemberEditRequest request) { organizationUserRoleService.removeMember(request); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/request/OrganizationEditRequest.java b/backend/services/system-setting/src/main/java/io/metersphere/system/request/OrganizationEditRequest.java index 24b9faf653..f3739e1810 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/request/OrganizationEditRequest.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/request/OrganizationEditRequest.java @@ -14,8 +14,11 @@ import java.util.List; @Data public class OrganizationEditRequest implements Serializable { + @Schema(title = "组织ID") + private String id; + @Schema(title = "组织名称", requiredMode = Schema.RequiredMode.REQUIRED) - @NotBlank(message = "{organization.name.not_blank}", groups = {Created.class}) + @NotBlank(message = "{organization.name.not_blank}", groups = {Created.class, Updated.class}) @Size(min = 1, max = 100, message = "{organization.name.length_range}", groups = {Created.class, Updated.class}) private String name; @@ -23,6 +26,6 @@ public class OrganizationEditRequest implements Serializable { private String description; @Schema(title = "成员ID集合") - @NotEmpty(groups = {Created.class}, message = "{member.id.not_empty}") + @NotEmpty(message = "{member.id.not_empty}", groups = {Created.class, Updated.class}) private List memberIds; } 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 3659f71420..9e9577cfc3 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 @@ -91,10 +91,6 @@ public class OrganizationService{ } UserRoleRelationExample example = new UserRoleRelationExample(); example.createCriteria().andUserIdEqualTo(userId).andSourceIdEqualTo(organizationId); - List userRoleRelations = userRoleRelationMapper.selectByExample(example); - if (CollectionUtils.isEmpty(userRoleRelations)) { - throw new MSException(Translator.get("organization_member_not_exist")); - } userRoleRelationMapper.deleteByExample(example); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationUserRoleLogService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationUserRoleLogService.java new file mode 100644 index 0000000000..3b49c0bec4 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationUserRoleLogService.java @@ -0,0 +1,126 @@ +package io.metersphere.system.service; + +import io.metersphere.constants.HttpMethodConstants; +import io.metersphere.sdk.dto.LogDTO; +import io.metersphere.sdk.dto.request.PermissionSettingUpdateRequest; +import io.metersphere.sdk.log.constants.OperationLogModule; +import io.metersphere.sdk.log.constants.OperationLogType; +import io.metersphere.sdk.util.JSON; +import io.metersphere.system.domain.UserRole; +import io.metersphere.system.mapper.UserRoleMapper; +import io.metersphere.system.request.OrganizationUserRoleEditRequest; +import io.metersphere.system.request.OrganizationUserRoleMemberEditRequest; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional(rollbackFor = Exception.class) +public class OrganizationUserRoleLogService { + + @Resource + private UserRoleMapper userRoleMapper; + + private static final String PRE_URI = "/user/role/organization"; + + /** + * 新增组织-用户组 + * @param request 接口请求参数 + * @return 日志详情 + */ + public LogDTO addLog(OrganizationUserRoleEditRequest request) { + LogDTO dto = new LogDTO( + "", + request.getScopeId(), + null, + null, + OperationLogType.ADD.name(), + OperationLogModule.ORGANIZATION_USER_ROLE, + request.getName()); + dto.setPath(PRE_URI + "/add"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(request)); + return dto; + } + + /** + * 更新组织-用户组 + * @param request 接口请求参数 + * @return 日志详情 + */ + public LogDTO updateLog(OrganizationUserRoleEditRequest request) { + LogDTO dto = new LogDTO( + "", + request.getScopeId(), + null, + null, + OperationLogType.UPDATE.name(), + OperationLogModule.ORGANIZATION_USER_ROLE, + request.getName()); + dto.setPath(PRE_URI + "/update"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(request)); + return dto; + } + + /** + * 删除组织-用户组 + * @param id 接口请求参数 + * @return 日志详情 + */ + public LogDTO deleteLog(String id) { + UserRole userRole = userRoleMapper.selectByPrimaryKey(id); + LogDTO dto = new LogDTO( + "", + userRole.getScopeId(), + null, + null, + OperationLogType.DELETE.name(), + OperationLogModule.ORGANIZATION_USER_ROLE, + userRole.getName()); + dto.setPath(PRE_URI + "/delete"); + dto.setMethod(HttpMethodConstants.GET.name()); + dto.setOriginalValue(JSON.toJSONBytes(userRole)); + return dto; + } + + /** + * 更新组织-用户组-权限 + * @param request 接口请求参数 + * @return 日志详情 + */ + public LogDTO updatePermissionSettingLog(PermissionSettingUpdateRequest request) { + LogDTO dto = getLog(request.getUserRoleId()); + dto.setType(OperationLogType.UPDATE.name()); + dto.setPath(PRE_URI + "/update"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(request)); + return dto; + } + + /** + * 更新组织-用户组-成员 + * @param request 接口请求参数 + * @return 日志详情 + */ + public LogDTO editMemberLog(OrganizationUserRoleMemberEditRequest request) { + LogDTO dto = getLog(request.getUserRoleId()); + dto.setType(OperationLogType.UPDATE.name()); + dto.setPath(PRE_URI + "/update"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(request)); + return dto; + } + + private LogDTO getLog(String roleId) { + UserRole userRole = userRoleMapper.selectByPrimaryKey(roleId); + return new LogDTO( + "", + userRole.getScopeId(), + null, + null, + null, + OperationLogModule.ORGANIZATION_USER_ROLE, + userRole.getName()); + } +} diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationControllerTests.java index d7c81aea1e..aa2c60efbd 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationControllerTests.java @@ -240,8 +240,6 @@ public class OrganizationControllerTests extends BaseTest{ this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-x/admin", status().is5xxServerError()); // 用户不存在 this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-3/admin-x", status().is5xxServerError()); - // 用户组织关系不存在 - this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-4/default-admin", status().is5xxServerError()); } @Test