From 82db686b05c222cee8ecb6b2d344aead261c20a4 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 24 Nov 2023 10:52:54 +0800 Subject: [PATCH] fix --- .../logic/createldaporganizationlogic.go | 2 +- .../logic/getldaporganizationslogic.go | 36 ++++++++++--------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/server/ldap-admin/internal/logic/createldaporganizationlogic.go b/server/ldap-admin/internal/logic/createldaporganizationlogic.go index c797bd09..c662e47f 100644 --- a/server/ldap-admin/internal/logic/createldaporganizationlogic.go +++ b/server/ldap-admin/internal/logic/createldaporganizationlogic.go @@ -48,7 +48,7 @@ func (l *CreateLdapOrganizationLogic) CreateLdapOrganization(req *types.CreateLd return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,组织英文名不能包含中文") } if req.ParentOrganizationDN == "" { - return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,父级DN不能为空") + req.ParentOrganizationDN = l.svcCtx.Config.Ldap.BaseDN //不传则是第一层级 } if req.BusinessCategory == "" { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,组织分类名不能为空") diff --git a/server/ldap-admin/internal/logic/getldaporganizationslogic.go b/server/ldap-admin/internal/logic/getldaporganizationslogic.go index 24aef6d6..1706a018 100644 --- a/server/ldap-admin/internal/logic/getldaporganizationslogic.go +++ b/server/ldap-admin/internal/logic/getldaporganizationslogic.go @@ -33,12 +33,13 @@ func NewGetLdapOrganizationsLogic(ctx context.Context, svcCtx *svc.ServiceContex // func (l *GetLdapOrganizationsLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { // } type DNItem struct { - Attribute map[string]interface{} `json:"attribute"` - HasMember bool `json:"has_member"` //是否有成员 - DN string `json:"dn"` - ParentDN string `json:"parent_dn"` - Sort int `json:"sort"` - Child []*DNItem `json:"child"` + Attribute map[string]interface{} `json:"attribute"` + MemberCount int `json:"member_count"` + Level int `json:"level"` + DN string `json:"dn"` + ParentDN string `json:"parent_dn"` + Sort int `json:"sort"` + Child []*DNItem `json:"child"` } func (l *GetLdapOrganizationsLogic) GetLdapOrganizations(req *types.Request, r *http.Request) (resp *basic.Response) { @@ -55,7 +56,7 @@ func (l *GetLdapOrganizationsLogic) GetLdapOrganizations(req *types.Request, r * if len(peopleDNSlice) <= 1 { return resp.SetStatusWithMessage(basic.CodeServiceErr, "基础用户组的DN未配置") } - filter := "(|(&(objectClass=groupOfUniqueNames)(objectClass=top))(objectClass=organization))" + filter := "(&(objectClass=groupOfUniqueNames)(objectClass=top))" fields := []string{"businessCategory", "dn", "uniqueMember"} searchResult, err := l.svcCtx.Ldap.Search(l.svcCtx.Config.Ldap.BaseDN, ldap.ScopeWholeSubtree, filter, fields, nil) if err != nil { @@ -67,22 +68,25 @@ func (l *GetLdapOrganizationsLogic) GetLdapOrganizations(req *types.Request, r * for _, v := range searchResult.Entries { sortNum++ attribute := make(map[string]interface{}) - hasMember := false + memberCount := 0 for _, attr := range v.Attributes { //判断是否有成员(不包含root用户所以判断大于1) - if attr.Name == "uniqueMember" && len(attr.Values) > 1 { - hasMember = true + if attr.Name == "uniqueMember" { + memberCount = len(attr.Values) - 1 //不要包含原始用户 continue } attribute[attr.Name] = strings.Join(attr.Values, ",") } + dnSlice := strings.ReplaceAll(v.DN, ","+l.svcCtx.Config.Ldap.BaseDN, "") //把最顶级的组织去掉 + level := len(strings.Split(dnSlice, ",")) mapDN[v.DN] = &DNItem{ - DN: v.DN, - ParentDN: "", - HasMember: hasMember, - Attribute: attribute, - Sort: sortNum, - Child: make([]*DNItem, 0, 100), + DN: v.DN, + ParentDN: "", + Level: level, + MemberCount: memberCount, + Attribute: attribute, + Sort: sortNum, + Child: make([]*DNItem, 0, 100), } } //组织树形层级关系