fusenapi/server/ldap-admin/internal/logic/getldaporganizationslogic.go

116 lines
3.4 KiB
Go
Raw Normal View History

2023-11-13 09:52:20 +00:00
package logic
import (
"fusenapi/utils/auth"
"fusenapi/utils/basic"
2023-11-16 03:37:35 +00:00
"fusenapi/utils/ldap_lib"
2023-11-16 10:29:45 +00:00
"github.com/go-ldap/ldap/v3"
2023-11-15 03:23:50 +00:00
"sort"
"strings"
2023-11-13 09:52:20 +00:00
2023-11-16 06:15:45 +00:00
"context"
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"
)
2023-11-17 02:33:40 +00:00
type GetLdapOrganizationsLogic struct {
2023-11-13 09:52:20 +00:00
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
2023-11-17 02:33:40 +00:00
func NewGetLdapOrganizationsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLdapOrganizationsLogic {
return &GetLdapOrganizationsLogic{
2023-11-13 09:52:20 +00:00
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
2023-11-17 02:33:40 +00:00
// func (l *GetLdapOrganizationsLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
2023-11-13 09:52:20 +00:00
// }
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-17 02:33:40 +00:00
func (l *GetLdapOrganizationsLogic) GetLdapOrganizations(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
2023-11-15 06:12:05 +00:00
//从ldap获取组织架构数据
2023-11-16 03:33:26 +00:00
rootCn := strings.Split(l.svcCtx.Config.Ldap.RootDN, ",")
if len(rootCn) == 0 {
2023-11-16 03:37:35 +00:00
return resp.SetStatusWithMessage(basic.CodeServiceErr, "root用户DN未设置")
2023-11-16 03:33:26 +00:00
}
2023-11-16 06:07:53 +00:00
ldapServer := ldap_lib.NewLdap(l.svcCtx.Ldap, l.svcCtx.Config.Ldap.BaseDN, l.svcCtx.Config.Ldap.RootDN)
2023-11-16 08:57:29 +00:00
peopleDNSlice := strings.Split(l.svcCtx.Config.Ldap.PeopleGroupDN, ",")
if len(peopleDNSlice) <= 1 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "基础用户组的DN未配置")
}
2023-11-17 03:10:38 +00:00
filter := "(|(&(objectClass=groupOfUniqueNames)(objectClass=top))(objectClass=organization))"
fields := []string{"businessCategory", "dn"}
searchResult, err := ldapServer.Search(l.svcCtx.Config.Ldap.BaseDN, ldap.ScopeWholeSubtree, filter, fields, nil)
2023-11-15 03:23:50 +00:00
if err != nil {
2023-11-16 03:37:35 +00:00
return resp.SetStatusWithMessage(basic.CodeServiceErr, "查询失败:"+err.Error())
2023-11-15 03:23:50 +00:00
}
2023-11-15 06:18:45 +00:00
mapDN := make(map[string]*DNItem)
2023-11-15 03:23:50 +00:00
sortNum := 0
2023-11-15 09:41:16 +00:00
//每个DN存入map
2023-11-15 03:23:50 +00:00
for _, v := range searchResult.Entries {
sortNum++
2023-11-15 03:49:00 +00:00
attribute := make(map[string]interface{})
for _, attr := range v.Attributes {
2023-11-17 03:10:38 +00:00
attribute[attr.Name] = strings.Join(attr.Values, ",")
2023-11-15 03:49:00 +00:00
}
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
}
}
2023-11-15 09:41:16 +00:00
//组织树形层级关系
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
}
2023-11-16 06:15:45 +00:00
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
2023-11-17 02:33:40 +00:00
// func (l *GetLdapOrganizationsLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
2023-11-16 06:15:45 +00:00
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }