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

@@ -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
}