This commit is contained in:
laodaming
2023-11-17 18:17:33 +08:00
parent 28f5fa1019
commit 57fc586eb8
6 changed files with 120 additions and 78 deletions

View File

@@ -2,15 +2,11 @@ package logic
import (
"context"
"fusenapi/server/ldap-admin/internal/svc"
"fusenapi/server/ldap-admin/internal/types"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/ldap_lib"
"github.com/go-ldap/ldap/v3"
"strconv"
"strings"
"fusenapi/server/ldap-admin/internal/svc"
"fusenapi/server/ldap-admin/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -38,59 +34,20 @@ func (l *GetLdapUserInfoLogic) GetLdapUserInfo(req *types.GetLdapUserInfoReq, us
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误用户DN错误")
}
ldapServer := ldap_lib.NewLdap(l.svcCtx.Ldap, l.svcCtx.Config.Ldap.BaseDN, l.svcCtx.Config.Ldap.RootDN)
res, err := ldapServer.Search(req.UserDN, ldap.ScopeWholeSubtree, "(&(objectClass=posixAccount)(objectClass=inetOrgPerson))", nil, nil)
user, err := ldapServer.GetLdapUserInfo(req.UserDN)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "获取用户信息失败:"+err.Error())
return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
}
if len(res.Entries) != 1 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "查询到不到用户信息")
}
apiRsp := types.GetLdapUserInfoRsp{
UserId: 0,
Status: 0,
}
for _, entry := range res.Entries {
if entry.DN != req.UserDN {
continue
}
apiRsp.UserDN = entry.DN
for _, attr := range entry.Attributes {
switch attr.Name {
case "uidNumber": //用户id
if len(attr.Values) == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "用户id不存在")
}
apiRsp.UserId, err = strconv.ParseInt(attr.Values[0], 10, 64)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "用户id转数字失败")
}
case "sn": //用户真名
apiRsp.UserName = strings.Join(attr.Values, "")
case "mail": //邮箱
apiRsp.Email = strings.Join(attr.Values, "")
case "mobile": //手机号
apiRsp.Mobile = strings.Join(attr.Values, "")
case "postalAddress": //头像
apiRsp.Avatar = strings.Join(attr.Values, "")
case "postalCode": //状态
if len(attr.Values) == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "用户状态不存在")
}
apiRsp.Status, err = strconv.ParseInt(attr.Values[0], 10, 64)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "用户状态转数字失败")
}
}
}
break
}
if apiRsp.UserId == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "查询到的不是用户信息!!!")
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", apiRsp)
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetLdapUserInfoRsp{
UserId: user.UserId,
UserDN: user.UserDN,
UserName: user.UserName,
Email: user.Email,
Mobile: user.Mobile,
Avatar: user.Avatar,
Status: user.Status,
})
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理

View File

@@ -43,25 +43,35 @@ func (l *UpdateLdapUserLogic) UpdateLdapUser(req *types.UpdateLdapUserReq, useri
if req.AuthGroupId < 0 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误无效的用户权限组id")
}
if req.Password != "" {
//todo 验证下是不是本人
}
if len(req.UserDN) <= 3 || req.UserDN[:3] != "cn=" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误无效的用户DN")
}
//把用户名转pinyin
userNamePinyin := chinese_to_pinyin.ChineseToPinyin(req.UserName)
ldapServer := ldap_lib.NewLdap(l.svcCtx.Ldap, l.svcCtx.Config.Ldap.BaseDN, l.svcCtx.Config.Ldap.RootDN)
err := ldapServer.Update(req.UserDN, map[string][]string{
//更新的属性
attr := map[string][]string{
"homeDirectory": {"/home/users/" + userNamePinyin},
"departmentNumber": {fmt.Sprintf("%d", req.AuthGroupId)},
"sn": {req.UserName},
"uid": {userNamePinyin},
"mobile": {req.Mobile},
"userPassword": {req.Password},
"postalAddress": {req.Avatar},
"postalCode": {fmt.Sprintf("%d", req.Status)},
})
}
if req.Password != "" {
//查询个人信息
/*user, err := ldapServer.GetLdapUserInfo(req.UserDN)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
}*/
//验证旧的密码
//加密新的密码
//赋值属性
attr["userPassword"] = []string{""}
}
err := ldapServer.Update(req.UserDN, attr)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "更新用户失败,"+err.Error())