This commit is contained in:
laodaming 2023-11-24 10:52:54 +08:00
parent 9c97414597
commit 82db686b05
2 changed files with 21 additions and 17 deletions

View File

@ -48,7 +48,7 @@ func (l *CreateLdapOrganizationLogic) CreateLdapOrganization(req *types.CreateLd
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,组织英文名不能包含中文") return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,组织英文名不能包含中文")
} }
if req.ParentOrganizationDN == "" { if req.ParentOrganizationDN == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,父级DN不能为空") req.ParentOrganizationDN = l.svcCtx.Config.Ldap.BaseDN //不传则是第一层级
} }
if req.BusinessCategory == "" { if req.BusinessCategory == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,组织分类名不能为空") return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,组织分类名不能为空")

View File

@ -34,7 +34,8 @@ func NewGetLdapOrganizationsLogic(ctx context.Context, svcCtx *svc.ServiceContex
// } // }
type DNItem struct { type DNItem struct {
Attribute map[string]interface{} `json:"attribute"` Attribute map[string]interface{} `json:"attribute"`
HasMember bool `json:"has_member"` //是否有成员 MemberCount int `json:"member_count"`
Level int `json:"level"`
DN string `json:"dn"` DN string `json:"dn"`
ParentDN string `json:"parent_dn"` ParentDN string `json:"parent_dn"`
Sort int `json:"sort"` Sort int `json:"sort"`
@ -55,7 +56,7 @@ func (l *GetLdapOrganizationsLogic) GetLdapOrganizations(req *types.Request, r *
if len(peopleDNSlice) <= 1 { if len(peopleDNSlice) <= 1 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "基础用户组的DN未配置") return resp.SetStatusWithMessage(basic.CodeServiceErr, "基础用户组的DN未配置")
} }
filter := "(|(&(objectClass=groupOfUniqueNames)(objectClass=top))(objectClass=organization))" filter := "(&(objectClass=groupOfUniqueNames)(objectClass=top))"
fields := []string{"businessCategory", "dn", "uniqueMember"} fields := []string{"businessCategory", "dn", "uniqueMember"}
searchResult, err := l.svcCtx.Ldap.Search(l.svcCtx.Config.Ldap.BaseDN, ldap.ScopeWholeSubtree, filter, fields, nil) searchResult, err := l.svcCtx.Ldap.Search(l.svcCtx.Config.Ldap.BaseDN, ldap.ScopeWholeSubtree, filter, fields, nil)
if err != nil { if err != nil {
@ -67,19 +68,22 @@ func (l *GetLdapOrganizationsLogic) GetLdapOrganizations(req *types.Request, r *
for _, v := range searchResult.Entries { for _, v := range searchResult.Entries {
sortNum++ sortNum++
attribute := make(map[string]interface{}) attribute := make(map[string]interface{})
hasMember := false memberCount := 0
for _, attr := range v.Attributes { for _, attr := range v.Attributes {
//判断是否有成员(不包含root用户所以判断大于1) //判断是否有成员(不包含root用户所以判断大于1)
if attr.Name == "uniqueMember" && len(attr.Values) > 1 { if attr.Name == "uniqueMember" {
hasMember = true memberCount = len(attr.Values) - 1 //不要包含原始用户
continue continue
} }
attribute[attr.Name] = strings.Join(attr.Values, ",") 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{ mapDN[v.DN] = &DNItem{
DN: v.DN, DN: v.DN,
ParentDN: "", ParentDN: "",
HasMember: hasMember, Level: level,
MemberCount: memberCount,
Attribute: attribute, Attribute: attribute,
Sort: sortNum, Sort: sortNum,
Child: make([]*DNItem, 0, 100), Child: make([]*DNItem, 0, 100),