From 6703b2abbbb8cdf15d3adfb6727982fc3a8f4c1e Mon Sep 17 00:00:00 2001 From: qq213539 <213539@qq.com> Date: Tue, 10 Dec 2019 21:18:56 +0800 Subject: [PATCH] =?UTF-8?q?#2145=20=E8=87=AA=E5=AE=9A=E4=B9=89namespaceId?= =?UTF-8?q?=20=E6=9C=8D=E5=8A=A1=E7=AB=AF=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/server/service/PersistService.java | 11 ++++++++ .../controller/NamespaceController.java | 27 ++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/PersistService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/PersistService.java index 4b2d76539..49614fefb 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/PersistService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/PersistService.java @@ -3173,6 +3173,17 @@ public class PersistService { } } + /** + * @author klw(213539@qq.com) + * @Description: count tenant_info by tenant_id + * @Date 2019/12/10 17:36 + * @param: tenantId + * @return int + */ + public int countByTenantId(String tenantId){ + return jt.queryForObject("select count(1) from tenant_info where tenant_id = '" + tenantId + "'", Integer.class); + } + /** * Update tenantInfo showname * diff --git a/console/src/main/java/com/alibaba/nacos/console/controller/NamespaceController.java b/console/src/main/java/com/alibaba/nacos/console/controller/NamespaceController.java index 12720d87e..93ffd4eb4 100644 --- a/console/src/main/java/com/alibaba/nacos/console/controller/NamespaceController.java +++ b/console/src/main/java/com/alibaba/nacos/console/controller/NamespaceController.java @@ -29,6 +29,7 @@ import javax.servlet.http.HttpServletResponse; import java.util.ArrayList; import java.util.List; import java.util.UUID; +import java.util.regex.Pattern; /** * namespace service @@ -42,6 +43,8 @@ public class NamespaceController { @Autowired private PersistService persistService; + private Pattern namespaceIdCheckPattern = Pattern.compile("^[\\w-]+"); + /** * Get namespace list * @@ -102,15 +105,37 @@ public class NamespaceController { */ @PostMapping public Boolean createNamespace(HttpServletRequest request, HttpServletResponse response, + @RequestParam("customNamespaceId") String namespaceId, @RequestParam("namespaceName") String namespaceName, @RequestParam(value = "namespaceDesc", required = false) String namespaceDesc) { // TODO 获取用kp - String namespaceId = UUID.randomUUID().toString(); + if(StringUtils.isBlank(namespaceId)){ + namespaceId = UUID.randomUUID().toString(); + } else { + namespaceId = namespaceId.trim(); + if (!namespaceIdCheckPattern.matcher(namespaceId).matches()) { + return false; + } + if (namespaceId.length() > 128) { + return false; + } + if(persistService.countByTenantId(namespaceId) > 0){ + return false; + } + } persistService.insertTenantInfoAtomic("1", namespaceId, namespaceName, namespaceDesc, "nacos", System.currentTimeMillis()); return true; } + @GetMapping(params = "checkNamespaceIdExist=true") + public Boolean checkNamespaceIdExist(@RequestParam("customNamespaceId") String namespaceId){ + if(StringUtils.isBlank(namespaceId)){ + return true; + } + return (persistService.countByTenantId(namespaceId) > 0); + } + /** * edit namespace *