package logic import ( "fusenapi/utils/basic" "fusenapi/utils/chinese_to_pinyin" "fusenapi/utils/ldap_lib" "net/http" "strings" "context" "fusenapi/server/ldap-admin/internal/svc" "fusenapi/server/ldap-admin/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type CreateLdapOrganizationLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewCreateLdapOrganizationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateLdapOrganizationLogic { return &CreateLdapOrganizationLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } // 处理进入前逻辑w,r // func (l *CreateLdapOrganizationLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { // } func (l *CreateLdapOrganizationLogic) CreateLdapOrganization(req *types.CreateLdapOrganizationReq, r *http.Request) (resp *basic.Response) { ldapServer := ldap_lib.NewLdap(l.svcCtx.Ldap, l.svcCtx.Config.Ldap.BaseDN, l.svcCtx.Config.Ldap.RootDN, l.svcCtx.Config.Ldap.PeopleGroupDN) if !ldapServer.VerifyAuthority(r.Header.Get("Ldap-Authorization"), l.svcCtx.Config.Auth.AccessSecret) { return resp.SetStatusWithMessage(basic.CodeUnAuth, "无权限,请联系管理员开通") } req.OrganizationEnName = strings.Trim(req.OrganizationEnName, " ") req.ParentOrganizationDN = strings.Trim(req.ParentOrganizationDN, " ") req.BusinessCategory = strings.Trim(req.BusinessCategory, " ") if len(strings.Split(req.OrganizationEnName, ",")) != 1 { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,组织英文名不符合规范") } //转拼音比较下 if req.OrganizationEnName != chinese_to_pinyin.ChineseToPinyin(req.OrganizationEnName) { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,组织英文名不能包含中文") } if req.ParentOrganizationDN == "" { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,父级DN不能为空") } if req.BusinessCategory == "" { return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,组织分类名不能为空") } //组装organization dn organizationDN := "ou=" + req.OrganizationEnName + "," + req.ParentOrganizationDN err := ldapServer.Create(organizationDN, map[string][]string{ "objectClass": {"top", "groupOfUniqueNames"}, "cn": {req.OrganizationEnName}, "ou": {req.OrganizationEnName}, "businessCategory": {req.BusinessCategory}, "uniqueMember": {l.svcCtx.Config.Ldap.RootDN}, //创建groupOfUniqueNames对象类型需要至少一个member,把root加进去 }) if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "创建组织失败,"+err.Error()) } return resp.SetStatus(basic.CodeOK) } // 处理逻辑后 w,r 如:重定向, resp 必须重新处理 // func (l *CreateLdapOrganizationLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { // // httpx.OkJsonCtx(r.Context(), w, resp) // }