2023-11-16 06:07:53 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fusenapi/utils/auth"
|
|
|
|
"fusenapi/utils/basic"
|
2023-11-17 07:16:34 +00:00
|
|
|
"fusenapi/utils/chinese_to_pinyin"
|
2023-11-16 06:07:53 +00:00
|
|
|
"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-17 02:33:40 +00:00
|
|
|
type CreateLdapOrganizationLogic struct {
|
2023-11-16 06:07:53 +00:00
|
|
|
logx.Logger
|
|
|
|
ctx context.Context
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
}
|
|
|
|
|
2023-11-17 02:33:40 +00:00
|
|
|
func NewCreateLdapOrganizationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateLdapOrganizationLogic {
|
|
|
|
return &CreateLdapOrganizationLogic{
|
2023-11-16 06:07:53 +00:00
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
ctx: ctx,
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 处理进入前逻辑w,r
|
2023-11-17 02:33:40 +00:00
|
|
|
// func (l *CreateLdapOrganizationLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
2023-11-16 06:07:53 +00:00
|
|
|
// }
|
|
|
|
|
2023-11-17 02:33:40 +00:00
|
|
|
func (l *CreateLdapOrganizationLogic) CreateLdapOrganization(req *types.CreateLdapOrganizationReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
2023-11-17 07:11:23 +00:00
|
|
|
req.OrganizationEnName = strings.Trim(req.OrganizationEnName, " ")
|
2023-11-17 02:33:40 +00:00
|
|
|
req.ParentOrganizationDN = strings.Trim(req.ParentOrganizationDN, " ")
|
2023-11-17 07:16:34 +00:00
|
|
|
req.OrganizationName = strings.Trim(req.OrganizationName, " ")
|
2023-11-17 07:11:23 +00:00
|
|
|
if len(strings.Split(req.OrganizationEnName, ",")) != 1 {
|
2023-11-17 07:16:34 +00:00
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,组织英文名不符合规范")
|
|
|
|
}
|
|
|
|
//转拼音比较下
|
|
|
|
if req.OrganizationEnName != chinese_to_pinyin.ChineseToPinyin(req.OrganizationEnName) {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,组织英文名不能包含中文")
|
2023-11-16 06:07:53 +00:00
|
|
|
}
|
2023-11-17 02:33:40 +00:00
|
|
|
if req.ParentOrganizationDN == "" {
|
2023-11-17 03:24:54 +00:00
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,父级DN不能为空")
|
2023-11-16 06:07:53 +00:00
|
|
|
}
|
2023-11-17 07:16:34 +00:00
|
|
|
if req.OrganizationName == "" {
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,组织中文名不能为空")
|
2023-11-16 06:07:53 +00:00
|
|
|
}
|
|
|
|
//组装organization dn
|
2023-11-17 07:11:23 +00:00
|
|
|
organizationDN := "ou=" + req.OrganizationEnName + "," + req.ParentOrganizationDN
|
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"},
|
2023-11-17 07:16:34 +00:00
|
|
|
"cn": {req.OrganizationName},
|
2023-11-17 07:11:23 +00:00
|
|
|
"ou": {req.OrganizationEnName},
|
2023-11-17 07:16:34 +00:00
|
|
|
"businessCategory": {req.OrganizationName},
|
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)
|
2023-11-17 08:02:35 +00:00
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "创建组织失败,"+err.Error())
|
2023-11-16 06:07:53 +00:00
|
|
|
}
|
|
|
|
return resp.SetStatus(basic.CodeOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
2023-11-17 02:33:40 +00:00
|
|
|
// func (l *CreateLdapOrganizationLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
2023-11-16 06:07:53 +00:00
|
|
|
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
|
|
|
// }
|