feat(系统设置): 增加系统授权管理

This commit is contained in:
wxg0103 2023-08-02 18:02:58 +08:00 committed by fit2-zhao
parent fb01646f23
commit 96a765fa12
10 changed files with 211 additions and 26 deletions

View File

@ -0,0 +1,41 @@
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.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/license")
@Tag(name="license校验")
public class LicenseController {
@GetMapping("/validate")
@Operation(summary = "license校验")
public LicenseDTO validate() {
LicenseService licenseService = CommonBeanFactory.getBean(LicenseService.class);
if (licenseService != null) {
return licenseService.validate();
}
return new LicenseDTO();
}
@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 new LicenseDTO();
}
}

View File

@ -1,25 +0,0 @@
package io.metersphere.sdk.controller;
import io.metersphere.sdk.dto.LicenseDTO;
import io.metersphere.sdk.service.LicenseService;
import io.metersphere.sdk.util.CommonBeanFactory;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/license")
@Tag(name="license校验")
public class LicenseValidateController {
@GetMapping("/validate")
@Operation(summary = "license校验")
public LicenseDTO validate() {
LicenseService licenseService = CommonBeanFactory.getBean(LicenseService.class);
if (licenseService != null) {
return licenseService.validate();
}
return new LicenseDTO();
}
}

View File

@ -7,11 +7,16 @@ 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;
import io.metersphere.sdk.util.Translator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.BooleanUtils;
@ -22,6 +27,7 @@ import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping
@Tag(name="登陆")
public class LoginController {
@Resource
@ -29,6 +35,7 @@ public class LoginController {
@GetMapping(value = "/is-login")
@Operation(summary = "是否登录")
public ResultHolder isLogin() throws Exception {
RsaKey rsaKey = RsaUtil.getRsaKey();
SessionUser user = SessionUtils.getUser();
@ -51,6 +58,8 @@ 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) {
@ -67,6 +76,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 {
SecurityUtils.getSubject().logout();
return ResultHolder.success("logout success");

View File

@ -14,7 +14,8 @@ public enum OperationLogType {
EXPORT,
LOGIN,
SELECT,
RECOVER;
RECOVER,
LOGOUT;
public boolean contains(OperationLogType keyword) {
return this.name().contains(keyword.name());

View File

@ -0,0 +1,35 @@
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;
}
}

View File

@ -9,4 +9,6 @@ public interface LicenseService {
LicenseDTO validate();
LicenseDTO addLicense(String licenseCode);
}

View File

@ -0,0 +1,47 @@
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;
}
}

View File

@ -0,0 +1,21 @@
package io.metersphere.system.job;
import com.fit2cloud.quartz.anno.QuartzScheduled;
import io.metersphere.sdk.dto.LicenseDTO;
import io.metersphere.sdk.service.LicenseService;
import io.metersphere.sdk.util.CommonBeanFactory;
import io.metersphere.sdk.util.LogUtils;
import org.springframework.stereotype.Component;
@Component
public class LicenseCacheJob {
@QuartzScheduled(cron = "0 5 0 * * ?")
public void checkLicenseTask() {
LicenseService licenseService = CommonBeanFactory.getBean(LicenseService.class);
if (licenseService != null) {
LicenseDTO dto = licenseService.refreshLicense();
LogUtils.info("刷新LICENSE状态: " + dto.getStatus());
}
}
}

View File

@ -0,0 +1,24 @@
package io.metersphere.system.job;
import base.BaseTest;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@AutoConfigureMockMvc
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class LicenseCacheTests extends BaseTest {
@Resource
private LicenseCacheJob licenseCacheJob;
@Test
public void cleanupProject() throws Exception{
//TODO
licenseCacheJob.checkLicenseTask();
}
}

View File

@ -0,0 +1,28 @@
package io.metersphere.system.service;
import io.metersphere.sdk.dto.LicenseDTO;
import io.metersphere.sdk.service.LicenseService;
import org.springframework.stereotype.Service;
@Service
public class LicenseServiceImpl implements LicenseService {
public synchronized LicenseDTO refreshLicense() {
LicenseDTO licenseDTO = new LicenseDTO();
return licenseDTO;
}
@Override
public LicenseDTO validate() {
LicenseDTO licenseDTO = new LicenseDTO();
return licenseDTO;
}
@Override
public LicenseDTO addLicense(String licenseCode) {
LicenseDTO licenseDTO = new LicenseDTO();
return licenseDTO;
}
}