This commit is contained in:
laodaming 2023-11-24 12:05:09 +08:00
parent 8c45de4d25
commit dbf39e3057
4 changed files with 47 additions and 33 deletions

View File

@ -3,6 +3,7 @@ package logic
import ( import (
"fusenapi/utils/basic" "fusenapi/utils/basic"
"fusenapi/utils/chinese_to_pinyin" "fusenapi/utils/chinese_to_pinyin"
"fusenapi/utils/email"
"net/http" "net/http"
"strings" "strings"
@ -53,15 +54,22 @@ func (l *CreateLdapOrganizationLogic) CreateLdapOrganization(req *types.CreateLd
if req.BusinessCategory == "" { if req.BusinessCategory == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,组织分类名不能为空") return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,组织分类名不能为空")
} }
if len(req.OwnerDN) <= 3 || req.OwnerDN[:3] != "cn=" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "无效的用户DN")
}
cnEmail := strings.Split(req.OwnerDN, ",")[0][3:]
if !email.IsEmailValid(cnEmail) {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "错误的用户cn")
}
//组装organization dn //组装organization dn
organizationDN := "ou=" + req.OrganizationEnName + "," + req.ParentOrganizationDN organizationDN := "ou=" + req.OrganizationEnName + "," + req.ParentOrganizationDN
err := l.svcCtx.Ldap.Create(organizationDN, map[string][]string{ err := l.svcCtx.Ldap.Create(organizationDN, map[string][]string{
"objectClass": {"top", "groupOfUniqueNames"}, "objectClass": {"top", "groupOfUniqueNames"},
"owner": {""}, //负责人DN "owner": {req.OwnerDN}, //负责人DN
"cn": {req.OrganizationEnName}, "cn": {req.OrganizationEnName},
"ou": {req.OrganizationEnName}, "ou": {req.OrganizationEnName},
"businessCategory": {req.BusinessCategory}, "businessCategory": {req.BusinessCategory},
"uniqueMember": {l.svcCtx.Config.Ldap.RootDN}, //创建groupOfUniqueNames对象类型需要至少一个member,把root加进去 "uniqueMember": {req.OwnerDN}, //必须有一个初始的成员
}) })
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)

View File

@ -36,11 +36,12 @@ 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"`
MemberCount int `json:"member_count"` MemberCount int `json:"member_count"`
BusinessCategory string `json:"business_category"`
OwnerName string `json:"owner_name"` OwnerName string `json:"owner_name"`
OwnerDN string `json:"owner_dn"` OwnerDN string `json:"owner_dn"`
Level int `json:"level"` Level int `json:"level"`
DN string `json:"dn"` DepartmentDN string `json:"department_dn"`
ParentDN string `json:"parent_dn"` DepartmentParentDN string `json:"department_parent_dn"`
Sort int `json:"sort"` Sort int `json:"sort"`
Child []*DNItem `json:"child"` Child []*DNItem `json:"child"`
} }
@ -73,26 +74,29 @@ 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{})
memberCount := 0 memberCount := 0 //成员数
businessCategory := "" //部门名称
for _, attr := range v.Attributes { for _, attr := range v.Attributes {
//判断是否有成员(不包含root用户所以判断大于1) switch attr.Name {
if attr.Name == "uniqueMember" { case "uniqueMember":
memberCount = len(attr.Values) - 1 //不包含root用户 memberCount = len(attr.Values)
case "owner":
if len(attr.Values) == 0 {
continue continue
} }
if attr.Name == "owner" && len(attr.Values) != 0 { //负责人
ownerDN = attr.Values[0] ownerDN = attr.Values[0]
//解析用户DN只需要提取cn //解析用户DN只需要提取cn
userCn := strings.Split(attr.Values[0], ",")[0] userCn := strings.Split(attr.Values[0], ",")[0]
ownerFilterBuilder.WriteString(fmt.Sprintf("(%s)", userCn)) ownerFilterBuilder.WriteString(fmt.Sprintf("(%s)", userCn))
case "businessCategory":
businessCategory = strings.Join(attr.Values, ",")
} }
attribute[attr.Name] = strings.Join(attr.Values, ",")
} }
dnSlice := strings.ReplaceAll(v.DN, ","+l.svcCtx.Config.Ldap.BaseDN, "") //把最顶级的组织去掉 dnSlice := strings.ReplaceAll(v.DN, ","+l.svcCtx.Config.Ldap.BaseDN, "") //把最顶级的组织去掉
level := len(strings.Split(dnSlice, ",")) level := len(strings.Split(dnSlice, ","))
data := &DNItem{ data := &DNItem{
DN: v.DN, DepartmentDN: v.DN,
ParentDN: "", BusinessCategory: businessCategory,
Level: level, Level: level,
MemberCount: memberCount, MemberCount: memberCount,
Attribute: attribute, Attribute: attribute,
@ -135,7 +139,7 @@ func (l *GetLdapOrganizationsLogic) GetLdapOrganizations(req *types.Request, r *
//有父级 //有父级
parentDN := strings.Join(sl[1:], ",") parentDN := strings.Join(sl[1:], ",")
if parent, ok := mapDN[parentDN]; ok { if parent, ok := mapDN[parentDN]; ok {
v.ParentDN = parentDN v.DepartmentParentDN = parentDN
parent.Child = append(parent.Child, v) parent.Child = append(parent.Child, v)
//排序 //排序
sort.Slice(parent.Child, func(i, j int) bool { sort.Slice(parent.Child, func(i, j int) bool {

View File

@ -134,8 +134,9 @@ type MenuItem struct {
type CreateLdapOrganizationReq struct { type CreateLdapOrganizationReq struct {
OrganizationEnName string `json:"organization_en_name"` //组织英文名 OrganizationEnName string `json:"organization_en_name"` //组织英文名
BusinessCategory string `json:"business_category"` //组织类别 BusinessCategory string `json:"business_category"` //组织类别名称
ParentOrganizationDN string `json:"parent_organization_dn"` //父级dn ParentOrganizationDN string `json:"parent_organization_dn"` //父级dn
OwnerDN string `json:"owner_dn"` //负责人dn
} }
type DeleteLdapOrganizationReq struct { type DeleteLdapOrganizationReq struct {

View File

@ -222,8 +222,9 @@ type MenuItem {
//增加ldap组织 //增加ldap组织
type CreateLdapOrganizationReq { type CreateLdapOrganizationReq {
OrganizationEnName string `json:"organization_en_name"` //组织英文名 OrganizationEnName string `json:"organization_en_name"` //组织英文名
BusinessCategory string `json:"business_category"` //组织类别 BusinessCategory string `json:"business_category"` //组织类别名称
ParentOrganizationDN string `json:"parent_organization_dn"` //父级dn ParentOrganizationDN string `json:"parent_organization_dn"` //父级dn
OwnerDN string `json:"owner_dn"` //负责人dn
} }
//删除ldap组织 //删除ldap组织
type DeleteLdapOrganizationReq { type DeleteLdapOrganizationReq {