This commit is contained in:
laodaming 2023-11-22 13:13:12 +08:00
parent e5a73420b6
commit 79faa6a377

View File

@ -29,51 +29,53 @@ func (l *Ldap) GetLdapUserInfo(userDN string) (*LdapUserInfo, error) {
if len(res.Entries) != 1 { if len(res.Entries) != 1 {
return nil, errors.New("查询到不到用户信息") return nil, errors.New("查询到不到用户信息")
} }
user := &LdapUserInfo{} if len(res.Entries) == 0 {
for _, entry := range res.Entries { return nil, errors.New("ldap user not exists(entry not exists)")
if entry.DN != userDN { }
continue userEntry := res.Entries[0]
} if userEntry.DN != userDN {
user.UserDN = entry.DN return nil, errors.New("ldap user not exists(DN not match)")
for _, attr := range entry.Attributes { }
switch attr.Name { user := &LdapUserInfo{
case "uidNumber": //用户id UserDN: userEntry.DN,
if len(attr.Values) == 0 { }
return nil, errors.New("用户id不存在") for _, attr := range userEntry.Attributes {
} switch attr.Name {
user.UserId, err = strconv.ParseInt(attr.Values[0], 10, 64) case "uidNumber": //用户id
if err != nil { if len(attr.Values) == 0 {
return nil, err return nil, errors.New("用户id不存在")
} }
case "sn": //用户真名 user.UserId, err = strconv.ParseInt(attr.Values[0], 10, 64)
user.UserName = strings.Join(attr.Values, "") if err != nil {
case "mail": //邮箱 return nil, err
user.Email = strings.Join(attr.Values, "") }
case "mobile": //手机号 case "sn": //用户真名
user.Mobile = strings.Join(attr.Values, "") user.UserName = strings.Join(attr.Values, "")
case "postalAddress": //头像 case "mail": //邮箱
user.Avatar = strings.Join(attr.Values, "") user.Email = strings.Join(attr.Values, "")
case "userPassword": //密码 case "mobile": //手机号
user.Password = strings.Join(attr.Values, ",") user.Mobile = strings.Join(attr.Values, "")
case "employeeType": //员工类型 case "postalAddress": //头像
if len(attr.Values) == 0 { user.Avatar = strings.Join(attr.Values, "")
return nil, errors.New("用户类型不存在") case "userPassword": //密码
} user.Password = strings.Join(attr.Values, ",")
user.EmployeeType, err = strconv.ParseInt(attr.Values[0], 10, 64) case "employeeType": //员工类型
if err != nil { if len(attr.Values) == 0 {
return nil, err return nil, errors.New("用户类型不存在")
} }
case "postalCode": //状态 user.EmployeeType, err = strconv.ParseInt(attr.Values[0], 10, 64)
if len(attr.Values) == 0 { if err != nil {
return nil, errors.New("用户状态不存在") return nil, err
} }
user.Status, err = strconv.ParseInt(attr.Values[0], 10, 64) case "postalCode": //状态
if err != nil { if len(attr.Values) == 0 {
return nil, err return nil, errors.New("用户状态不存在")
} }
user.Status, err = strconv.ParseInt(attr.Values[0], 10, 64)
if err != nil {
return nil, err
} }
} }
break
} }
if user.UserId == 0 { if user.UserId == 0 {
return nil, errors.New("查询到的不是用户信息!!!") return nil, errors.New("查询到的不是用户信息!!!")