From 8c45de4d258fb162e17fea0e008d32e8303ce65e Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 24 Nov 2023 11:45:56 +0800 Subject: [PATCH] fix --- .../logic/createldaporganizationlogic.go | 1 + .../logic/getldaporganizationslogic.go | 37 +++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/server/ldap-admin/internal/logic/createldaporganizationlogic.go b/server/ldap-admin/internal/logic/createldaporganizationlogic.go index c662e47f..05b535fe 100644 --- a/server/ldap-admin/internal/logic/createldaporganizationlogic.go +++ b/server/ldap-admin/internal/logic/createldaporganizationlogic.go @@ -57,6 +57,7 @@ func (l *CreateLdapOrganizationLogic) CreateLdapOrganization(req *types.CreateLd organizationDN := "ou=" + req.OrganizationEnName + "," + req.ParentOrganizationDN err := l.svcCtx.Ldap.Create(organizationDN, map[string][]string{ "objectClass": {"top", "groupOfUniqueNames"}, + "owner": {""}, //负责人DN "cn": {req.OrganizationEnName}, "ou": {req.OrganizationEnName}, "businessCategory": {req.BusinessCategory}, diff --git a/server/ldap-admin/internal/logic/getldaporganizationslogic.go b/server/ldap-admin/internal/logic/getldaporganizationslogic.go index 80da8855..b138e6a5 100644 --- a/server/ldap-admin/internal/logic/getldaporganizationslogic.go +++ b/server/ldap-admin/internal/logic/getldaporganizationslogic.go @@ -1,6 +1,7 @@ package logic import ( + "fmt" "fusenapi/utils/basic" "github.com/go-ldap/ldap/v3" "net/http" @@ -35,6 +36,8 @@ func NewGetLdapOrganizationsLogic(ctx context.Context, svcCtx *svc.ServiceContex type DNItem struct { Attribute map[string]interface{} `json:"attribute"` MemberCount int `json:"member_count"` + OwnerName string `json:"owner_name"` + OwnerDN string `json:"owner_dn"` Level int `json:"level"` DN string `json:"dn"` ParentDN string `json:"parent_dn"` @@ -57,14 +60,16 @@ func (l *GetLdapOrganizationsLogic) GetLdapOrganizations(req *types.Request, r * return resp.SetStatusWithMessage(basic.CodeServiceErr, "基础用户组的DN未配置") } filter := "(&(objectClass=groupOfUniqueNames)(objectClass=top))" - fields := []string{"businessCategory", "dn", "uniqueMember"} + fields := []string{"businessCategory", "owner", "dn", "uniqueMember"} searchResult, err := l.svcCtx.Ldap.Search(l.svcCtx.Config.Ldap.BaseDN, ldap.ScopeWholeSubtree, filter, fields, nil) if err != nil { return resp.SetStatusWithMessage(basic.CodeServiceErr, "查询失败:"+err.Error()) } mapDN := make(map[string]*DNItem) sortNum := 0 + ownerFilterBuilder := strings.Builder{} //每个DN存入map + ownerDN := "" for _, v := range searchResult.Entries { sortNum++ attribute := make(map[string]interface{}) @@ -72,22 +77,48 @@ func (l *GetLdapOrganizationsLogic) GetLdapOrganizations(req *types.Request, r * for _, attr := range v.Attributes { //判断是否有成员(不包含root用户所以判断大于1) if attr.Name == "uniqueMember" { - memberCount = len(attr.Values) + memberCount = len(attr.Values) - 1 //不包含root用户 continue } + if attr.Name == "owner" && len(attr.Values) != 0 { //负责人 + ownerDN = attr.Values[0] + //解析用户DN,只需要提取cn + userCn := strings.Split(attr.Values[0], ",")[0] + ownerFilterBuilder.WriteString(fmt.Sprintf("(%s)", userCn)) + } 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{ + data := &DNItem{ DN: v.DN, ParentDN: "", Level: level, MemberCount: memberCount, Attribute: attribute, + OwnerDN: ownerDN, Sort: sortNum, Child: make([]*DNItem, 0, 100), } + mapDN[v.DN] = data + } + ownerFilters := ownerFilterBuilder.String() + if ownerFilters != "" { + ownerFilters = "(|" + ownerFilterBuilder.String() + ")" + //获取负责人列表信息 + ldapOwnerList, err := l.svcCtx.Ldap.GetLdapBaseTeamUsersByParams(ownerFilters) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "获取部门负责人失败,"+err.Error()) + } + //把负责人塞到对应部门中 + for _, v := range mapDN { + for _, owner := range ldapOwnerList { + if v.OwnerDN == owner.UserDN { + v.OwnerName = owner.UserName + } + } + } } //组织树形层级关系 minLevel := 0