Browse Source

feat: LDAP用户新增和修改

tuonian 4 months ago
parent
commit
0bb1e64504

+ 1 - 0
frontend/src/components/curd/index.tsx

@@ -132,6 +132,7 @@ export function CurdPage<T extends ICurdData>({
     const onSaveSuccess = (data: Partial<T>) => {
         setQuery({...query})
         onSuccess?.(data)
+        setVisible(false)
     }
 
     return <div className="curd">

+ 1 - 1
frontend/src/pages/ldap/server/index.tsx

@@ -180,7 +180,7 @@ export const Server = () => {
                 </Form.Item>
                 <Form.Item label="搜索结果">
                     <div className="search-results">
-                        <Table dataSource={searchUsers} pagination={false}>
+                        <Table dataSource={searchUsers} pagination={false} rowKey="uid">
                             <Table.Column dataIndex="account" title="uid"/>
                             <Table.Column dataIndex="userName" title="姓名"/>
                             <Table.Column dataIndex="dn" title="DN"/>

+ 4 - 3
frontend/src/pages/ldap/user/list/index.tsx

@@ -7,7 +7,7 @@ import './index.less'
 import {Link} from "react-router-dom";
 import {useRouteLoaderData} from "react-router";
 import {SyncOutlined} from "@ant-design/icons";
-import {isNull, isNullOrTrue, Message} from "auto-antd";
+import {isNull, Message} from "auto-antd";
 import {UserAttributesText} from "../components/UserAttributes.tsx";
 import {safeParse} from "../../../../utils/json.ts";
 
@@ -79,6 +79,7 @@ const columns: CurdColumn[] = [
         type: 'string',
         placeholder: 'eg. ou=users,dc=tonyandmoney,dc=cn',
         width: 150,
+        hidden: true,
     },
     {
         key: 'mail',
@@ -152,12 +153,12 @@ const parseUser = (user: LDAP.User) => {
 const stringifyUser = (user: Partial<LDAP.User>) => {
     const attributes = []
     for (const attr of (user.attributes as any[])) {
-        if (!attr.Name){
+        if (!attr.Name || isNull(attr.Value)){
             continue
         }
         attributes.push({
             Name: attr.Name,
-            Values: isNullOrTrue(attr.Value) ? [attr.Values] : []
+            Values: !isNull(attr.Value) ? [attr.Value] : []
         })
     }
     for (const k of toObjKeys){

+ 14 - 2
server/modules/ldap/client.go

@@ -62,6 +62,15 @@ func GetActiveClient(server *models.LdapServer) (*Client, error) {
 	return client, nil
 }
 
+func CloseActiveClient(server *models.LdapServer) {
+	delete(ActiveClients, server.Key)
+	client := ActiveClients[server.Key]
+	if client == nil {
+		return
+	}
+	client.Close()
+}
+
 func (c *Client) Close() {
 	if c.Connected && c.Conn != nil {
 		c.Conn.Close()
@@ -127,7 +136,7 @@ func (c *Client) SearchByAccount(account string) (*ldap.Entry, error) {
 		logs.Error("search fail : %v", err)
 		return nil, err
 	}
-	if len(sr.Entries) != 1 {
+	if len(sr.Entries) < 1 {
 		logs.Error("no account found for: %v", account)
 	}
 	return sr.Entries[0], nil
@@ -173,7 +182,10 @@ func (c *Client) Modify() error {
 // 搜索指定账号:(&(objectClass=*)(uid=%s))
 func (c *Client) Add(user *models.LdapUser) error {
 	entries, err := c.Search(fmt.Sprintf("(&(objectClass=*)(uid=%s))", user.Account))
-	if err == nil {
+	if err != nil {
+		return err
+	}
+	if len(entries) > 0 {
 		entry := entries[0]
 		if entry.DN != user.DN {
 			logs.Warn("DN not match: {}, {}", entry.DN, user.DN)

+ 2 - 1
server/modules/ldap/service.go

@@ -85,7 +85,7 @@ func (c *Service) GetServers(current *models.User, req *vo.PageReq) (*vo.PageRes
 // Update 保存或者修改
 // post /ldap/server
 func (c *Service) Update(current *models.User, body *models.LdapServer) (*models.LdapServer, error) {
-
+	body.Uid = string(rune(current.Id))
 	if body.Url == "" {
 		return nil, errors.New("请完成服务配置,缺少Url!")
 	}
@@ -124,6 +124,7 @@ func (c *Service) Update(current *models.User, body *models.LdapServer) (*models
 		}
 		body.Id = int(id)
 	}
+	CloseActiveClient(body)
 	return body, nil
 }
 

+ 1 - 1
server/modules/ldap/user_service.go

@@ -82,7 +82,7 @@ func (c *UserService) Search(server *models.LdapServer, filter string) ([]*model
 		var isOrganize = false
 		objectClass := entry.GetAttributeValues("objectClass")
 		for _, oc := range objectClass {
-			if oc == server.OrganizeClass {
+			if oc == server.OrganizeClass || oc == "organization" {
 				isOrganize = true
 				break
 			}

+ 9 - 0
server/modules/ldap/utils.go

@@ -5,6 +5,7 @@ import (
 	"github.com/astaxie/beego/logs"
 	"github.com/go-ldap/ldap/v3"
 	"nginx-ui/server/models"
+	"strings"
 	"time"
 )
 
@@ -14,6 +15,14 @@ func modifyLDAPUser(user *models.LdapUser, entry *ldap.Entry) {
 	user.UserName = entry.GetAttributeValue("cn")
 	user.Mail = entry.GetAttributeValue("mail")
 	user.DN = entry.DN
+	var organizeList []string
+	items := strings.Split(user.DN, ",")
+	for _, item := range items {
+		if !strings.HasPrefix(item, "cn=") {
+			organizeList = append(organizeList, item)
+		}
+	}
+	user.Organize = strings.Join(organizeList, ",")
 }
 
 func createUser(entry *ldap.Entry) models.LdapUser {