diff --git a/backend/framework/plugin/plugin-sdk/src/main/java/io/metersphere/plugin/sdk/spi/QuotaPlugin.java b/backend/framework/plugin/plugin-sdk/src/main/java/io/metersphere/plugin/sdk/spi/QuotaPlugin.java new file mode 100644 index 0000000000..e4715bbce3 --- /dev/null +++ b/backend/framework/plugin/plugin-sdk/src/main/java/io/metersphere/plugin/sdk/spi/QuotaPlugin.java @@ -0,0 +1,7 @@ +package io.metersphere.plugin.sdk.spi; + +import io.metersphere.plugin.sdk.util.MSPluginException; + +public abstract class QuotaPlugin extends AbstractMsPlugin { + public abstract void interceptor(Object pjp) throws MSPluginException; +} diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/PluginScenarioType.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/PluginScenarioType.java index 0edfc5936f..2f01397013 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/PluginScenarioType.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/PluginScenarioType.java @@ -12,5 +12,9 @@ public enum PluginScenarioType { /** * jdbc 驱动插件 */ - JDBC_DRIVER + JDBC_DRIVER, + /** + * 配额控制 + */ + QUOTA } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/config/QuotaInterceptor.java b/backend/services/system-setting/src/main/java/io/metersphere/system/config/QuotaInterceptor.java new file mode 100644 index 0000000000..7f2c95174d --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/config/QuotaInterceptor.java @@ -0,0 +1,32 @@ +package io.metersphere.system.config; + +import io.metersphere.plugin.sdk.spi.QuotaPlugin; +import io.metersphere.system.service.PluginLoadService; +import jakarta.annotation.Resource; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.pf4j.PluginWrapper; +import org.springframework.stereotype.Component; + +@Aspect +@Component +public class QuotaInterceptor { + @Resource + private PluginLoadService pluginLoadService; + // 插件ID + private final String QUOTA = "cloud-quota-plugin"; + + @Around("execution(* io.metersphere..*(..)) && " + + "(@annotation(org.springframework.web.bind.annotation.PostMapping) ||" + + "@annotation(org.springframework.web.bind.annotation.GetMapping)|| " + + "@annotation(org.springframework.web.bind.annotation.RequestMapping))") + public Object interceptor(ProceedingJoinPoint pjp) throws Throwable { + // 验证配额规则 + PluginWrapper pluginWrapper = pluginLoadService.getMsPluginManager().getPlugin(QUOTA); + if (pluginWrapper != null && pluginWrapper.getPlugin() instanceof QuotaPlugin quotaPlugin) { + quotaPlugin.interceptor(pjp); + } + return pjp.proceed(); + } +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java index 08f0924b89..e69d042c91 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java @@ -7,9 +7,9 @@ import io.metersphere.plugin.platform.dto.SelectOption; import io.metersphere.plugin.platform.spi.AbstractPlatformPlugin; import io.metersphere.plugin.platform.spi.Platform; import io.metersphere.plugin.sdk.spi.MsPlugin; +import io.metersphere.plugin.sdk.spi.QuotaPlugin; import io.metersphere.sdk.constants.KafkaTopicConstants; import io.metersphere.sdk.constants.PluginScenarioType; -import io.metersphere.system.dto.sdk.OptionDTO; import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.JSON; @@ -18,10 +18,11 @@ import io.metersphere.system.domain.Plugin; import io.metersphere.system.domain.PluginExample; import io.metersphere.system.dto.PluginDTO; import io.metersphere.system.dto.PluginNotifiedDTO; -import io.metersphere.system.mapper.ExtPluginMapper; -import io.metersphere.system.mapper.PluginMapper; import io.metersphere.system.dto.request.PlatformOptionRequest; import io.metersphere.system.dto.request.PluginUpdateRequest; +import io.metersphere.system.dto.sdk.OptionDTO; +import io.metersphere.system.mapper.ExtPluginMapper; +import io.metersphere.system.mapper.PluginMapper; import io.metersphere.system.utils.ServiceUtils; import jakarta.annotation.Resource; import jakarta.servlet.http.HttpServletResponse; @@ -165,6 +166,8 @@ public class PluginService { plugin.setScenario(PluginScenarioType.API_PROTOCOL.name()); } else if (msPlugin instanceof AbstractPlatformPlugin) { plugin.setScenario(PluginScenarioType.PLATFORM.name()); + }else if(msPlugin instanceof QuotaPlugin){ + plugin.setScenario(PluginScenarioType.QUOTA.name()); } plugin.setXpack(msPlugin.isXpack()); plugin.setPluginId(descriptor.getPluginId() + "-" + descriptor.getVersion()); diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/PluginControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/PluginControllerTests.java index 329b5ed95d..b957add4ca 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/PluginControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/PluginControllerTests.java @@ -161,6 +161,19 @@ public class PluginControllerTests extends BaseTest { getDefaultMultiPartParam(request, myDriver)); Assertions.assertEquals(jdbcDriverPluginService.getJdbcDriverClass(DEFAULT_ORGANIZATION_ID), Arrays.asList("io.jianxing.MyDriver", "com.mysql.cj.jdbc.Driver")); + + // 校验QUOTA动上传成功 + request.setName("cloud-quota-plugin"); + request.setOrganizationIds(Arrays.asList(org.getId())); + File quota = new File( + this.getClass().getClassLoader().getResource("file/cloud-quota-plugin-3.x.jar") + .getPath() + ); + this.requestMultipartWithOkAndReturn(DEFAULT_ADD, + getDefaultMultiPartParam(request, quota)); + // 清理掉 + this.requestGetWithOk(DEFAULT_DELETE, "cloud-quota-plugin"); + // @@重名校验异常 // 校验插件名称重名 assertErrorCode(this.requestMultipart(DEFAULT_ADD, diff --git a/backend/services/system-setting/src/test/resources/file/cloud-quota-plugin-3.x.jar b/backend/services/system-setting/src/test/resources/file/cloud-quota-plugin-3.x.jar new file mode 100644 index 0000000000..f433ca25cb Binary files /dev/null and b/backend/services/system-setting/src/test/resources/file/cloud-quota-plugin-3.x.jar differ