From 9404fa27f14a549d9f22e7d5e4250b7d51a687e2 Mon Sep 17 00:00:00 2001 From: MaxKey Date: Sat, 8 Oct 2022 09:57:48 +0800 Subject: [PATCH] LdapContext test --- .../maxkey/persistence/ldap/LdapUtils.java | 7 ++++ .../ldap-context/ldap-context.component.ts | 14 +++++++ .../src/app/service/base.service.ts | 19 +++++++-- .../src/assets/i18n/en-US.json | 20 +++++++++- .../src/assets/i18n/zh-CN.json | 20 +++++++++- .../src/assets/i18n/zh-TW.json | 20 +++++++++- .../contorller/LdapContextController.java | 40 +++++++++++++++++++ 7 files changed, 133 insertions(+), 7 deletions(-) diff --git a/maxkey-core/src/main/java/org/maxkey/persistence/ldap/LdapUtils.java b/maxkey-core/src/main/java/org/maxkey/persistence/ldap/LdapUtils.java index 383388867..b9a733069 100644 --- a/maxkey-core/src/main/java/org/maxkey/persistence/ldap/LdapUtils.java +++ b/maxkey-core/src/main/java/org/maxkey/persistence/ldap/LdapUtils.java @@ -38,6 +38,13 @@ import org.slf4j.LoggerFactory; public class LdapUtils { private static final Logger _logger = LoggerFactory.getLogger(LdapUtils.class); + public class Product{ + public static final String ActiveDirectory = "ActiveDirectory"; + public static final String OpenLDAP = "OpenLDAP"; + public static final String StandardLDAP = "StandardLDAP"; + } + + public static final String propertyBaseDN = "baseDN"; public static final String propertyDomain = "domain"; public static final String propertyTrustStore = "trustStore"; diff --git a/maxkey-web-frontend/maxkey-web-mgt-app/src/app/routes/config/ldap-context/ldap-context.component.ts b/maxkey-web-frontend/maxkey-web-mgt-app/src/app/routes/config/ldap-context/ldap-context.component.ts index c73ac67c8..d5bfa7e7b 100644 --- a/maxkey-web-frontend/maxkey-web-mgt-app/src/app/routes/config/ldap-context/ldap-context.component.ts +++ b/maxkey-web-frontend/maxkey-web-mgt-app/src/app/routes/config/ldap-context/ldap-context.component.ts @@ -78,4 +78,18 @@ export class LdapContextComponent implements OnInit { this.cdr.detectChanges(); }); } + + onTest(e: MouseEvent): void { + e.preventDefault(); + this.form.submitting = true; + this.ldapContextService.test(this.form.model).subscribe(res => { + if (res.code == 0) { + this.msg.success(this.i18n.fanyi('mxk.alert.test.success')); + } else { + this.msg.error(this.i18n.fanyi('mxk.alert.test.error')); + } + this.form.submitting = false; + this.cdr.detectChanges(); + }); + } } diff --git a/maxkey-web-frontend/maxkey-web-mgt-app/src/app/service/base.service.ts b/maxkey-web-frontend/maxkey-web-mgt-app/src/app/service/base.service.ts index 6d163d681..89c174585 100644 --- a/maxkey-web-frontend/maxkey-web-mgt-app/src/app/service/base.service.ts +++ b/maxkey-web-frontend/maxkey-web-mgt-app/src/app/service/base.service.ts @@ -1,19 +1,18 @@ /* * Copyright [2022] [MaxKey of copyright http://www.maxkey.top] - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ - import { HttpClient, HttpParams } from '@angular/common/http'; import { NzSafeAny } from 'ng-zorro-antd/core/types'; @@ -36,6 +35,7 @@ export class BaseService { add: string; update: string; delete: string; + test: string; tree: string; member: string; memberOut: string; @@ -49,6 +49,7 @@ export class BaseService { load: '/load', update: '/update', delete: '/delete', + test: '/test', tree: '/tree', member: '/member', memberOut: '/memberOut' @@ -108,6 +109,16 @@ export class BaseService { return this.http.delete>(`${this.server.urls.base + this.server.urls.delete}?ids=${ids}`); } + test(params: NzSafeAny, testURL?: string): Observable> { + let _testURL = ''; + if (testURL) { + _testURL = testURL; + } else { + _testURL = `${this.server.urls.base + this.server.urls.test}`; + } + return this.http.get>(_testURL, { params: this.parseParams(params) }); + } + tree(params: NzSafeAny): Observable> { return this.http.get>(this.server.urls.base + this.server.urls.tree, { params: this.parseParams(params) diff --git a/maxkey-web-frontend/maxkey-web-mgt-app/src/assets/i18n/en-US.json b/maxkey-web-frontend/maxkey-web-mgt-app/src/assets/i18n/en-US.json index 4e966a709..f0a165f64 100644 --- a/maxkey-web-frontend/maxkey-web-mgt-app/src/assets/i18n/en-US.json +++ b/maxkey-web-frontend/maxkey-web-mgt-app/src/assets/i18n/en-US.json @@ -455,7 +455,20 @@ }, "roles": { "name": "Role Name", - "dynamic": "Dynamic", + "type": { + "":"Member Type", + "user":"User", + "user-dynamic":"User-Dynamic", + "post":"Post" + }, + "member": "Member", + "permissions": "Permissions", + "category": { + "":"Category", + "dynamic":"Dynamic", + "static":"Static", + "app":"App" + }, "filters": "Attributes Filter", "orgIdsList": "Dept List", "resumeTime": "Join Time", @@ -643,6 +656,10 @@ "success":"Delete Success!", "error":"Delete Error!" }, + "test":{ + "success":"Test Success!", + "error":"Test Error!" + }, "operate":{ "success":"Operate Success!", "error":"Operate Error!" @@ -662,6 +679,7 @@ "add": "Add", "edit": "Edit", "delete": "Delete", + "test": "Test", "select": "Select", "confirm": "Confirm", "synchr": "Synchr", diff --git a/maxkey-web-frontend/maxkey-web-mgt-app/src/assets/i18n/zh-CN.json b/maxkey-web-frontend/maxkey-web-mgt-app/src/assets/i18n/zh-CN.json index f53afe64f..bbf63bba0 100644 --- a/maxkey-web-frontend/maxkey-web-mgt-app/src/assets/i18n/zh-CN.json +++ b/maxkey-web-frontend/maxkey-web-mgt-app/src/assets/i18n/zh-CN.json @@ -453,7 +453,20 @@ }, "roles": { "name": "角色名称", - "dynamic": "动态角色", + "type": { + "":"成员类型", + "user":"用户", + "user-dynamic":"用户-动态", + "post":"岗位" + }, + "member": "成员", + "permissions": "访问权限", + "category": { + "":"类型", + "dynamic":"动态", + "static":"静态", + "app":"应用" + }, "filters": "属性过滤器", "orgIdsList": "部门列表", "resumeTime": "加入时间", @@ -637,6 +650,10 @@ "success":"更新成功!", "error":"更新失败!" }, + "test":{ + "success":"测试成功!", + "error":"测试失败!" + }, "delete":{ "success":"删除成功!", "error":"删除失败!" @@ -659,6 +676,7 @@ "add": "新增", "edit": "编辑", "delete": "删除", + "test": "测试", "select": "选择", "confirm": "确定", "synchr": "同步", diff --git a/maxkey-web-frontend/maxkey-web-mgt-app/src/assets/i18n/zh-TW.json b/maxkey-web-frontend/maxkey-web-mgt-app/src/assets/i18n/zh-TW.json index bbef89fa6..05eed5fb0 100644 --- a/maxkey-web-frontend/maxkey-web-mgt-app/src/assets/i18n/zh-TW.json +++ b/maxkey-web-frontend/maxkey-web-mgt-app/src/assets/i18n/zh-TW.json @@ -454,7 +454,20 @@ }, "roles": { "name": "角色名稱", - "dynamic": "動態角色", + "type": { + "":"成員類型", + "user":"用戶", + "user-dynamic":"用戶-動態", + "post":"崗位" + }, + "member": "成員", + "permissions": "存取控制", + "category": { + "":"類型", + "dynamic":"動態", + "static":"靜態", + "app":"應用" + }, "filters": "屬性過濾器", "orgIdsList": "部門列表", "resumeTime": "加入時間", @@ -642,6 +655,10 @@ "success":"删除成功!", "error":"删除失敗!" }, + "test":{ + "success":"測試成功!", + "error":"測試失敗!" + }, "operate":{ "success":"操作成功!", "error":"操作失敗!" @@ -660,6 +677,7 @@ "add": "新增", "edit": "編輯", "delete": "刪除", + "test": "測試", "select": "選擇", "confirm": "確定", "synchr": "同步", diff --git a/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/config/contorller/LdapContextController.java b/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/config/contorller/LdapContextController.java index ab409c560..b1230b456 100644 --- a/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/config/contorller/LdapContextController.java +++ b/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/config/contorller/LdapContextController.java @@ -23,6 +23,8 @@ import org.maxkey.crypto.password.PasswordReciprocal; import org.maxkey.entity.LdapContext; import org.maxkey.entity.Message; import org.maxkey.entity.UserInfo; +import org.maxkey.persistence.ldap.ActiveDirectoryUtils; +import org.maxkey.persistence.ldap.LdapUtils; import org.maxkey.persistence.service.LdapContextService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -71,4 +73,42 @@ public class LdapContextController { return new Message(Message.FAIL).buildResponse(); } } + + + @RequestMapping(value={"/test"}, produces = {MediaType.APPLICATION_JSON_VALUE}) + public ResponseEntity test(@CurrentUser UserInfo currentUser){ + LdapContext ldapContext = ldapContextService.get(currentUser.getInstId()); + if(ldapContext != null && StringUtils.isNoneBlank(ldapContext.getCredentials())) { + ldapContext.setCredentials(PasswordReciprocal.getInstance().decoder(ldapContext.getCredentials())); + } + + LdapUtils ldapUtils = null; + if(ldapContext.getProduct().equalsIgnoreCase(LdapUtils.Product.ActiveDirectory)) { + ldapUtils = new ActiveDirectoryUtils( + ldapContext.getProviderUrl(), + ldapContext.getPrincipal(), + ldapContext.getCredentials(), + ldapContext.getBasedn(), + ldapContext.getMsadDomain()); + }else if(ldapContext.getProduct().equalsIgnoreCase(LdapUtils.Product.OpenLDAP)) { + ldapUtils = new LdapUtils( + ldapContext.getProviderUrl(), + ldapContext.getPrincipal(), + ldapContext.getCredentials(), + ldapContext.getBasedn()); + }else if(ldapContext.getProduct().equalsIgnoreCase(LdapUtils.Product.StandardLDAP)) { + ldapUtils = new LdapUtils( + ldapContext.getProviderUrl(), + ldapContext.getPrincipal(), + ldapContext.getCredentials(), + ldapContext.getBasedn()); + } + + if(ldapUtils.openConnection() != null) { + ldapUtils.close(); + return new Message(Message.SUCCESS).buildResponse(); + }else { + return new Message(Message.FAIL).buildResponse(); + } + } }