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

152 lines
5.0 KiB
Go
Raw Normal View History

2023-11-17 03:25:16 +00:00
package logic
import (
2023-11-17 07:06:03 +00:00
"fmt"
2023-11-17 03:25:16 +00:00
"fusenapi/utils/auth"
"fusenapi/utils/basic"
2023-11-17 07:06:03 +00:00
"fusenapi/utils/ldap_lib"
"github.com/go-ldap/ldap/v3"
"strconv"
2023-11-17 03:25:16 +00:00
"strings"
"context"
"fusenapi/server/ldap-admin/internal/svc"
"fusenapi/server/ldap-admin/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetLdapOrganizationMembersLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetLdapOrganizationMembersLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLdapOrganizationMembersLogic {
return &GetLdapOrganizationMembersLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *GetLdapOrganizationMembersLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *GetLdapOrganizationMembersLogic) GetLdapOrganizationMembers(req *types.GetLdapOrganizationMembersReq, userinfo *auth.UserInfo) (resp *basic.Response) {
req.OrganizationDN = strings.Trim(req.OrganizationDN, " ")
if len(req.OrganizationDN) <= 3 || req.OrganizationDN[:3] != "ou=" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,无效的组织DN")
}
2023-11-17 07:06:03 +00:00
//先获取组织成员
ldapServer := ldap_lib.NewLdap(l.svcCtx.Ldap, l.svcCtx.Config.Ldap.BaseDN, l.svcCtx.Config.Ldap.RootDN)
//获取跟用户dn筛选排除该用户
rootDNSlice := strings.Split(l.svcCtx.Config.Ldap.RootDN, ",")
if len(rootDNSlice) == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "根用户DN未设置")
}
rootCn := rootDNSlice[0]
filter := "(&(objectClass=groupOfUniqueNames)(!(" + rootCn + ")))"
fields := []string{"uniqueMember"} //只是查询成员
result, err := ldapServer.Search(req.OrganizationDN, ldap.ScopeWholeSubtree, filter, fields, nil)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "查询ldap组织成员错误,"+err.Error())
}
2023-11-17 08:44:08 +00:00
//遍历成员提取cn用于从用户基础组中获取用户信息列表
2023-11-17 07:06:03 +00:00
filterBuilder := strings.Builder{}
memberCount := 0
for _, entry := range result.Entries {
if entry.DN != req.OrganizationDN {
continue
}
2023-11-17 08:44:08 +00:00
//查到用户信息了
2023-11-17 07:06:03 +00:00
for _, attr := range entry.Attributes {
if attr.Name != "uniqueMember" {
continue
}
memberCount = len(attr.Values)
for _, memberDn := range attr.Values {
//不需要根用户
if memberDn == l.svcCtx.Config.Ldap.RootDN {
continue
}
//解析dn成每个小的单元
cellList := strings.Split(memberDn, ",") //取cn邮箱
filterBuilder.WriteString(fmt.Sprintf("(&%s)", "("+cellList[0]+")"))
}
break
}
break
}
//从新赋值filter
filter = "(&(objectClass=posixAccount)(objectClass=inetOrgPerson)(|" + filterBuilder.String() + "))"
fmt.Println(filter)
//从用户基本组中找到员工
result, err = ldapServer.Search(l.svcCtx.Config.Ldap.PeopleGroupDN, ldap.ScopeWholeSubtree, filter, nil, nil)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "查询ldap帐号信息失败,"+err.Error())
}
userList := make([]types.GetLdapOrganizationMembersItem, 0, memberCount)
for _, entry := range result.Entries {
2023-11-17 08:40:50 +00:00
user := types.GetLdapOrganizationMembersItem{
2023-11-17 07:06:03 +00:00
UserDN: entry.DN,
}
2023-11-17 08:40:50 +00:00
canAppend := true
2023-11-17 07:06:03 +00:00
for _, attr := range entry.Attributes {
switch attr.Name {
case "uidNumber": //用户id
if len(attr.Values) == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "用户id不存在")
}
2023-11-17 08:40:50 +00:00
user.UserId, err = strconv.ParseInt(attr.Values[0], 10, 64)
2023-11-17 07:06:03 +00:00
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "用户id转数字失败")
}
case "sn": //用户真名
2023-11-17 08:40:50 +00:00
user.UserName = strings.Join(attr.Values, "")
2023-11-17 07:06:03 +00:00
case "mail": //邮箱
2023-11-17 08:40:50 +00:00
user.Email = strings.Join(attr.Values, "")
2023-11-17 07:06:03 +00:00
case "mobile": //手机号
2023-11-17 08:40:50 +00:00
user.Mobile = strings.Join(attr.Values, "")
2023-11-17 07:06:03 +00:00
case "postalAddress": //头像
2023-11-17 08:40:50 +00:00
user.Avatar = strings.Join(attr.Values, "")
2023-11-17 07:06:03 +00:00
case "postalCode": //状态
if len(attr.Values) == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "用户状态不存在")
}
2023-11-17 08:40:50 +00:00
user.Status, err = strconv.ParseInt(attr.Values[0], 10, 64)
2023-11-17 07:06:03 +00:00
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "用户状态转数字失败")
}
2023-11-17 08:40:50 +00:00
//无效员工就不要显示了
if user.Status != 1 {
//从该组中移除该成员
if err = ldapServer.RemoveUserFromOrganization(req.OrganizationDN, entry.DN); err != nil {
logx.Error("移除组中离职成员失败,", err.Error())
}
canAppend = false //要移除的成员就不要显示了
break
}
2023-11-17 07:06:03 +00:00
}
}
//添加列表
2023-11-17 08:40:50 +00:00
if canAppend {
userList = append(userList, user)
}
2023-11-17 07:06:03 +00:00
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetLdapOrganizationMembersRsp{
List: userList,
})
2023-11-17 03:25:16 +00:00
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *GetLdapOrganizationMembersLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }