This commit is contained in:
laodaming 2023-11-21 11:39:03 +08:00
parent 06b826769b
commit 297dffcbc0
8 changed files with 152 additions and 85 deletions

View File

@ -6,7 +6,6 @@ import (
"fusenapi/utils/basic"
"fusenapi/utils/ldap_lib"
"github.com/go-ldap/ldap/v3"
"strconv"
"strings"
"context"
@ -84,72 +83,28 @@ func (l *GetLdapOrganizationMembersLogic) GetLdapOrganizationMembers(req *types.
//从新赋值filter
filter = "(&(objectClass=posixAccount)(objectClass=inetOrgPerson)(|" + filterBuilder.String() + "))"
//从用户基本组中找到员工
result, err = ldapServer.Search(l.svcCtx.Config.Ldap.PeopleGroupDN, ldap.ScopeWholeSubtree, filter, nil, nil)
userList, err := ldapServer.GetLdapBaseTeamUsersByParams(filter)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "查询ldap帐号信息失败,"+err.Error())
}
userList := make([]types.GetLdapOrganizationMembersItem, 0, memberCount)
for _, entry := range result.Entries {
user := types.GetLdapOrganizationMembersItem{
UserDN: entry.DN,
}
canAppend := true
for _, attr := range entry.Attributes {
switch attr.Name {
case "uidNumber": //用户id
if len(attr.Values) == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "用户id不存在")
}
user.UserId, err = strconv.ParseInt(attr.Values[0], 10, 64)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "用户id转数字失败")
}
case "sn": //用户真名
user.UserName = strings.Join(attr.Values, "")
case "mail": //邮箱
user.Email = strings.Join(attr.Values, "")
case "mobile": //手机号
user.Mobile = strings.Join(attr.Values, "")
case "postalAddress": //头像
user.Avatar = strings.Join(attr.Values, "")
case "employeeType": //人员类型
if len(attr.Values) == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "用户类型不存在")
}
user.EmployeeType, err = strconv.ParseInt(attr.Values[0], 10, 64)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "用户类型转数字失败")
}
case "postalCode": //状态
if len(attr.Values) == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "用户状态不存在")
}
user.Status, err = strconv.ParseInt(attr.Values[0], 10, 64)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "用户状态转数字失败")
}
//无效员工就不要显示了
if user.Status != 1 {
//从该组中移除该成员
if err = ldapServer.RemoveUserFromOrganization(req.OrganizationDN, entry.DN); err != nil {
logx.Error("移除组中离职成员失败,", err.Error())
}
canAppend = false //要移除的成员就不要显示了
break
}
}
}
//添加列表
if canAppend {
userList = append(userList, user)
}
list := make([]types.GetLdapOrganizationMembersItem, 0, memberCount)
for _, user := range userList {
list = append(list, types.GetLdapOrganizationMembersItem{
UserId: user.UserId,
UserDN: user.UserDN,
UserName: user.UserName,
Email: user.Email,
Mobile: user.Mobile,
Avatar: user.Avatar,
EmployeeType: user.EmployeeType,
Status: user.Status,
CreateTime: user.CreateTime.Format("2006-01-02 15:04:05"),
UpdateTime: user.UpdateTime.Format("2006-01-02 15:04:05"),
})
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetLdapOrganizationMembersRsp{
List: userList,
List: list,
})
}

View File

@ -54,6 +54,8 @@ func (l *GetLdapUserInfoLogic) GetLdapUserInfo(req *types.GetLdapUserInfoReq, us
Avatar: user.Avatar,
Status: user.Status,
EmployeeTpye: user.EmployeeType,
CreateTime: user.CreateTime.Format("2006-01-02 15:04:05"),
UpdateTime: user.UpdateTime.Format("2006-01-02 15:04:05"),
})
}

View File

@ -52,6 +52,8 @@ func (l *GetLdapUsersLogic) GetLdapUsers(req *types.GetLdapUsersReq, userinfo *a
Avatar: v.Avatar,
EmployeeType: v.EmployeeType,
Status: v.Status,
CreateTime: v.CreateTime.Format("2006-01-02 15:04:05"),
UpdateTime: v.UpdateTime.Format("2006-01-02 15:04:05"),
})
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetLdapUsersRsp{

View File

@ -174,6 +174,8 @@ type GetLdapUserInfoRsp struct {
Avatar string `json:"avatar"` //头像地址
EmployeeTpye int64 `json:"employee_tpye"` //雇佣类型 1正式 2实习 3外包
Status int64 `json:"status,options=0|1"` //状态 1正常0离职
CreateTime string `json:"create_time"`
UpdateTime string `json:"update_time"`
}
type AddLdapOrganizationMemberReq struct {
@ -203,6 +205,8 @@ type GetLdapOrganizationMembersItem struct {
Avatar string `json:"avatar"` //头像地址
EmployeeType int64 `json:"employee_type"`
Status int64 `json:"status,options=0|1"` //状态 1正常0离职
CreateTime string `json:"create_time"`
UpdateTime string `json:"update_time"`
}
type GetLdapUsersReq struct {
@ -223,6 +227,8 @@ type GetLdapUsersItem struct {
Avatar string `json:"avatar"` //头像地址
EmployeeType int64 `json:"employee_type"`
Status int64 `json:"status,options=0|1"` //状态 1正常0离职
CreateTime string `json:"create_time"`
UpdateTime string `json:"update_time"`
}
type Request struct {

View File

@ -257,6 +257,8 @@ type GetLdapUserInfoRsp {
Avatar string `json:"avatar"` //头像地址
EmployeeTpye int64 `json:"employee_tpye"` //雇佣类型 1正式 2实习 3外包
Status int64 `json:"status,options=0|1"` //状态 1正常0离职
CreateTime string `json:"create_time"`
UpdateTime string `json:"update_time"`
}
//ldap组织添加成员
type AddLdapOrganizationMemberReq {
@ -284,6 +286,8 @@ type GetLdapOrganizationMembersItem {
Avatar string `json:"avatar"` //头像地址
EmployeeType int64 `json:"employee_type"`
Status int64 `json:"status,options=0|1"` //状态 1正常0离职
CreateTime string `json:"create_time"`
UpdateTime string `json:"update_time"`
}
//获取基础用户组中成员列表
type GetLdapUsersReq {
@ -302,4 +306,6 @@ type GetLdapUsersItem {
Avatar string `json:"avatar"` //头像地址
EmployeeType int64 `json:"employee_type"`
Status int64 `json:"status,options=0|1"` //状态 1正常0离职
CreateTime string `json:"create_time"`
UpdateTime string `json:"update_time"`
}

View File

@ -104,6 +104,8 @@ func (l *Ldap) SearchWithPaging(DN string, scope int, filter string, attr []stri
return l.conn.Search(searchRequest)
}
//*********************************************************************************************
// AddUserToGroup 添加用户到组织
func (l *Ldap) AddUserToOrganization(organizationDN, userDN string) error {
modify := ldap.NewModifyRequest(organizationDN, nil)

View File

@ -0,0 +1,10 @@
package ldap_lib
import (
"time"
)
func LdapTimeToTime(timeStr string) (time.Time, error) {
// 将时间字符串转换为时间
return time.Parse("20060102150405Z", timeStr)
}

View File

@ -4,21 +4,23 @@ import (
"encoding/hex"
"errors"
"github.com/go-ldap/ldap/v3"
"github.com/zeromicro/go-zero/core/logx"
"strconv"
"strings"
"time"
)
type LdapUserInfo struct {
UserId int64 `json:"userId"`
UserDN string `json:"user_dn"`
UserName string `json:"user_name"` //用户名
Password string `json:"password"` //密码
Email string `json:"email"` //邮箱
Mobile string `json:"mobile"` //手机号
Avatar string `json:"avatar"` //头像地址
EmployeeType int64 `json:"employee_type"` //1正式 2实习 3外包
Status int64 `json:"status,options=0|1"` //状态 1正常0离职
UserId int64 `json:"userId"`
UserDN string `json:"user_dn"`
UserName string `json:"user_name"` //用户名
Password string `json:"password"` //密码
Email string `json:"email"` //邮箱
Mobile string `json:"mobile"` //手机号
Avatar string `json:"avatar"` //头像地址
EmployeeType int64 `json:"employee_type"` //1正式 2实习 3外包
Status int64 `json:"status,options=0|1"` //状态 1正常0离职
CreateTime time.Time `json:"create_time"`
UpdateTime time.Time `json:"update_time"`
}
// 获取用户详情
@ -40,12 +42,11 @@ func (l *Ldap) GetLdapUserInfo(userDN string) (*LdapUserInfo, error) {
switch attr.Name {
case "uidNumber": //用户id
if len(attr.Values) == 0 {
continue
return nil, errors.New("用户id不存在")
}
user.UserId, err = strconv.ParseInt(attr.Values[0], 10, 64)
if err != nil {
logx.Error(err)
return nil, errors.New("用户id转数字失败")
return nil, err
}
case "sn": //用户真名
user.UserName = strings.Join(attr.Values, "")
@ -59,19 +60,29 @@ func (l *Ldap) GetLdapUserInfo(userDN string) (*LdapUserInfo, error) {
user.Password = strings.Join(attr.Values, ",")
case "employeeType": //员工类型
if len(attr.Values) == 0 {
continue
return nil, errors.New("用户类型不存在")
}
user.EmployeeType, err = strconv.ParseInt(attr.Values[0], 10, 64)
if err != nil {
return nil, errors.New("用户类型转数字失败")
return nil, err
}
case "postalCode": //状态
if len(attr.Values) == 0 {
continue
return nil, errors.New("用户状态不存在")
}
user.Status, err = strconv.ParseInt(attr.Values[0], 10, 64)
if err != nil {
return nil, errors.New("用户状态转数字失败")
return nil, err
}
case "createTimestamp":
user.CreateTime, err = LdapTimeToTime(attr.Values[0])
if err != nil {
return nil, err
}
case "modifyTimestamp":
user.UpdateTime, err = LdapTimeToTime(attr.Values[0])
if err != nil {
return nil, err
}
}
}
@ -102,12 +113,11 @@ func (l *Ldap) GetLdapBaseTeamUserList(pageSize uint32, pageCookie string) ([]Ld
switch attr.Name {
case "uidNumber": //用户id
if len(attr.Values) == 0 {
continue
return nil, "", errors.New("用户id不存在")
}
user.UserId, err = strconv.ParseInt(attr.Values[0], 10, 64)
if err != nil {
logx.Error(err)
return nil, "", errors.New("用户id转数字失败")
return nil, "", err
}
case "sn": //用户真名
user.UserName = strings.Join(attr.Values, "")
@ -121,19 +131,29 @@ func (l *Ldap) GetLdapBaseTeamUserList(pageSize uint32, pageCookie string) ([]Ld
user.Password = strings.Join(attr.Values, ",")
case "employeeType": //员工类型
if len(attr.Values) == 0 {
continue
return nil, "", errors.New("用户类型不存在")
}
user.EmployeeType, err = strconv.ParseInt(attr.Values[0], 10, 64)
if err != nil {
return nil, "", errors.New("用户类型转数字失败")
return nil, "", err
}
case "postalCode": //状态
if len(attr.Values) == 0 {
continue
return nil, "", errors.New("用户状态不存在")
}
user.Status, err = strconv.ParseInt(attr.Values[0], 10, 64)
if err != nil {
return nil, "", errors.New("用户状态转数字失败")
return nil, "", err
}
case "createTimestamp":
user.CreateTime, err = LdapTimeToTime(attr.Values[0])
if err != nil {
return nil, "", err
}
case "modifyTimestamp":
user.UpdateTime, err = LdapTimeToTime(attr.Values[0])
if err != nil {
return nil, "", err
}
}
}
@ -150,3 +170,67 @@ func (l *Ldap) GetLdapBaseTeamUserList(pageSize uint32, pageCookie string) ([]Ld
}
return list, rspCookie, nil
}
// 从基础用户组中获取指定一批用户
func (l *Ldap) GetLdapBaseTeamUsersByParams(filter string) ([]LdapUserInfo, error) {
result, err := l.Search(l.peopleGroupDN, ldap.ScopeWholeSubtree, filter, nil, nil)
if err != nil {
return nil, err
}
list := make([]LdapUserInfo, 0, len(result.Entries))
for _, entry := range result.Entries {
user := LdapUserInfo{
UserDN: entry.DN,
}
for _, attr := range entry.Attributes {
switch attr.Name {
case "uidNumber": //用户id
if len(attr.Values) == 0 {
return nil, errors.New("用户id不存在")
}
user.UserId, err = strconv.ParseInt(attr.Values[0], 10, 64)
if err != nil {
return nil, err
}
case "sn": //用户真名
user.UserName = strings.Join(attr.Values, "")
case "mail": //邮箱
user.Email = strings.Join(attr.Values, "")
case "mobile": //手机号
user.Mobile = strings.Join(attr.Values, "")
case "postalAddress": //头像
user.Avatar = strings.Join(attr.Values, "")
case "userPassword": //密码
user.Password = strings.Join(attr.Values, ",")
case "employeeType": //员工类型
if len(attr.Values) == 0 {
return nil, errors.New("用户类型不存在")
}
user.EmployeeType, err = strconv.ParseInt(attr.Values[0], 10, 64)
if err != nil {
return nil, err
}
case "postalCode": //状态
if len(attr.Values) == 0 {
return nil, errors.New("用户状态不存在")
}
user.Status, err = strconv.ParseInt(attr.Values[0], 10, 64)
if err != nil {
return nil, err
}
case "createTimestamp":
user.CreateTime, err = LdapTimeToTime(attr.Values[0])
if err != nil {
return nil, err
}
case "modifyTimestamp":
user.UpdateTime, err = LdapTimeToTime(attr.Values[0])
if err != nil {
return nil, err
}
}
}
list = append(list, user)
}
return list, nil
}