2023-11-16 08:57:29 +00:00
|
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"fusenapi/model/gmodel"
|
|
|
|
|
"fusenapi/utils/auth"
|
|
|
|
|
"fusenapi/utils/basic"
|
|
|
|
|
"fusenapi/utils/chinese_to_pinyin"
|
2023-11-17 02:07:05 +00:00
|
|
|
|
"fusenapi/utils/email"
|
2023-11-20 03:37:03 +00:00
|
|
|
|
"fusenapi/utils/encryption_decryption"
|
2023-11-16 08:57:29 +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"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type CreateLdapUserLogic struct {
|
|
|
|
|
logx.Logger
|
|
|
|
|
ctx context.Context
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewCreateLdapUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateLdapUserLogic {
|
|
|
|
|
return &CreateLdapUserLogic{
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理进入前逻辑w,r
|
|
|
|
|
// func (l *CreateLdapUserLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
func (l *CreateLdapUserLogic) CreateLdapUser(req *types.CreateLdapUserReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
|
|
|
|
req.UserName = strings.Trim(req.UserName, " ")
|
|
|
|
|
req.Mobile = strings.Trim(req.Mobile, " ")
|
|
|
|
|
req.Email = strings.Trim(req.Email, " ")
|
|
|
|
|
req.Password = strings.Trim(req.Password, " ")
|
2023-11-21 02:13:12 +00:00
|
|
|
|
if req.GroupId < 0 {
|
|
|
|
|
req.GroupId = 0
|
|
|
|
|
}
|
2023-11-16 08:57:29 +00:00
|
|
|
|
if req.UserName == "" {
|
2023-11-17 03:24:54 +00:00
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,用户名不能为空")
|
2023-11-16 08:57:29 +00:00
|
|
|
|
}
|
|
|
|
|
if req.Password == "" {
|
2023-11-17 03:24:54 +00:00
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,密码不能为空")
|
2023-11-16 08:57:29 +00:00
|
|
|
|
}
|
2023-11-17 02:07:05 +00:00
|
|
|
|
if !email.IsEmailValid(req.Email) {
|
2023-11-17 03:24:54 +00:00
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,邮箱格式不正确")
|
2023-11-16 09:18:18 +00:00
|
|
|
|
}
|
2023-11-20 09:16:17 +00:00
|
|
|
|
ldapServer := ldap_lib.NewLdap(l.svcCtx.Ldap, l.svcCtx.Config.Ldap.BaseDN, l.svcCtx.Config.Ldap.RootDN, l.svcCtx.Config.Ldap.PeopleGroupDN)
|
2023-11-16 08:57:29 +00:00
|
|
|
|
//把用户名转pinyin
|
|
|
|
|
userNamePinyin := chinese_to_pinyin.ChineseToPinyin(req.UserName)
|
|
|
|
|
//新增一条记录获取递增用户id
|
|
|
|
|
userData := &gmodel.LdapUser{}
|
|
|
|
|
if err := l.svcCtx.AllModels.LdapUser.Create(l.ctx, userData); err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "获取自增用户id失败")
|
|
|
|
|
}
|
2023-11-16 09:18:18 +00:00
|
|
|
|
userDN := fmt.Sprintf("cn=%s,%s", req.Email, l.svcCtx.Config.Ldap.PeopleGroupDN)
|
2023-11-20 03:37:03 +00:00
|
|
|
|
pwd, err := encryption_decryption.CBCEncrypt(req.Password)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logx.Error(err)
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "加密密码失败")
|
|
|
|
|
}
|
2023-11-16 08:57:29 +00:00
|
|
|
|
if err := ldapServer.Create(userDN, map[string][]string{
|
|
|
|
|
"objectClass": {"person", "organizationalPerson", "inetOrgPerson", "posixAccount", "top", "shadowAccount"}, //固有属性
|
|
|
|
|
"shadowLastChange": {"19676"}, //固有属性
|
|
|
|
|
"shadowMin": {"0"}, //固有属性
|
|
|
|
|
"shadowMax": {"99999"}, //固有属性
|
|
|
|
|
"shadowWarning": {"7"}, //固有属性
|
|
|
|
|
"loginShell": {"/usr/sbin/nologin"}, //固有属性
|
|
|
|
|
"homeDirectory": {"/home/users/" + userNamePinyin},
|
2023-11-21 02:08:46 +00:00
|
|
|
|
"employeeType": {fmt.Sprintf("%d", req.EmployeeType)}, //员工类型:1正式 2实习 3外包
|
|
|
|
|
"uidNumber": {fmt.Sprintf("%d", userData.Id)}, //用户id
|
|
|
|
|
"gidNumber": {fmt.Sprintf("%d", userData.Id)}, //用户id
|
|
|
|
|
"uid": {userNamePinyin}, //用户名(拼音)
|
|
|
|
|
"cn": {req.Email}, //邮箱
|
|
|
|
|
"sn": {req.UserName}, //用户名
|
|
|
|
|
"mail": {req.Email}, //邮箱
|
|
|
|
|
"postalCode": {fmt.Sprintf("%d", req.Status)}, //状态
|
|
|
|
|
"departmentNumber": {fmt.Sprintf("%d", req.GroupId)}, //权限分组id
|
|
|
|
|
"postalAddress": {req.Avatar}, //头像
|
|
|
|
|
"mobile": {req.Mobile}, //手机号
|
|
|
|
|
"userPassword": {"{crypt}" + pwd}, //密码
|
2023-11-16 08:57:29 +00:00
|
|
|
|
}); err != nil {
|
|
|
|
|
logx.Error(err)
|
2023-11-17 08:02:35 +00:00
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "添加用户失败,"+err.Error())
|
2023-11-16 08:57:29 +00:00
|
|
|
|
}
|
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "添加用户成功")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
|
|
|
|
// func (l *CreateLdapUserLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
|
|
|
|
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
|
|
|
|
// }
|