From 8fa5ed30dd6a3ace3964fa681ffc7e1a64810a3b Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Tue, 22 Aug 2023 16:01:40 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):?= =?UTF-8?q?=20=E7=99=BB=E5=BD=95=E7=99=BB=E5=87=BA=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sdk/controller/LicenseController.java | 7 +-- .../sdk/controller/LoginController.java | 6 +-- .../sdk/service/BaseUserService.java | 25 ++++++++-- .../sdk/service/LicenseLogService.java | 35 -------------- .../sdk/service/LicenseService.java | 2 +- .../sdk/service/LoginLogService.java | 47 ------------------- 6 files changed, 26 insertions(+), 96 deletions(-) delete mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/service/LicenseLogService.java delete mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/service/LoginLogService.java diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/LicenseController.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/LicenseController.java index 853c8c457e..34a3660025 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/LicenseController.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/LicenseController.java @@ -2,11 +2,9 @@ package io.metersphere.sdk.controller; import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.dto.LicenseDTO; -import io.metersphere.sdk.log.annotation.Log; -import io.metersphere.sdk.log.constants.OperationLogType; -import io.metersphere.sdk.service.LicenseLogService; import io.metersphere.sdk.service.LicenseService; import io.metersphere.sdk.util.CommonBeanFactory; +import io.metersphere.sdk.util.SessionUtils; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import org.apache.shiro.authz.annotation.Logical; @@ -30,11 +28,10 @@ public class LicenseController { @PostMapping("/add") @Operation(summary = "添加有效的License") @RequiresPermissions(value= {PermissionConstants.SYSTEM_AUTH_READ, PermissionConstants.SYSTEM_AUTH_READ_UPDATE}, logical = Logical.OR) - @Log(type = OperationLogType.ADD, expression = "#msClass.addLog()", msClass = LicenseLogService.class) public LicenseDTO addLicense(@RequestBody String licenseCode) { LicenseService licenseService = CommonBeanFactory.getBean(LicenseService.class); if (licenseService != null) { - return licenseService.addLicense(licenseCode); + return licenseService.addLicense(licenseCode, SessionUtils.getUserId()); } return new LicenseDTO(); } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/LoginController.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/LoginController.java index d51e400e90..db8b5982c2 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/LoginController.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/controller/LoginController.java @@ -1,16 +1,15 @@ package io.metersphere.sdk.controller; +import io.metersphere.sdk.constants.HttpMethodConstants; import io.metersphere.sdk.constants.UserSource; import io.metersphere.sdk.controller.handler.ResultHolder; import io.metersphere.sdk.dto.LoginRequest; import io.metersphere.sdk.dto.SessionUser; import io.metersphere.sdk.dto.UserDTO; import io.metersphere.sdk.exception.MSException; -import io.metersphere.sdk.log.annotation.Log; import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.service.BaseUserService; -import io.metersphere.sdk.service.LoginLogService; import io.metersphere.sdk.util.RsaKey; import io.metersphere.sdk.util.RsaUtil; import io.metersphere.sdk.util.SessionUtils; @@ -59,7 +58,6 @@ public class LoginController { @PostMapping(value = "/login") @Operation(summary = "登录") - @Log(type = OperationLogType.LOGIN, expression = "#msClass.loginLog()", msClass = LoginLogService.class) public ResultHolder login(@RequestBody LoginRequest request) { SessionUser sessionUser = SessionUtils.getUser(); if (sessionUser != null) { @@ -77,8 +75,8 @@ public class LoginController { @GetMapping(value = "/signout") @Operation(summary = "退出登录") - @Log(type = OperationLogType.LOGOUT, expression = "#msClass.logoutLog()", msClass = LoginLogService.class) public ResultHolder logout(HttpServletResponse response) throws Exception { + baseUserService.saveLog(SessionUtils.getUserId(), HttpMethodConstants.GET.name(), "/signout", "登出成功", OperationLogType.LOGOUT.name()); SecurityUtils.getSubject().logout(); return ResultHolder.success("logout success"); } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserService.java index 60fd601580..119db36bd8 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserService.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserService.java @@ -3,12 +3,12 @@ package io.metersphere.sdk.service; import io.metersphere.project.domain.Project; import io.metersphere.project.domain.ProjectExample; import io.metersphere.project.mapper.ProjectMapper; -import io.metersphere.sdk.constants.InternalUserRole; -import io.metersphere.sdk.constants.UserRoleType; -import io.metersphere.sdk.constants.UserSource; +import io.metersphere.sdk.constants.*; import io.metersphere.sdk.controller.handler.ResultHolder; import io.metersphere.sdk.dto.*; import io.metersphere.sdk.exception.MSException; +import io.metersphere.sdk.log.constants.OperationLogType; +import io.metersphere.sdk.log.service.OperationLogService; import io.metersphere.sdk.mapper.BaseProjectMapper; import io.metersphere.sdk.mapper.BaseUserMapper; import io.metersphere.sdk.util.CodingUtil; @@ -54,6 +54,8 @@ public class BaseUserService { private ProjectMapper projectMapper; @Resource private BaseProjectMapper baseProjectMapper; + @Resource + private OperationLogService operationLogService; public UserDTO getUserDTO(String userId) { @@ -78,11 +80,11 @@ public class BaseUserService { if (!StringUtils.equals(login, UserSource.LDAP.name())) { password = StringUtils.trim(request.getPassword()); } - UsernamePasswordToken token = new UsernamePasswordToken(username, password); Subject subject = SecurityUtils.getSubject(); try { subject.login(token); + saveLog(SessionUtils.getUserId(), HttpMethodConstants.POST.name(), "/login", "登录成功", OperationLogType.LOGIN.name()); if (subject.isAuthenticated()) { SessionUser sessionUser = SessionUtils.getUser(); autoSwitch(sessionUser); @@ -106,6 +108,21 @@ public class BaseUserService { throw new UnauthorizedException(Translator.get("not_authorized") + e.getMessage()); } } + //保存日志 + public void saveLog(String userId, String method, String path, String content, String type){ + User user = userMapper.selectByPrimaryKey(userId); + LogDTO dto = new LogDTO( + OperationLogConstants.SYSTEM, + OperationLogConstants.SYSTEM, + OperationLogConstants.SYSTEM, + userId, + type, + OperationLogConstants.SYSTEM, + StringUtils.join(user.getName(),StringUtils.EMPTY, content)); + dto.setMethod(method); + dto.setPath(path); + operationLogService.add(dto); + } public void autoSwitch(UserDTO user) { // 用户有 last_project_id 权限 diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/LicenseLogService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/LicenseLogService.java deleted file mode 100644 index 29c4574d5c..0000000000 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/LicenseLogService.java +++ /dev/null @@ -1,35 +0,0 @@ -package io.metersphere.sdk.service; - -import io.metersphere.sdk.constants.HttpMethodConstants; -import io.metersphere.sdk.constants.OperationLogConstants; -import io.metersphere.sdk.dto.LogDTO; -import io.metersphere.sdk.log.constants.OperationLogModule; -import io.metersphere.sdk.log.constants.OperationLogType; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -@Service -@Transactional(rollbackFor = Exception.class) -public class LicenseLogService { - - /** - * 添加接口日志 - * - * @return - */ - public LogDTO addLog() { - LogDTO dto = new LogDTO( - OperationLogConstants.SYSTEM, - OperationLogConstants.SYSTEM, - null, - null, - OperationLogType.ADD.name(), - OperationLogModule.SYSTEM_AUTHORIZATION_MANAGEMENT, - "License授权"); - - dto.setPath("/license/add"); - dto.setMethod(HttpMethodConstants.POST.name()); - return dto; - } - -} diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/LicenseService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/LicenseService.java index 451d8eb065..ae8181b563 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/LicenseService.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/LicenseService.java @@ -9,6 +9,6 @@ public interface LicenseService { LicenseDTO validate(); - LicenseDTO addLicense(String licenseCode); + LicenseDTO addLicense(String licenseCode, String userId); } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/LoginLogService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/LoginLogService.java deleted file mode 100644 index 4f07fce1fb..0000000000 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/LoginLogService.java +++ /dev/null @@ -1,47 +0,0 @@ -package io.metersphere.sdk.service; - -import io.metersphere.sdk.constants.OperationLogConstants; -import io.metersphere.sdk.dto.LogDTO; -import io.metersphere.sdk.log.constants.OperationLogType; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -@Service -@Transactional(rollbackFor = Exception.class) -public class LoginLogService { - - /** - * 添加接口日志 - * - * @return - */ - public LogDTO loginLog() { - LogDTO dto = new LogDTO( - OperationLogConstants.SYSTEM, - OperationLogConstants.SYSTEM, - null, - null, - OperationLogType.LOGIN.name(), - OperationLogConstants.SYSTEM, - "登录"); - return dto; - } - - /** - * 添加接口日志 - * - * @return - */ - public LogDTO logoutLog() { - LogDTO dto = new LogDTO( - OperationLogConstants.SYSTEM, - OperationLogConstants.SYSTEM, - null, - null, - OperationLogType.LOGIN.name(), - OperationLogConstants.SYSTEM, - "登出"); - return dto; - } - -}