Browse Source

LdapContext test

MaxKey 2 years ago
parent
commit
9404fa27f1

+ 7 - 0
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";

+ 14 - 0
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();
+    });
+  }
 }

+ 15 - 4
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<T> {
       add: string;
       update: string;
       delete: string;
+      test: string;
       tree: string;
       member: string;
       memberOut: string;
@@ -49,6 +49,7 @@ export class BaseService<T> {
         load: '/load',
         update: '/update',
         delete: '/delete',
+        test: '/test',
         tree: '/tree',
         member: '/member',
         memberOut: '/memberOut'
@@ -108,6 +109,16 @@ export class BaseService<T> {
     return this.http.delete<Message<T>>(`${this.server.urls.base + this.server.urls.delete}?ids=${ids}`);
   }
 
+  test(params: NzSafeAny, testURL?: string): Observable<Message<T>> {
+    let _testURL = '';
+    if (testURL) {
+      _testURL = testURL;
+    } else {
+      _testURL = `${this.server.urls.base + this.server.urls.test}`;
+    }
+    return this.http.get<Message<T>>(_testURL, { params: this.parseParams(params) });
+  }
+
   tree(params: NzSafeAny): Observable<Message<any>> {
     return this.http.get<Message<any>>(this.server.urls.base + this.server.urls.tree, {
       params: this.parseParams(params)

+ 19 - 1
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",

+ 19 - 1
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": "同步",

+ 19 - 1
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": "同步",

+ 40 - 0
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<LdapContext>(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<LdapContext>(Message.SUCCESS).buildResponse();
+		}else {
+			return new Message<LdapContext>(Message.FAIL).buildResponse();
+		}
+	}
 }