2023-11-13 09:52:20 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
2023-11-14 10:39:10 +00:00
|
|
|
"context"
|
2023-11-13 09:52:20 +00:00
|
|
|
"fusenapi/utils/auth"
|
|
|
|
"fusenapi/utils/basic"
|
2023-11-15 03:23:50 +00:00
|
|
|
"github.com/go-ldap/ldap/v3"
|
|
|
|
"log"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2023-11-13 09:52:20 +00:00
|
|
|
|
|
|
|
"fusenapi/server/ldap-admin/internal/svc"
|
|
|
|
"fusenapi/server/ldap-admin/internal/types"
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GetDepartmentsLogic struct {
|
|
|
|
logx.Logger
|
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGetDepartmentsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetDepartmentsLogic {
|
|
|
|
return &GetDepartmentsLogic{
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 处理进入前逻辑w,r
|
|
|
|
// func (l *GetDepartmentsLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// }
|
2023-11-15 06:18:45 +00:00
|
|
|
type DNItem struct {
|
2023-11-15 03:49:00 +00:00
|
|
|
Attribute map[string]interface{} `json:"attribute"`
|
|
|
|
DN string `json:"dn"`
|
2023-11-15 06:32:47 +00:00
|
|
|
ParentDN string `json:"parent_dn"`
|
2023-11-15 03:49:00 +00:00
|
|
|
Sort int `json:"sort"`
|
2023-11-15 06:18:45 +00:00
|
|
|
Child []*DNItem `json:"child"`
|
2023-11-15 03:23:50 +00:00
|
|
|
}
|
2023-11-13 09:52:20 +00:00
|
|
|
|
2023-11-14 03:26:08 +00:00
|
|
|
func (l *GetDepartmentsLogic) GetDepartments(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
|
2023-11-15 06:12:05 +00:00
|
|
|
//从ldap获取组织架构数据
|
2023-11-15 03:23:50 +00:00
|
|
|
// 创建搜索请求
|
|
|
|
searchRequest := ldap.NewSearchRequest(
|
2023-11-15 06:18:45 +00:00
|
|
|
l.svcCtx.Config.Ldap.BaseDN,
|
2023-11-15 03:23:50 +00:00
|
|
|
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
|
|
|
"(objectClass=*)",
|
|
|
|
[]string{ /*"cn", "sn", "givenName", "mail", "telephoneNumber", "department", "title"*/ },
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
// 执行搜索请求
|
|
|
|
searchResult, err := l.svcCtx.Ldap.Search(searchRequest)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-11-15 06:18:45 +00:00
|
|
|
mapDN := make(map[string]*DNItem)
|
2023-11-15 03:23:50 +00:00
|
|
|
sortNum := 0
|
|
|
|
for _, v := range searchResult.Entries {
|
|
|
|
sortNum++
|
2023-11-15 03:49:00 +00:00
|
|
|
attribute := make(map[string]interface{})
|
|
|
|
for _, attr := range v.Attributes {
|
|
|
|
if attr.Name == "objectClass" {
|
|
|
|
mapObjectClass := make(map[string]struct{})
|
|
|
|
for _, objectClassItem := range attr.Values {
|
|
|
|
mapObjectClass[objectClassItem] = struct{}{}
|
|
|
|
}
|
|
|
|
attribute[attr.Name] = mapObjectClass
|
|
|
|
} else {
|
|
|
|
attribute[attr.Name] = strings.Join(attr.Values, ",")
|
|
|
|
}
|
|
|
|
}
|
2023-11-15 06:18:45 +00:00
|
|
|
mapDN[v.DN] = &DNItem{
|
2023-11-15 03:49:00 +00:00
|
|
|
DN: v.DN,
|
2023-11-15 06:32:47 +00:00
|
|
|
ParentDN: "",
|
2023-11-15 03:49:00 +00:00
|
|
|
Attribute: attribute,
|
|
|
|
Sort: sortNum,
|
2023-11-15 06:18:45 +00:00
|
|
|
Child: make([]*DNItem, 0, 100),
|
2023-11-15 03:23:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//组织层级关系
|
|
|
|
minLevel := 0
|
|
|
|
for dn, v := range mapDN {
|
|
|
|
sl := strings.Split(dn, ",")
|
|
|
|
lensl := len(sl)
|
|
|
|
if lensl < minLevel || minLevel == 0 {
|
|
|
|
minLevel = lensl
|
|
|
|
}
|
|
|
|
//没有父级
|
|
|
|
if lensl <= 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
//有父级
|
2023-11-15 06:32:47 +00:00
|
|
|
parentDN := strings.Join(sl[1:], ",")
|
|
|
|
if parent, ok := mapDN[parentDN]; ok {
|
|
|
|
v.ParentDN = parentDN
|
2023-11-15 03:23:50 +00:00
|
|
|
parent.Child = append(parent.Child, v)
|
|
|
|
//排序
|
|
|
|
sort.Slice(parent.Child, func(i, j int) bool {
|
|
|
|
return parent.Child[i].Sort < parent.Child[j].Sort
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//只获取最顶层
|
|
|
|
list := make([]interface{}, 0, len(mapDN))
|
|
|
|
for dn, v := range mapDN {
|
|
|
|
sl := strings.Split(dn, ",")
|
|
|
|
if len(sl) == minLevel {
|
|
|
|
list = append(list, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "", list)
|
2023-11-14 04:13:04 +00:00
|
|
|
}
|