feat(系统设置):系统参数设置(基本配置、邮件设置、认证设置)功能开发
This commit is contained in:
parent
d2ed865d63
commit
103cb3e999
|
@ -0,0 +1,68 @@
|
|||
package io.metersphere.sdk.constants;
|
||||
|
||||
|
||||
public interface ParamConstants {
|
||||
|
||||
String getValue();
|
||||
|
||||
enum Classify implements ParamConstants {
|
||||
MAIL("smtp"),
|
||||
BASE("base"),
|
||||
LDAP("ldap"),
|
||||
REGISTRY("registry");
|
||||
|
||||
private String value;
|
||||
|
||||
Classify(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
enum BASE implements ParamConstants {
|
||||
URL("base.url"),
|
||||
PROMETHEUS_HOST("base.prometheus.host");
|
||||
|
||||
private String value;
|
||||
|
||||
private BASE(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
enum MAIL implements ParamConstants {
|
||||
SERVER("smtp.host"),
|
||||
PORT("smtp.port"),
|
||||
ACCOUNT("smtp.account"),
|
||||
FROM("smtp.from"),
|
||||
PASSWORD("smtp.password"),
|
||||
SSL("smtp.ssl"),
|
||||
TLS("smtp.tls"),
|
||||
RECIPIENTS("smtp.recipient");
|
||||
|
||||
private String value;
|
||||
|
||||
private MAIL(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValue() {
|
||||
return this.value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -27,6 +27,8 @@ public class PermissionConstants {
|
|||
|
||||
public static final String SYSTEM_SETTING_READ = "SYSTEM_SETTING:READ";
|
||||
public static final String SYSTEM_SETTING_READ_UPDATE = "SYSTEM_SETTING:READ+UPDATE";
|
||||
public static final String SYSTEM_SETTING_READ_CREAT = "SYSTEM_SETTING:READ+CREAT";
|
||||
public static final String SYSTEM_SETTING_READ_DELETE = "SYSTEM_SETTING:READ+DELETE";
|
||||
public static final String SYSTEM_SETTING_READ_AUTH_MANAGE = "SYSTEM_SETTING:READ+AUTH_MANAGE";
|
||||
public static final String SYSTEM_QUOTA_READ = "SYSTEM_QUOTA:READ";
|
||||
public static final String SYSTEM_QUOTA_READ_UPDATE = "SYSTEM_QUOTA:READ+UPDATE";
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package io.metersphere.sdk.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class EMailInfoDto {
|
||||
private String host;
|
||||
private String port;
|
||||
private String account;
|
||||
private String from;
|
||||
private String password;
|
||||
private String ssl;
|
||||
private String tls;
|
||||
private String recipient;
|
||||
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package io.metersphere.sdk.notice.sender;
|
||||
|
||||
public class AbstractNoticeSender {
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package io.metersphere.sdk.notice.sender.impl;
|
||||
|
||||
|
||||
import io.metersphere.sdk.constants.ParamConstants;
|
||||
import io.metersphere.sdk.notice.sender.AbstractNoticeSender;
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
@Component
|
||||
public class MailNoticeSender extends AbstractNoticeSender {
|
||||
|
||||
|
||||
public JavaMailSenderImpl getMailSender(Map<String, String> paramMap) {
|
||||
Properties props = new Properties();
|
||||
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
|
||||
javaMailSender.setDefaultEncoding(StandardCharsets.UTF_8.name());
|
||||
javaMailSender.setProtocol("smtp");
|
||||
javaMailSender.setHost(paramMap.get(ParamConstants.MAIL.SERVER.getValue()));
|
||||
javaMailSender.setPort(Integer.parseInt(paramMap.get(ParamConstants.MAIL.PORT.getValue())));
|
||||
javaMailSender.setUsername(paramMap.get(ParamConstants.MAIL.ACCOUNT.getValue()));
|
||||
javaMailSender.setPassword(paramMap.get(ParamConstants.MAIL.PASSWORD.getValue()));
|
||||
|
||||
if (BooleanUtils.toBoolean(paramMap.get(ParamConstants.MAIL.SSL.getValue()))) {
|
||||
javaMailSender.setProtocol("smtps");
|
||||
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
|
||||
}
|
||||
if (BooleanUtils.toBoolean(paramMap.get(ParamConstants.MAIL.TLS.getValue()))) {
|
||||
String result = BooleanUtils.toString(BooleanUtils.toBoolean(paramMap.get(ParamConstants.MAIL.TLS.getValue())), "true", "false");
|
||||
props.put("mail.smtp.starttls.enable", result);
|
||||
props.put("mail.smtp.starttls.required", result);
|
||||
}
|
||||
|
||||
props.put("mail.smtp.ssl.trust", javaMailSender.getHost());
|
||||
props.put("mail.smtp.auth", "true");
|
||||
props.put("mail.smtp.timeout", "30000");
|
||||
props.put("mail.smtp.connectiontimeout", "5000");
|
||||
javaMailSender.setJavaMailProperties(props);
|
||||
return javaMailSender;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,202 @@
|
|||
package io.metersphere.sdk.service;
|
||||
|
||||
import io.metersphere.dto.BaseSystemConfigDTO;
|
||||
import io.metersphere.sdk.constants.ParamConstants;
|
||||
import io.metersphere.sdk.dto.EMailInfoDto;
|
||||
import io.metersphere.sdk.exception.MSException;
|
||||
import io.metersphere.sdk.notice.sender.impl.MailNoticeSender;
|
||||
import io.metersphere.sdk.util.EncryptUtils;
|
||||
import io.metersphere.sdk.util.LogUtils;
|
||||
import io.metersphere.sdk.util.Translator;
|
||||
import io.metersphere.system.domain.SystemParameter;
|
||||
import io.metersphere.system.domain.SystemParameterExample;
|
||||
import io.metersphere.system.mapper.SystemParameterMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.mail.internet.InternetAddress;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public class SystemParameterService {
|
||||
|
||||
@Resource
|
||||
SystemParameterMapper systemParameterMapper;
|
||||
@Resource
|
||||
MailNoticeSender mailNoticeSender;
|
||||
|
||||
public void saveBaseInfo(List<SystemParameter> parameters) {
|
||||
SystemParameterExample example = new SystemParameterExample();
|
||||
parameters.forEach(param -> {
|
||||
param.setParamValue(StringUtils.removeEnd(param.getParamValue(), "/"));
|
||||
if (StringUtils.equals(param.getParamKey(), "base.url")) {
|
||||
example.createCriteria().andParamKeyEqualTo(param.getParamKey());
|
||||
List<SystemParameter> baseUrlParameterList = systemParameterMapper.selectByExample(example);
|
||||
if (CollectionUtils.isNotEmpty(baseUrlParameterList)) {
|
||||
SystemParameter parameter = baseUrlParameterList.get(0);
|
||||
if (!StringUtils.equals(parameter.getParamValue(), param.getParamValue())) {
|
||||
systemParameterMapper.updateByPrimaryKey(param);
|
||||
}
|
||||
} else {
|
||||
systemParameterMapper.insert(param);
|
||||
}
|
||||
example.clear();
|
||||
} else {
|
||||
example.createCriteria().andParamKeyEqualTo(param.getParamKey());
|
||||
if (systemParameterMapper.countByExample(example) > 0) {
|
||||
systemParameterMapper.updateByPrimaryKey(param);
|
||||
} else {
|
||||
systemParameterMapper.insert(param);
|
||||
}
|
||||
example.clear();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public BaseSystemConfigDTO getBaseInfo() {
|
||||
List<SystemParameter> paramList = this.getParamList(ParamConstants.Classify.BASE.getValue());
|
||||
return TransBaseToDto(paramList);
|
||||
}
|
||||
|
||||
private BaseSystemConfigDTO TransBaseToDto(List<SystemParameter> paramList) {
|
||||
BaseSystemConfigDTO baseSystemConfigDTO = new BaseSystemConfigDTO();
|
||||
if (!CollectionUtils.isEmpty(paramList)) {
|
||||
for (SystemParameter param : paramList) {
|
||||
if (StringUtils.equals(param.getParamKey(), ParamConstants.BASE.URL.getValue())) {
|
||||
baseSystemConfigDTO.setUrl(param.getParamValue());
|
||||
}
|
||||
if (StringUtils.equals(param.getParamKey(), ParamConstants.BASE.PROMETHEUS_HOST.getValue())) {
|
||||
baseSystemConfigDTO.setPrometheusHost(param.getParamValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
return baseSystemConfigDTO;
|
||||
}
|
||||
|
||||
|
||||
public List<SystemParameter> getParamList(String type) {
|
||||
SystemParameterExample example = new SystemParameterExample();
|
||||
example.createCriteria().andParamKeyLike(type + "%");
|
||||
return systemParameterMapper.selectByExample(example);
|
||||
}
|
||||
|
||||
public EMailInfoDto getEmailInfo() {
|
||||
List<SystemParameter> paramList = this.getParamList(ParamConstants.Classify.MAIL.getValue());
|
||||
return TransEmailToDto(paramList);
|
||||
}
|
||||
|
||||
private EMailInfoDto TransEmailToDto(List<SystemParameter> paramList) {
|
||||
EMailInfoDto mailInfo = new EMailInfoDto();
|
||||
if (!CollectionUtils.isEmpty(paramList)) {
|
||||
for (SystemParameter param : paramList) {
|
||||
if (StringUtils.equals(param.getParamKey(), ParamConstants.MAIL.SERVER.getValue())) {
|
||||
mailInfo.setHost(param.getParamValue());
|
||||
} else if (StringUtils.equals(param.getParamKey(), ParamConstants.MAIL.PORT.getValue())) {
|
||||
mailInfo.setPort(param.getParamValue());
|
||||
} else if (StringUtils.equals(param.getParamKey(), ParamConstants.MAIL.ACCOUNT.getValue())) {
|
||||
mailInfo.setAccount(param.getParamValue());
|
||||
} else if (StringUtils.equals(param.getParamKey(), ParamConstants.MAIL.FROM.getValue())) {
|
||||
mailInfo.setFrom(param.getParamValue());
|
||||
} else if (StringUtils.equals(param.getParamKey(), ParamConstants.MAIL.PASSWORD.getValue())) {
|
||||
String password = EncryptUtils.aesDecrypt(param.getParamValue()).toString();
|
||||
mailInfo.setPassword(password);
|
||||
} else if (StringUtils.equals(param.getParamKey(), ParamConstants.MAIL.SSL.getValue())) {
|
||||
mailInfo.setSsl(param.getParamValue());
|
||||
} else if (StringUtils.equals(param.getParamKey(), ParamConstants.MAIL.TLS.getValue())) {
|
||||
mailInfo.setTls(param.getParamValue());
|
||||
} else if (StringUtils.equals(param.getParamKey(), ParamConstants.MAIL.RECIPIENTS.getValue())) {
|
||||
mailInfo.setRecipient(param.getParamValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
return mailInfo;
|
||||
}
|
||||
|
||||
public void editEMailInfo(List<SystemParameter> parameters) {
|
||||
SystemParameterExample example = new SystemParameterExample();
|
||||
parameters.forEach(parameter -> {
|
||||
if (parameter.getParamKey().equals(ParamConstants.MAIL.PASSWORD.getValue())) {
|
||||
if (!StringUtils.isBlank(parameter.getParamValue())) {
|
||||
String string = EncryptUtils.aesEncrypt(parameter.getParamValue()).toString();
|
||||
parameter.setParamValue(string);
|
||||
}
|
||||
}
|
||||
example.createCriteria().andParamKeyEqualTo(parameter.getParamKey());
|
||||
if (systemParameterMapper.countByExample(example) > 0) {
|
||||
systemParameterMapper.updateByPrimaryKey(parameter);
|
||||
} else {
|
||||
systemParameterMapper.insert(parameter);
|
||||
}
|
||||
example.clear();
|
||||
});
|
||||
}
|
||||
|
||||
public void testEmailConnection(HashMap<String, String> hashMap) {
|
||||
JavaMailSenderImpl javaMailSender = null;
|
||||
try {
|
||||
javaMailSender = mailNoticeSender.getMailSender(hashMap);
|
||||
javaMailSender.testConnection();
|
||||
} catch (Exception e) {
|
||||
LogUtils.error(e.getMessage(), e);
|
||||
throw new MSException(Translator.get("connection_failed"));
|
||||
}
|
||||
|
||||
String recipients = hashMap.get(ParamConstants.MAIL.RECIPIENTS.getValue());
|
||||
if (!StringUtils.isBlank(recipients)) {
|
||||
try {
|
||||
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
|
||||
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
|
||||
String username = javaMailSender.getUsername();
|
||||
String email;
|
||||
if (username.contains("@")) {
|
||||
email = username;
|
||||
} else {
|
||||
String mailHost = javaMailSender.getHost();
|
||||
String domainName = mailHost.substring(mailHost.indexOf(".") + 1);
|
||||
email = username + "@" + domainName;
|
||||
}
|
||||
InternetAddress from = new InternetAddress();
|
||||
String smtpFrom = hashMap.get(ParamConstants.MAIL.FROM.getValue());
|
||||
if (StringUtils.isBlank(smtpFrom)) {
|
||||
from.setAddress(email);
|
||||
from.setPersonal(username);
|
||||
} else {
|
||||
// 指定发件人后,address 应该是邮件服务器验证过的发件人
|
||||
if (smtpFrom.contains("@")) {
|
||||
from.setAddress(smtpFrom);
|
||||
} else {
|
||||
from.setAddress(email);
|
||||
}
|
||||
from.setPersonal(smtpFrom);
|
||||
}
|
||||
helper.setFrom(from);
|
||||
|
||||
LogUtils.debug("发件人地址" + javaMailSender.getUsername());
|
||||
LogUtils.debug("helper" + helper);
|
||||
helper.setSubject("MeterSphere测试邮件");
|
||||
|
||||
LogUtils.info("收件人地址: {}", Arrays.asList(recipients));
|
||||
helper.setText("这是一封测试邮件,邮件发送成功", true);
|
||||
helper.setTo(recipients);
|
||||
try {
|
||||
javaMailSender.send(mimeMessage);
|
||||
} catch (Exception e) {
|
||||
LogUtils.error("发送邮件失败: ", e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LogUtils.error(e);
|
||||
throw new MSException(Translator.get("connection_failed"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package io.metersphere.system.controller;
|
||||
|
||||
import com.github.pagehelper.Page;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import io.metersphere.sdk.constants.PermissionConstants;
|
||||
import io.metersphere.sdk.log.annotation.RequestLog;
|
||||
import io.metersphere.sdk.log.constants.OperationLogModule;
|
||||
import io.metersphere.sdk.log.constants.OperationLogType;
|
||||
import io.metersphere.sdk.util.PageUtils;
|
||||
import io.metersphere.sdk.util.Pager;
|
||||
import io.metersphere.system.domain.AuthSource;
|
||||
import io.metersphere.system.service.AuthSourceService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/system/authsource")
|
||||
public class AuthSourceController {
|
||||
@Resource
|
||||
private AuthSourceService authSourceService;
|
||||
|
||||
@PostMapping("/list/{goPage}/{pageSize}")
|
||||
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ)
|
||||
public Pager<List<AuthSource>> list(@PathVariable int goPage, @PathVariable int pageSize) {
|
||||
Page<Object> page = PageHelper.startPage(goPage, pageSize, true);
|
||||
return PageUtils.setPageInfo(page, authSourceService.list());
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_CREAT)
|
||||
@RequestLog(type = OperationLogType.ADD, module = OperationLogModule.SYSTEM_PARAMETER_SETTING,
|
||||
details = "#authSource.type")
|
||||
public void add(@RequestBody AuthSource authSource) {
|
||||
authSourceService.addAuthSource(authSource);
|
||||
}
|
||||
|
||||
@PostMapping("/update")
|
||||
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_UPDATE)
|
||||
@RequestLog(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING,
|
||||
details = "#authSource.type")
|
||||
public void update(@RequestBody AuthSource authSource) {
|
||||
authSourceService.updateAuthSource(authSource);
|
||||
}
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ)
|
||||
public AuthSource get(@PathVariable(value = "id") String id) {
|
||||
return authSourceService.getAuthSource(id);
|
||||
}
|
||||
|
||||
@GetMapping("/delete/{id}")
|
||||
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_DELETE)
|
||||
@RequestLog(type = OperationLogType.DELETE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING,
|
||||
details = "#authSource.type")
|
||||
public void delete(@PathVariable(value = "id") String id) {
|
||||
authSourceService.deleteAuthSource(id);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
package io.metersphere.system.controller;
|
||||
|
||||
|
||||
import io.metersphere.dto.BaseSystemConfigDTO;
|
||||
import io.metersphere.sdk.constants.PermissionConstants;
|
||||
import io.metersphere.sdk.dto.EMailInfoDto;
|
||||
import io.metersphere.sdk.log.annotation.RequestLog;
|
||||
import io.metersphere.sdk.log.constants.OperationLogModule;
|
||||
import io.metersphere.sdk.log.constants.OperationLogType;
|
||||
import io.metersphere.sdk.service.SystemParameterService;
|
||||
import io.metersphere.system.domain.SystemParameter;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/system/parameter")
|
||||
public class SystemParameterController {
|
||||
|
||||
@Resource
|
||||
SystemParameterService systemParameterService;
|
||||
|
||||
|
||||
/**
|
||||
* 基本配置
|
||||
*
|
||||
* @param systemParameter
|
||||
*/
|
||||
@PostMapping("/save/baseInfo")
|
||||
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_UPDATE)
|
||||
@RequestLog(type = OperationLogType.ADD, module = OperationLogModule.SYSTEM_PARAMETER_SETTING)
|
||||
public void saveBaseParameter(@RequestBody List<SystemParameter> systemParameter) {
|
||||
systemParameterService.saveBaseInfo(systemParameter);
|
||||
}
|
||||
|
||||
@GetMapping("/get/baseInfo")
|
||||
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ)
|
||||
public BaseSystemConfigDTO getBaseInfo() {
|
||||
return systemParameterService.getBaseInfo();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 邮件设置
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/get/emailInfo")
|
||||
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ)
|
||||
public EMailInfoDto getEmailInfo() {
|
||||
return systemParameterService.getEmailInfo();
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/edit/emailInfo")
|
||||
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_UPDATE)
|
||||
@RequestLog(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING)
|
||||
public void editEMailInfo(@RequestBody List<SystemParameter> systemParameter) {
|
||||
systemParameterService.editEMailInfo(systemParameter);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 邮件测试连接
|
||||
*
|
||||
* @param hashMap
|
||||
*/
|
||||
@PostMapping("/test/email")
|
||||
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ)
|
||||
public void testEmailConnection(@RequestBody HashMap<String, String> hashMap) {
|
||||
systemParameterService.testEmailConnection(hashMap);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package io.metersphere.system.service;
|
||||
|
||||
|
||||
import io.metersphere.sdk.exception.MSException;
|
||||
import io.metersphere.sdk.util.Translator;
|
||||
import io.metersphere.system.domain.AuthSource;
|
||||
import io.metersphere.system.domain.AuthSourceExample;
|
||||
import io.metersphere.system.mapper.AuthSourceMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public class AuthSourceService {
|
||||
@Resource
|
||||
private AuthSourceMapper authSourceMapper;
|
||||
|
||||
public List<AuthSource> list() {
|
||||
AuthSourceExample example = new AuthSourceExample();
|
||||
return authSourceMapper.selectByExample(example);
|
||||
}
|
||||
|
||||
public void addAuthSource(AuthSource authSource) {
|
||||
checkAuthSource(authSource);
|
||||
long createTime = System.currentTimeMillis();
|
||||
authSource.setCreateTime(createTime);
|
||||
authSource.setUpdateTime(createTime);
|
||||
authSource.setId(UUID.randomUUID().toString());
|
||||
authSourceMapper.insertSelective(authSource);
|
||||
}
|
||||
|
||||
public void checkAuthSource(AuthSource authSource) {
|
||||
String resourcePoolName = authSource.getName();
|
||||
if (StringUtils.isBlank(resourcePoolName)) {
|
||||
throw new MSException(Translator.get("authsource_name_is_null"));
|
||||
}
|
||||
|
||||
AuthSourceExample example = new AuthSourceExample();
|
||||
AuthSourceExample.Criteria criteria = example.createCriteria();
|
||||
criteria.andNameEqualTo(resourcePoolName);
|
||||
if (StringUtils.isNotBlank(authSource.getId())) {
|
||||
criteria.andIdNotEqualTo(authSource.getId());
|
||||
}
|
||||
|
||||
if (authSourceMapper.countByExample(example) > 0) {
|
||||
throw new MSException(Translator.get("authsource_name_already_exists"));
|
||||
}
|
||||
if (StringUtils.isBlank(authSource.getConfiguration().toString())) {
|
||||
throw new MSException(Translator.get("authsource_configuration_is_null"));
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteAuthSource(String id) {
|
||||
authSourceMapper.deleteByPrimaryKey(id);
|
||||
}
|
||||
|
||||
public AuthSource getAuthSource(String id) {
|
||||
return authSourceMapper.selectByPrimaryKey(id);
|
||||
}
|
||||
|
||||
public void updateAuthSource(AuthSource authSource) {
|
||||
checkAuthSource(authSource);
|
||||
authSource.setCreateTime(null);
|
||||
authSource.setUpdateTime(System.currentTimeMillis());
|
||||
authSourceMapper.updateByPrimaryKeySelective(authSource);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,114 @@
|
|||
package io.metersphere.system.controller;
|
||||
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
import io.metersphere.sdk.constants.SessionConstants;
|
||||
import io.metersphere.sdk.util.JSON;
|
||||
import io.metersphere.system.domain.AuthSource;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
public class AuthSourceControllerTest {
|
||||
|
||||
@Resource
|
||||
private MockMvc mockMvc;
|
||||
|
||||
private static String sessionId;
|
||||
private static String csrfToken;
|
||||
|
||||
@BeforeEach
|
||||
public void login() throws Exception {
|
||||
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/login")
|
||||
.content("{\"username\":\"admin\",\"password\":\"metersphere\"}")
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||
.andReturn();
|
||||
sessionId = JsonPath.read(mvcResult.getResponse().getContentAsString(), "$.data.sessionId");
|
||||
csrfToken = JsonPath.read(mvcResult.getResponse().getContentAsString(), "$.data.csrfToken");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
public void testAddSource() throws Exception {
|
||||
AuthSource authSource = new AuthSource();
|
||||
authSource.setId("2b6a83d0-7c66-43ed-a1d9-5132d3167aaf");
|
||||
authSource.setConfiguration("123".getBytes());
|
||||
authSource.setName("测试CAS");
|
||||
authSource.setCreateTime(System.currentTimeMillis());
|
||||
authSource.setUpdateTime(System.currentTimeMillis());
|
||||
mockMvc.perform(MockMvcRequestBuilders.post("/system/authsource/add")
|
||||
.header(SessionConstants.HEADER_TOKEN, sessionId)
|
||||
.header(SessionConstants.CSRF_TOKEN, csrfToken)
|
||||
.content(JSON.toJSONString(authSource))
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||
.andDo(print());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
public void testGetSourceList() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.post("/system/authsource/list/1/10")
|
||||
.header(SessionConstants.HEADER_TOKEN, sessionId)
|
||||
.header(SessionConstants.CSRF_TOKEN, csrfToken))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(print());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
public void testUpdateSource() throws Exception {
|
||||
AuthSource authSource = new AuthSource();
|
||||
authSource.setId("2b6a83d0-7c66-43ed-a1d9-5132d3167aaf");
|
||||
authSource.setConfiguration("123666".getBytes());
|
||||
authSource.setName("更新");
|
||||
authSource.setUpdateTime(System.currentTimeMillis());
|
||||
mockMvc.perform(MockMvcRequestBuilders.post("/system/authsource/update")
|
||||
.header(SessionConstants.HEADER_TOKEN, sessionId)
|
||||
.header(SessionConstants.CSRF_TOKEN, csrfToken)
|
||||
.content(JSON.toJSONString(authSource))
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||
.andDo(print());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
public void testGetSourceById() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/system/authsource/get/2b6a83d0-7c66-43ed-a1d9-5132d3167aaf")
|
||||
.header(SessionConstants.HEADER_TOKEN, sessionId)
|
||||
.header(SessionConstants.CSRF_TOKEN, csrfToken))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(print());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
public void testDelSourceById() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/system/authsource/delete/2b6a83d0-7c66-43ed-a1d9-5132d3167aaf")
|
||||
.header(SessionConstants.HEADER_TOKEN, sessionId)
|
||||
.header(SessionConstants.CSRF_TOKEN, csrfToken))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(print());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,150 @@
|
|||
package io.metersphere.system.controller;
|
||||
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
import io.metersphere.sdk.constants.SessionConstants;
|
||||
import io.metersphere.sdk.util.JSON;
|
||||
import io.metersphere.system.domain.SystemParameter;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
public class SystemParameterControllerTest {
|
||||
|
||||
@Resource
|
||||
private MockMvc mockMvc;
|
||||
|
||||
private static String sessionId;
|
||||
private static String csrfToken;
|
||||
|
||||
@BeforeEach
|
||||
public void login() throws Exception {
|
||||
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/login")
|
||||
.content("{\"username\":\"admin\",\"password\":\"metersphere\"}")
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||
.andReturn();
|
||||
sessionId = JsonPath.read(mvcResult.getResponse().getContentAsString(), "$.data.sessionId");
|
||||
csrfToken = JsonPath.read(mvcResult.getResponse().getContentAsString(), "$.data.csrfToken");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
public void testSaveBaseInfo() throws Exception {
|
||||
|
||||
List<SystemParameter> systemParameters = new ArrayList<>();
|
||||
SystemParameter systemParameter = new SystemParameter();
|
||||
systemParameter.setParamKey("base.url");
|
||||
systemParameter.setParamValue("https://baidu.com");
|
||||
systemParameter.setType("text");
|
||||
SystemParameter parameter = new SystemParameter();
|
||||
parameter.setParamKey("base.prometheus.host");
|
||||
parameter.setParamValue("http://127.0.0.1:1111");
|
||||
parameter.setType("text");
|
||||
systemParameters.add(systemParameter);
|
||||
systemParameters.add(parameter);
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders.post("/system/parameter/save/baseInfo")
|
||||
.header(SessionConstants.HEADER_TOKEN, sessionId)
|
||||
.header(SessionConstants.CSRF_TOKEN, csrfToken)
|
||||
.content(JSON.toJSONString(systemParameters))
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||
.andDo(print());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
public void testGetBaseInfo() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/system/parameter/get/baseInfo")
|
||||
.header(SessionConstants.HEADER_TOKEN, sessionId)
|
||||
.header(SessionConstants.CSRF_TOKEN, csrfToken))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(print());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
public void testGetEmailInfo()throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/system/parameter/get/emailInfo")
|
||||
.header(SessionConstants.HEADER_TOKEN, sessionId)
|
||||
.header(SessionConstants.CSRF_TOKEN, csrfToken))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
public void testEditEmailInfo()throws Exception {
|
||||
|
||||
List<SystemParameter> systemParameters = new ArrayList<>();
|
||||
SystemParameter systemParameter1 = new SystemParameter();
|
||||
systemParameter1.setParamKey("smtp.host");
|
||||
systemParameter1.setParamValue("xxx.xxx.com");
|
||||
systemParameter1.setType("text");
|
||||
|
||||
SystemParameter systemParameter2 = new SystemParameter();
|
||||
systemParameter2.setParamKey("smtp.port");
|
||||
systemParameter2.setParamValue("xxx");
|
||||
systemParameter2.setType("text");
|
||||
|
||||
SystemParameter systemParameter3 = new SystemParameter();
|
||||
systemParameter3.setParamKey("smtp.account");
|
||||
systemParameter3.setParamValue("aaa@qq.com");
|
||||
systemParameter3.setType("text");
|
||||
|
||||
|
||||
systemParameters.add(systemParameter1);
|
||||
systemParameters.add(systemParameter2);
|
||||
systemParameters.add(systemParameter3);
|
||||
|
||||
mockMvc.perform(MockMvcRequestBuilders.post("/system/parameter/edit/emailInfo")
|
||||
.header(SessionConstants.HEADER_TOKEN, sessionId)
|
||||
.header(SessionConstants.CSRF_TOKEN, csrfToken)
|
||||
.content(JSON.toJSONString(systemParameters))
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||
.andDo(print());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
public void testEmailConnec()throws Exception {
|
||||
HashMap<String, String> hashMap = new HashMap<>();
|
||||
hashMap.put("smtp.host","xx");
|
||||
hashMap.put("smtp.port","xx");
|
||||
hashMap.put("smtp.account","xx");
|
||||
hashMap.put("smtp.password","xx");
|
||||
hashMap.put("smtp.from","xx");
|
||||
hashMap.put("smtp.recipient","xx");
|
||||
hashMap.put("smtp.ssl","ture");
|
||||
hashMap.put("smtp.tls","false");
|
||||
mockMvc.perform(MockMvcRequestBuilders.post("/system/parameter/test/email")
|
||||
.header(SessionConstants.HEADER_TOKEN, sessionId)
|
||||
.header(SessionConstants.CSRF_TOKEN, csrfToken)
|
||||
.content(JSON.toJSONString(hashMap))
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().is5xxServerError());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue