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; - } - -}