From c1c0f7bf1639001d2218421f6b3bbd7e679c82bc Mon Sep 17 00:00:00 2001 From: fit2cloud-chenyw Date: Wed, 13 Sep 2023 21:26:09 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=97=A5=E5=BF=97):=20apikey=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/aop/DeLogAnnotationHandler.java | 23 +++---- .../commons/constants/SysLogConstants.java | 3 +- .../java/io/dataease/ext/ExtSysLogMapper.xml | 11 ++++ .../plugins/server/XUserKeysServer.java | 21 ++++++- .../dataease/service/sys/log/LogService.java | 63 ++++++++----------- .../resources/i18n/messages_en_US.properties | 1 + .../resources/i18n/messages_zh_CN.properties | 1 + .../resources/i18n/messages_zh_TW.properties | 1 + 8 files changed, 69 insertions(+), 55 deletions(-) diff --git a/core/backend/src/main/java/io/dataease/auth/aop/DeLogAnnotationHandler.java b/core/backend/src/main/java/io/dataease/auth/aop/DeLogAnnotationHandler.java index 9adcf3e2fc..6bc3b09c94 100644 --- a/core/backend/src/main/java/io/dataease/auth/aop/DeLogAnnotationHandler.java +++ b/core/backend/src/main/java/io/dataease/auth/aop/DeLogAnnotationHandler.java @@ -4,7 +4,6 @@ package io.dataease.auth.aop; import io.dataease.auth.annotation.DeLog; import io.dataease.commons.constants.SysLogConstants; import io.dataease.commons.utils.AopUtils; -import io.dataease.commons.utils.DeLogUtils; import io.dataease.controller.ResultHolder; import io.dataease.dto.SysLogDTO; import io.dataease.dto.log.FolderItem; @@ -37,7 +36,7 @@ public class DeLogAnnotationHandler { @Resource private LogService logService; - private static List before = new ArrayList<>(); + private static final List before = new ArrayList<>(); @PostConstruct public void init() { @@ -46,7 +45,7 @@ public class DeLogAnnotationHandler { before.add(SysLogConstants.OPERATE_TYPE.UNAUTHORIZE.getValue()); } - private SysLogDTO exec(JoinPoint point, DeLog deLog) throws Exception{ + private SysLogDTO exec(JoinPoint point, DeLog deLog) throws Exception { Object[] args = point.getArgs(); if (ArrayUtils.isEmpty(args)) return null; @@ -73,7 +72,7 @@ public class DeLogAnnotationHandler { // 填充资源位置信息 int positionIndex = deLog.positionIndex(); - if (positionIndex > -1 && args.length > positionIndex){ + if (positionIndex > -1 && args.length > positionIndex) { String positionKey = deLog.positionKey(); Object positionArg = args[positionIndex]; Object bottomPositionValue = AopUtils.getParamValue(positionArg, positionKey, 0); @@ -83,7 +82,7 @@ public class DeLogAnnotationHandler { List items = new ArrayList<>(); items.add(folderItem); sysLogDTO.setPositions(items); - }else { + } else { List parentsAndSelf = logManager.parentsAndSelf(bottomPositionValue.toString(), sourcetype); sysLogDTO.setPositions(parentsAndSelf); } @@ -92,7 +91,7 @@ public class DeLogAnnotationHandler { } // 填充资源目标位置信息 int targetIndex = deLog.targetIndex(); - if (targetIndex > -1 && args.length > targetIndex){ + if (targetIndex > -1 && args.length > targetIndex) { String targetKey = deLog.targetKey(); Object targetArg = args[targetIndex]; SysLogConstants.SOURCE_TYPE targetType = deLog.targetType(); @@ -110,11 +109,11 @@ public class DeLogAnnotationHandler { SysLogDTO logDTO = null; Object result = null; DeLog log = getLog(point); - if(before.contains(log.operatetype().getValue())) { + if (before.contains(log.operatetype().getValue())) { // 前置处理 比如删除操作 需要在数据删除之前查询 logDTO = exec(point, log); result = point.proceed(point.getArgs()); - }else { + } else { // 后置处理 比如保存操作 需要在保存之后才有主键 result = point.proceed(point.getArgs()); logDTO = exec(point, log); @@ -128,15 +127,9 @@ public class DeLogAnnotationHandler { } - private DeLog getLog(JoinPoint point) { MethodSignature ms = (MethodSignature) point.getSignature(); Method method = ms.getMethod(); - DeLog deLog = method.getAnnotation(DeLog.class); - return deLog; + return method.getAnnotation(DeLog.class); } - - - - } diff --git a/core/backend/src/main/java/io/dataease/commons/constants/SysLogConstants.java b/core/backend/src/main/java/io/dataease/commons/constants/SysLogConstants.java index 2c309f8f09..6a4c0583c8 100644 --- a/core/backend/src/main/java/io/dataease/commons/constants/SysLogConstants.java +++ b/core/backend/src/main/java/io/dataease/commons/constants/SysLogConstants.java @@ -69,7 +69,8 @@ public class SysLogConstants { ROLE(8, "SOURCE_TYPE_ROLE"), DRIVER(9, "SOURCE_TYPE_DRIVER"), DRIVER_FILE(10, "SOURCE_TYPE_DRIVER_FILE"), - MENU(11, "SOURCE_TYPE_MENU"); + MENU(11, "SOURCE_TYPE_MENU"), + APIKEY(12, "SOURCE_TYPE_APIKEY"); private Integer value; private String name; diff --git a/core/backend/src/main/java/io/dataease/ext/ExtSysLogMapper.xml b/core/backend/src/main/java/io/dataease/ext/ExtSysLogMapper.xml index f3c7d7e02d..0a385b3cec 100644 --- a/core/backend/src/main/java/io/dataease/ext/ExtSysLogMapper.xml +++ b/core/backend/src/main/java/io/dataease/ext/ExtSysLogMapper.xml @@ -195,6 +195,17 @@ + + id, access_key as name + from user_key + + id in + + #{id} + + + + diff --git a/core/backend/src/main/java/io/dataease/plugins/server/XUserKeysServer.java b/core/backend/src/main/java/io/dataease/plugins/server/XUserKeysServer.java index 4cbbce3b1a..49ec24422e 100644 --- a/core/backend/src/main/java/io/dataease/plugins/server/XUserKeysServer.java +++ b/core/backend/src/main/java/io/dataease/plugins/server/XUserKeysServer.java @@ -1,5 +1,7 @@ package io.dataease.plugins.server; +import io.dataease.auth.annotation.DeLog; +import io.dataease.commons.constants.SysLogConstants; import io.dataease.commons.utils.AuthUtils; import io.dataease.plugins.config.SpringContextUtil; import io.dataease.plugins.xpack.ukey.dto.request.XpackUkeyDto; @@ -18,7 +20,6 @@ public class XUserKeysServer { @PostMapping("info") public List getUserKeysInfo() { - UkeyXpackService ukeyXpackService = SpringContextUtil.getBean(UkeyXpackService.class); Long userId = AuthUtils.getUser().getUserId(); return ukeyXpackService.getUserKeysInfo(userId); @@ -29,19 +30,33 @@ public class XUserKeysServer { return null; } + @DeLog( + operatetype = SysLogConstants.OPERATE_TYPE.CREATE, + sourcetype = SysLogConstants.SOURCE_TYPE.APIKEY, + value = "id" + ) @PostMapping("generate") - public void generateUserKey() { + public void generateUserKey(@RequestBody XpackUkeyDto ukeyDto) { UkeyXpackService ukeyXpackService = SpringContextUtil.getBean(UkeyXpackService.class); Long userId = AuthUtils.getUser().getUserId(); - ukeyXpackService.generateUserKey(userId); + XpackUkeyDto xpackUkeyDto = ukeyXpackService.generateUserKey(userId); + ukeyDto.setId(xpackUkeyDto.getId()); } + @DeLog( + operatetype = SysLogConstants.OPERATE_TYPE.DELETE, + sourcetype = SysLogConstants.SOURCE_TYPE.APIKEY + ) @PostMapping("delete/{id}") public void deleteUserKey(@PathVariable Long id) { UkeyXpackService ukeyXpackService = SpringContextUtil.getBean(UkeyXpackService.class); ukeyXpackService.deleteUserKey(id); } + @DeLog( + operatetype = SysLogConstants.OPERATE_TYPE.MODIFY, + sourcetype = SysLogConstants.SOURCE_TYPE.APIKEY + ) @PostMapping("changeStatus/{id}") public void changeStatus(@PathVariable Long id) { UkeyXpackService ukeyXpackService = SpringContextUtil.getBean(UkeyXpackService.class); diff --git a/core/backend/src/main/java/io/dataease/service/sys/log/LogService.java b/core/backend/src/main/java/io/dataease/service/sys/log/LogService.java index c0a74e7ce1..4bf5073891 100644 --- a/core/backend/src/main/java/io/dataease/service/sys/log/LogService.java +++ b/core/backend/src/main/java/io/dataease/service/sys/log/LogService.java @@ -32,36 +32,34 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.OutputStream; import java.net.URLEncoder; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.List; +import java.nio.charset.StandardCharsets; +import java.util.*; import java.util.stream.Collectors; @Service public class LogService { private static final String LOG_RETENTION = "30"; - private Gson gson = new Gson(); + private final Gson gson = new Gson(); // 仪表板的额外操作 分享以及公共链接 - private static Integer[] panel_ext_ope = {4, 5, 8, 9, 10}; + private static final Integer[] panel_ext_ope = {4, 5, 8, 9, 10}; - private static Integer[] link_ext_ope = {13, 14}; + private static final Integer[] link_ext_ope = {13, 14}; // 驱动文件操作 上传, 删除 - private static Integer[] driver_file_ope = {11, 3}; + private static final Integer[] driver_file_ope = {11, 3}; - private static Integer[] COMMON_SOURCE = {1, 2, 3, 6, 7, 8, 9}; + private static final Integer[] COMMON_SOURCE = {1, 2, 3, 6, 7, 8, 9, 12}; // 增 改 删 针对公共资源的操作 - private static Integer[] COMMON_SOURCE_OPERATE = {1, 2, 3}; + private static final Integer[] COMMON_SOURCE_OPERATE = {1, 2, 3}; // 授权相关操作 - private static Integer[] AUTH_OPERATE = {6, 7}; + private static final Integer[] AUTH_OPERATE = {6, 7}; // 授权相关资源 数据源 仪表板 数据集 菜单 - private static Integer[] AUTH_SOURCE = {1, 2, 3, 11}; + private static final Integer[] AUTH_SOURCE = {1, 2, 3, 11}; @Resource @@ -108,14 +106,12 @@ public class LogService { List results = new ArrayList<>(); - for (int i = 0; i < COMMON_SOURCE.length; i++) { + for (Integer sourceVal : COMMON_SOURCE) { - Integer sourceVal = COMMON_SOURCE[i]; String sourceTypeName = SysLogConstants.sourceTypeName(sourceVal); - for (int j = 0; j < COMMON_SOURCE_OPERATE.length; j++) { + for (Integer operateVal : COMMON_SOURCE_OPERATE) { - Integer operateVal = COMMON_SOURCE_OPERATE[j]; String operateTypeName = SysLogConstants.operateTypeName(operateVal); FolderItem folderItem = new FolderItem(); folderItem.setId(operateVal + "-" + sourceVal); @@ -125,23 +121,21 @@ public class LogService { } - for (int i = 0; i < driver_file_ope.length; i++) { + for (Integer integer : driver_file_ope) { SysLogConstants.SOURCE_TYPE sourceType = SysLogConstants.SOURCE_TYPE.DRIVER_FILE; FolderItem folderItem = new FolderItem(); - folderItem.setId(driver_file_ope[i] + "-" + sourceType.getValue()); - String operateTypeName = SysLogConstants.operateTypeName(driver_file_ope[i]); + folderItem.setId(integer + "-" + sourceType.getValue()); + String operateTypeName = SysLogConstants.operateTypeName(integer); String sourceTypeName = sourceType.getName(); folderItem.setName(Translator.get(operateTypeName) + Translator.get(sourceTypeName)); results.add(folderItem); } - for (int i = 0; i < AUTH_SOURCE.length; i++) { - Integer sourceVal = AUTH_SOURCE[i]; + for (Integer sourceVal : AUTH_SOURCE) { String sourceTypeName = SysLogConstants.sourceTypeName(sourceVal); - for (int j = 0; j < AUTH_OPERATE.length; j++) { + for (Integer operateVal : AUTH_OPERATE) { - Integer operateVal = AUTH_OPERATE[j]; String operateTypeName = SysLogConstants.operateTypeName(operateVal); FolderItem folderItem = new FolderItem(); folderItem.setId(operateVal + "-" + sourceVal); @@ -150,21 +144,21 @@ public class LogService { } } - for (int i = 0; i < panel_ext_ope.length; i++) { + for (Integer integer : panel_ext_ope) { SysLogConstants.SOURCE_TYPE sourceType = SysLogConstants.SOURCE_TYPE.PANEL; FolderItem folderItem = new FolderItem(); - folderItem.setId(panel_ext_ope[i] + "-" + sourceType.getValue()); - String operateTypeName = SysLogConstants.operateTypeName(panel_ext_ope[i]); + folderItem.setId(integer + "-" + sourceType.getValue()); + String operateTypeName = SysLogConstants.operateTypeName(integer); String sourceTypeName = sourceType.getName(); folderItem.setName(Translator.get(operateTypeName) + Translator.get(sourceTypeName)); results.add(folderItem); } - for (int i = 0; i < link_ext_ope.length; i++) { + for (Integer integer : link_ext_ope) { SysLogConstants.SOURCE_TYPE sourceType = SysLogConstants.SOURCE_TYPE.LINK; FolderItem folderItem = new FolderItem(); - folderItem.setId(link_ext_ope[i] + "-" + sourceType.getValue()); - String operateTypeName = SysLogConstants.operateTypeName(link_ext_ope[i]); + folderItem.setId(integer + "-" + sourceType.getValue()); + String operateTypeName = SysLogConstants.operateTypeName(integer); String sourceTypeName = sourceType.getName(); folderItem.setName(Translator.get(operateTypeName) + Translator.get(sourceTypeName)); results.add(folderItem); @@ -194,13 +188,10 @@ public class LogService { private List typesByArr(Integer[] opTypes, Integer[] sourceTypes) { List results = new ArrayList<>(); - for (int i = 0; i < sourceTypes.length; i++) { - Integer sourceVal = sourceTypes[i]; + for (Integer sourceVal : sourceTypes) { String sourceTypeName = SysLogConstants.sourceTypeName(sourceVal); - for (int j = 0; j < opTypes.length; j++) { - - Integer operateVal = opTypes[j]; + for (Integer operateVal : opTypes) { String operateTypeName = SysLogConstants.operateTypeName(operateVal); FolderItem folderItem = new FolderItem(); @@ -249,7 +240,7 @@ public class LogService { sysLogWithBLOBs.setUserId(user.getUserId()); sysLogWithBLOBs.setLoginName(user.getUsername()); sysLogWithBLOBs.setNickName(user.getNickName()); - } else if (sysLogDTO.getOperateType() == SysLogConstants.OPERATE_TYPE.LOGIN.getValue()) { + } else if (Objects.equals(sysLogDTO.getOperateType(), SysLogConstants.OPERATE_TYPE.LOGIN.getValue())) { sysLogWithBLOBs.setUserId(Long.parseLong(sysLogDTO.getSourceId())); sysLogWithBLOBs.setLoginName(sysLogDTO.getSourceName()); sysLogWithBLOBs.setNickName(sysLogDTO.getSourceName()); @@ -320,7 +311,7 @@ public class LogService { response.setContentType("application/vnd.ms-excel"); //文件名称 String fileName = "DataEase操作日志"; - String encodeFileName = URLEncoder.encode(fileName, "UTF-8"); + String encodeFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8); response.setHeader("Content-disposition", "attachment;filename=" + encodeFileName + ".xlsx"); wb.write(outputStream); outputStream.flush(); diff --git a/core/backend/src/main/resources/i18n/messages_en_US.properties b/core/backend/src/main/resources/i18n/messages_en_US.properties index 090389c5c3..98aed94418 100644 --- a/core/backend/src/main/resources/i18n/messages_en_US.properties +++ b/core/backend/src/main/resources/i18n/messages_en_US.properties @@ -170,6 +170,7 @@ SOURCE_TYPE_DRIVER=DRIVER SOURCE_TYPE_DRIVER_FILE=DRIVER FILE SOURCE_TYPE_MENU=MENU SOURCE_TYPE_LINK=PUBLIC LINK +SOURCE_TYPE_APIKEY=APIKEY I18N_OPERATE_TYPE=Operation type I18N_DETAIL=Operation details I18N_USER=Operator diff --git a/core/backend/src/main/resources/i18n/messages_zh_CN.properties b/core/backend/src/main/resources/i18n/messages_zh_CN.properties index 05b70ad31a..880f338e84 100644 --- a/core/backend/src/main/resources/i18n/messages_zh_CN.properties +++ b/core/backend/src/main/resources/i18n/messages_zh_CN.properties @@ -170,6 +170,7 @@ SOURCE_TYPE_DRIVER=\u9A71\u52A8 SOURCE_TYPE_DRIVER_FILE=\u9A71\u52A8\u6587\u4EF6 SOURCE_TYPE_MENU=\u83DC\u5355 SOURCE_TYPE_LINK=\u516C\u5171\u94FE\u63A5 +SOURCE_TYPE_APIKEY=APIKEY I18N_OPERATE_TYPE=\u64CD\u4F5C\u7C7B\u578B I18N_DETAIL=\u64CD\u4F5C\u8BE6\u60C5 I18N_USER=\u64CD\u4F5C\u4EBA diff --git a/core/backend/src/main/resources/i18n/messages_zh_TW.properties b/core/backend/src/main/resources/i18n/messages_zh_TW.properties index f4c4df9008..8ba40473b3 100644 --- a/core/backend/src/main/resources/i18n/messages_zh_TW.properties +++ b/core/backend/src/main/resources/i18n/messages_zh_TW.properties @@ -170,6 +170,7 @@ SOURCE_TYPE_DRIVER=\u9A45\u52D5 SOURCE_TYPE_DRIVER_FILE=\u9A45\u52D5\u6587\u4EF6 SOURCE_TYPE_MENU=\u83DC\u55AE SOURCE_TYPE_LINK=\u516C\u5171\u93C8\u63A5 +SOURCE_TYPE_APIKEY=APIKEY I18N_DRIVER_NOT_DELETE=\u4F7F\u7528\u4E2D\u7684\u9A45\u52D5\u4E0D\u5141\u8A31\u5220\u9664 I18N_DRIVER_REPEAT_NAME=\u540D\u7A31\u91CD\u8907 I18N_DRIVER_NOT_FOUND=\u672A\u627E\u5230\u9A45\u52D5