From 866e6e386d05301cd6359501615f68ac3d453832 Mon Sep 17 00:00:00 2001 From: qq213539 <213539@qq.com> Date: Tue, 10 Dec 2019 21:17:50 +0800 Subject: [PATCH 1/7] =?UTF-8?q?#2145=20=E8=87=AA=E5=AE=9A=E4=B9=89namespac?= =?UTF-8?q?eId=20=E5=89=8D=E7=AB=AF=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/NewNameSpace/NewNameSpace.js | 74 ++++++++++++++++--- .../static/console-fe/src/locales/en-US.js | 5 ++ .../static/console-fe/src/locales/zh-CN.js | 4 + 3 files changed, 74 insertions(+), 9 deletions(-) diff --git a/console/src/main/resources/static/console-fe/src/components/NewNameSpace/NewNameSpace.js b/console/src/main/resources/static/console-fe/src/components/NewNameSpace/NewNameSpace.js index 88c6c9b9bfb..bf5f7850746 100644 --- a/console/src/main/resources/static/console-fe/src/components/NewNameSpace/NewNameSpace.js +++ b/console/src/main/resources/static/console-fe/src/components/NewNameSpace/NewNameSpace.js @@ -109,14 +109,14 @@ class NewNameSpace extends React.Component { this.setState({ disabled: true, }); + request({ - type: 'post', - url: 'v1/console/namespaces', + type: 'get', + url: 'v1/console/namespaces?checkNamespaceIdExist=true', contentType: 'application/x-www-form-urlencoded', beforeSend: () => this.openLoading(), data: { - namespaceName: values.namespaceShowName, - namespaceDesc: values.namespaceDesc, + customNamespaceId: values.customNamespaceId, }, success: res => { this.disabled = false; @@ -124,13 +124,38 @@ class NewNameSpace extends React.Component { disabled: false, }); if (res === true) { - this.closeDialog(); - this.props.getNameSpaces(); - this.refreshNameSpace(); // 刷新全局namespace - } else { Dialog.alert({ title: locale.notice, - content: res.message, + content: locale.namespaceIdAlreadyExist, + }); + } else { + request({ + type: 'post', + url: 'v1/console/namespaces', + contentType: 'application/x-www-form-urlencoded', + beforeSend: () => this.openLoading(), + data: { + customNamespaceId: values.customNamespaceId, + namespaceName: values.namespaceShowName, + namespaceDesc: values.namespaceDesc, + }, + success: res => { + this.disabled = false; + this.setState({ + disabled: false, + }); + if (res === true) { + this.closeDialog(); + this.props.getNameSpaces(); + this.refreshNameSpace(); // 刷新全局namespace + } else { + Dialog.alert({ + title: locale.notice, + content: locale.newnamespceFailedMessage, + }); + } + }, + complete: () => this.closeLoading(), }); } }, @@ -164,6 +189,29 @@ class NewNameSpace extends React.Component { } } + validateNamespzecId(rule, value, callback) { + value = value.trim(); + if (value === '') { + callback(); + } else { + const { locale = {} } = this.props; + if (value.length > 128) { + callback(locale.namespaceIdTooLong); + } + const chartReg = /^[\w-]+/g; + const matchResult = value.match(chartReg); + if (matchResult.length > 1) { + callback(locale.input); + } else { + if (value.length !== matchResult[0].length) { + callback(locale.input); + return; + } + callback(); + } + } + } + render() { const { locale = {} } = this.props; const footer = ( @@ -193,6 +241,14 @@ class NewNameSpace extends React.Component { style={{ width: '100%', position: 'relative' }} visible={this.state.loading} > + + + Date: Tue, 10 Dec 2019 21:18:56 +0800 Subject: [PATCH 2/7] =?UTF-8?q?#2145=20=E8=87=AA=E5=AE=9A=E4=B9=89namespac?= =?UTF-8?q?eId=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 4b2d76539e2..49614fefbcf 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 void insertTenantInfoAtomic(String kp, String tenantId, String tenantName } } + /** + * @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 12720d87ee2..93ffd4eb46b 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 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 NamespaceAllInfo getNamespace(HttpServletRequest request, HttpServletResp */ @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 * From 1d42cde1b5a125c8d4218e25e060bf98c894447b Mon Sep 17 00:00:00 2001 From: qq213539 <213539@qq.com> Date: Tue, 10 Dec 2019 21:38:17 +0800 Subject: [PATCH 3/7] =?UTF-8?q?#2145=20=E5=AE=8C=E5=96=84=E5=89=8D?= =?UTF-8?q?=E7=AB=AF=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/NewNameSpace/NewNameSpace.js | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/console/src/main/resources/static/console-fe/src/components/NewNameSpace/NewNameSpace.js b/console/src/main/resources/static/console-fe/src/components/NewNameSpace/NewNameSpace.js index bf5f7850746..bdbb897582d 100644 --- a/console/src/main/resources/static/console-fe/src/components/NewNameSpace/NewNameSpace.js +++ b/console/src/main/resources/static/console-fe/src/components/NewNameSpace/NewNameSpace.js @@ -109,14 +109,17 @@ class NewNameSpace extends React.Component { this.setState({ disabled: true, }); - + let { customNamespaceId } = values; + if (!customNamespaceId) { + customNamespaceId = ''; + } request({ type: 'get', url: 'v1/console/namespaces?checkNamespaceIdExist=true', contentType: 'application/x-www-form-urlencoded', beforeSend: () => this.openLoading(), data: { - customNamespaceId: values.customNamespaceId, + customNamespaceId, }, success: res => { this.disabled = false; @@ -135,7 +138,7 @@ class NewNameSpace extends React.Component { contentType: 'application/x-www-form-urlencoded', beforeSend: () => this.openLoading(), data: { - customNamespaceId: values.customNamespaceId, + customNamespaceId, namespaceName: values.namespaceShowName, namespaceDesc: values.namespaceDesc, }, @@ -190,8 +193,7 @@ class NewNameSpace extends React.Component { } validateNamespzecId(rule, value, callback) { - value = value.trim(); - if (value === '') { + if (!value || value.trim() === '') { callback(); } else { const { locale = {} } = this.props; @@ -200,14 +202,18 @@ class NewNameSpace extends React.Component { } const chartReg = /^[\w-]+/g; const matchResult = value.match(chartReg); - if (matchResult.length > 1) { - callback(locale.input); - } else { - if (value.length !== matchResult[0].length) { + if (matchResult) { + if (matchResult.length > 1) { callback(locale.input); - return; + } else { + if (value.length !== matchResult[0].length) { + callback(locale.input); + return; + } + callback(); } - callback(); + } else { + callback(locale.input); } } } From ed43d5c8296da5838bbc9254e2010e75d26f0e3f Mon Sep 17 00:00:00 2001 From: qq213539 <213539@qq.com> Date: Tue, 10 Dec 2019 21:39:24 +0800 Subject: [PATCH 4/7] =?UTF-8?q?#2145=20namespace=20id=20=E4=B8=BA=E7=A9=BA?= =?UTF-8?q?=E5=85=81=E8=AE=B8=E6=96=B0=E5=A2=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/nacos/console/controller/NamespaceController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 93ffd4eb46b..b8a6efc3d65 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 @@ -131,7 +131,7 @@ public Boolean createNamespace(HttpServletRequest request, HttpServletResponse r @GetMapping(params = "checkNamespaceIdExist=true") public Boolean checkNamespaceIdExist(@RequestParam("customNamespaceId") String namespaceId){ if(StringUtils.isBlank(namespaceId)){ - return true; + return false; } return (persistService.countByTenantId(namespaceId) > 0); } From 374b35200d83fab00abfbab497920f29ae5219fb Mon Sep 17 00:00:00 2001 From: qq213539 <213539@qq.com> Date: Tue, 10 Dec 2019 21:42:32 +0800 Subject: [PATCH 5/7] =?UTF-8?q?#2145=20=E5=A2=9E=E5=8A=A0=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nacos/console/controller/NamespaceController.java | 7 +++++++ 1 file changed, 7 insertions(+) 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 b8a6efc3d65..f9583341533 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 @@ -128,6 +128,13 @@ public Boolean createNamespace(HttpServletRequest request, HttpServletResponse r return true; } + /** + * @author klw(213539@qq.com) + * @Description: check namespaceId exist + * @Date 2019/12/10 21:41 + * @param: namespaceId + * @return java.lang.Boolean + */ @GetMapping(params = "checkNamespaceIdExist=true") public Boolean checkNamespaceIdExist(@RequestParam("customNamespaceId") String namespaceId){ if(StringUtils.isBlank(namespaceId)){ From 592385b43e2c6bf178523b3732170abf48282873 Mon Sep 17 00:00:00 2001 From: qq213539 <213539@qq.com> Date: Tue, 10 Dec 2019 22:19:05 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E9=AD=94=E6=B3=95?= =?UTF-8?q?=E5=80=BC=E9=97=AE=E9=A2=98...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/nacos/console/controller/NamespaceController.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 f9583341533..3e923deb368 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 @@ -45,6 +45,8 @@ public class NamespaceController { private Pattern namespaceIdCheckPattern = Pattern.compile("^[\\w-]+"); + private static final int NAMESPACE_ID_MAX_LENGTH = 128; + /** * Get namespace list * @@ -116,7 +118,7 @@ public Boolean createNamespace(HttpServletRequest request, HttpServletResponse r if (!namespaceIdCheckPattern.matcher(namespaceId).matches()) { return false; } - if (namespaceId.length() > 128) { + if (namespaceId.length() > NAMESPACE_ID_MAX_LENGTH) { return false; } if(persistService.countByTenantId(namespaceId) > 0){ From b47cb1e3811e1ea0878b9b6ab5d3d254676406cf Mon Sep 17 00:00:00 2001 From: qq213539 <213539@qq.com> Date: Thu, 12 Dec 2019 09:26:04 +0800 Subject: [PATCH 7/7] =?UTF-8?q?sql=E6=B3=A8=E5=85=A5=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/nacos/config/server/service/PersistService.java | 4 +++- 1 file changed, 3 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 49614fefbcf..8d3aa5d9a76 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 @@ -3181,7 +3181,9 @@ public void insertTenantInfoAtomic(String kp, String tenantId, String tenantName * @return int */ public int countByTenantId(String tenantId){ - return jt.queryForObject("select count(1) from tenant_info where tenant_id = '" + tenantId + "'", Integer.class); + String[] args = new String[1]; + args[0] = tenantId; + return jt.queryForObject("select count(1) from tenant_info where tenant_id = ?", args, Integer.class); } /**