diff --git a/utils/ldap_lib/ldap_user.go b/utils/ldap_lib/ldap_user.go index 4cb4902e..3e61fe90 100644 --- a/utils/ldap_lib/ldap_user.go +++ b/utils/ldap_lib/ldap_user.go @@ -29,51 +29,53 @@ func (l *Ldap) GetLdapUserInfo(userDN string) (*LdapUserInfo, error) { if len(res.Entries) != 1 { return nil, errors.New("查询到不到用户信息") } - user := &LdapUserInfo{} - for _, entry := range res.Entries { - if entry.DN != userDN { - continue - } - user.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 - } + if len(res.Entries) == 0 { + return nil, errors.New("ldap user not exists(entry not exists)") + } + userEntry := res.Entries[0] + if userEntry.DN != userDN { + return nil, errors.New("ldap user not exists(DN not match)") + } + user := &LdapUserInfo{ + UserDN: userEntry.DN, + } + for _, attr := range userEntry.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 } } - break } if user.UserId == 0 { return nil, errors.New("查询到的不是用户信息!!!")