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

98 lines
3.5 KiB
Go
Raw Normal View History

2023-11-16 06:07:53 +00:00
package logic
import (
"fusenapi/utils/basic"
2023-11-24 04:24:40 +00:00
"fusenapi/utils/chinese_to_pinyin"
2023-11-24 04:05:09 +00:00
"fusenapi/utils/email"
2023-11-21 10:10:30 +00:00
"net/http"
2023-11-16 06:07:53 +00:00
"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-21 10:10:30 +00:00
func (l *CreateLdapOrganizationLogic) CreateLdapOrganization(req *types.CreateLdapOrganizationReq, r *http.Request) (resp *basic.Response) {
2023-11-22 02:12:46 +00:00
2023-11-22 02:19:27 +00:00
if !l.svcCtx.Ldap.VerifyAuthority(r) {
2023-11-21 10:10:30 +00:00
return resp.SetStatusWithMessage(basic.CodeUnAuth, "无权限,请联系管理员开通")
}
2023-11-17 02:33:40 +00:00
req.ParentOrganizationDN = strings.Trim(req.ParentOrganizationDN, " ")
2023-11-24 04:21:00 +00:00
req.OrganizationName = strings.Trim(req.OrganizationName, " ")
if req.OrganizationName == "" {
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-24 02:52:54 +00:00
req.ParentOrganizationDN = l.svcCtx.Config.Ldap.BaseDN //不传则是第一层级
2023-11-16 06:07:53 +00:00
}
2023-11-24 04:05:09 +00:00
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")
}
2023-11-24 04:24:40 +00:00
organizationNamePinyin := chinese_to_pinyin.ChineseToPinyin(req.OrganizationName)
2023-11-16 06:07:53 +00:00
//组装organization dn
2023-11-24 05:11:45 +00:00
organizationDN := "ou=" + organizationNamePinyin + "," + req.ParentOrganizationDN
2023-11-22 02:12:46 +00:00
err := l.svcCtx.Ldap.Create(organizationDN, map[string][]string{
2023-11-16 06:52:27 +00:00
"objectClass": {"top", "groupOfUniqueNames"},
2023-11-24 04:05:09 +00:00
"owner": {req.OwnerDN}, //负责人DN
2023-11-24 04:24:40 +00:00
"cn": {organizationNamePinyin},
"ou": {organizationNamePinyin},
2023-11-24 04:21:00 +00:00
"businessCategory": {req.OrganizationName},
2023-11-24 04:05:09 +00:00
"uniqueMember": {req.OwnerDN}, //必须有一个初始的成员
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
}
2023-11-27 06:41:10 +00:00
user, err := l.svcCtx.Ldap.GetLdapUserInfo(req.OwnerDN)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "获取负责人信息失败,"+err.Error())
}
if user.Status != 1 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "负责人状态不正常")
}
//用户加入的部门
user.OrganizationDNList = append(user.OrganizationDNList, organizationDN)
//用户管理的部门
user.ManageOrganizationDNList = append(user.ManageOrganizationDNList, organizationDN)
//更新用户信息
err = l.svcCtx.Ldap.Update(user.UserDN, map[string][]string{
"departmentNumber": user.OrganizationDNList,
"telexNumber": user.ManageOrganizationDNList,
})
if err != nil {
return resp.SetStatusWithMessage(basic.CodeOK, "添加组织成功,但是设置负责人信息失败,"+err.Error())
}
return resp.SetStatusWithMessage(basic.CodeOK, "添加组织成功")
2023-11-16 06:07:53 +00:00
}
// 处理逻辑后 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)
// }