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

72 lines
2.6 KiB
Go
Raw Normal View History

2023-11-16 06:07:53 +00:00
package logic
import (
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/ldap_lib"
"strings"
"context"
"fusenapi/server/ldap-admin/internal/svc"
"fusenapi/server/ldap-admin/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
2023-11-16 06:29:24 +00:00
type CreateLdapOrginationLogic struct {
2023-11-16 06:07:53 +00:00
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
2023-11-16 06:29:24 +00:00
func NewCreateLdapOrginationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateLdapOrginationLogic {
return &CreateLdapOrginationLogic{
2023-11-16 06:07:53 +00:00
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
2023-11-16 06:29:24 +00:00
// func (l *CreateLdapOrginationLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
2023-11-16 06:07:53 +00:00
// }
2023-11-16 06:29:24 +00:00
func (l *CreateLdapOrginationLogic) CreateLdapOrgination(req *types.CreateLdapOrginationReq, userinfo *auth.UserInfo) (resp *basic.Response) {
req.OrginationOu = strings.Trim(req.OrginationOu, " ")
req.ParentOrginationDN = strings.Trim(req.ParentOrginationDN, " ")
2023-11-16 06:07:53 +00:00
req.BusinessCategory = strings.Trim(req.BusinessCategory, " ")
2023-11-16 06:29:24 +00:00
if req.OrginationOu == "" {
2023-11-16 06:07:53 +00:00
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,organization_ou不能为空")
}
2023-11-16 06:29:24 +00:00
if len(strings.Split(req.OrginationOu, ",")) != 1 {
2023-11-16 06:07:53 +00:00
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,不合法的organization_ou")
}
2023-11-16 06:29:24 +00:00
if req.ParentOrginationDN == "" {
2023-11-16 06:07:53 +00:00
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,parentOrganization_dn不能为空")
}
if req.BusinessCategory == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,business_category不能为空")
}
//组装organization dn
2023-11-16 06:29:24 +00:00
organizationDN := "ou=" + req.OrginationOu + "," + req.ParentOrginationDN
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)
err := ldapServer.Create(organizationDN, map[string][]string{
2023-11-16 06:52:27 +00:00
"objectClass": {"top", "groupOfUniqueNames"},
"cn": {req.BusinessCategory},
2023-11-16 06:29:24 +00:00
"ou": {req.OrginationOu},
2023-11-16 06:07:53 +00:00
"businessCategory": {req.BusinessCategory},
2023-11-16 06:52:27 +00:00
"uniqueMember": {l.svcCtx.Config.Ldap.RootDN}, //创建groupOfUniqueNames对象类型需要至少一个member,把root加进去
2023-11-16 06:07:53 +00:00
})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "ldap服务报错,", err.Error())
}
return resp.SetStatus(basic.CodeOK)
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
2023-11-16 06:29:24 +00:00
// func (l *CreateLdapOrginationLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
2023-11-16 06:07:53 +00:00
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }