This commit is contained in:
laodaming 2023-11-24 11:45:56 +08:00
parent 963940c367
commit 8c45de4d25
2 changed files with 35 additions and 3 deletions

View File

@ -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},

View File

@ -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