fusenapi/server/ldap-admin/internal/logic/createldaporganizationlogic.go
laodaming ca91a6f3ce fix
2023-11-24 12:21:00 +08:00

76 lines
2.5 KiB
Go

package logic
import (
"fusenapi/utils/basic"
"fusenapi/utils/email"
"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) {
if !l.svcCtx.Ldap.VerifyAuthority(r) {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "无权限,请联系管理员开通")
}
req.ParentOrganizationDN = strings.Trim(req.ParentOrganizationDN, " ")
req.OrganizationName = strings.Trim(req.OrganizationName, " ")
if req.OrganizationName == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "组织名不能为空")
}
if req.ParentOrganizationDN == "" {
req.ParentOrganizationDN = l.svcCtx.Config.Ldap.BaseDN //不传则是第一层级
}
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
organizationDN := "ou=" + req.OrganizationName + "," + req.ParentOrganizationDN
err := l.svcCtx.Ldap.Create(organizationDN, map[string][]string{
"objectClass": {"top", "groupOfUniqueNames"},
"owner": {req.OwnerDN}, //负责人DN
"cn": {req.OrganizationName},
"ou": {req.OrganizationName},
"businessCategory": {req.OrganizationName},
"uniqueMember": {req.OwnerDN}, //必须有一个初始的成员
})
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)
// }