fix
This commit is contained in:
@@ -54,7 +54,7 @@ func (l *Ldap) Delete(DN string) error {
|
||||
// 查询资源(分组/用户)
|
||||
func (l *Ldap) Search(DN string, scope int, filter string, attr []string, controls []ldap.Control) (resp *ldap.SearchResult, err error) {
|
||||
if DN == l.rootDN {
|
||||
return nil, errors.New("你没有权限查询根用户")
|
||||
return nil, errors.New("没有权限查询根用户")
|
||||
}
|
||||
if filter == "" {
|
||||
rootCn := strings.Split(l.rootDN, ",")
|
||||
|
||||
73
utils/ldap_lib/ldap_user.go
Normal file
73
utils/ldap_lib/ldap_user.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package ldap_lib
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/go-ldap/ldap/v3"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type GetLdapUserInfoRsp 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"` //头像地址
|
||||
Status int64 `json:"status,options=0|1"` //状态 1正常0离职
|
||||
}
|
||||
|
||||
func (l *Ldap) GetLdapUserInfo(userDN string) (*GetLdapUserInfoRsp, error) {
|
||||
res, err := l.Search(userDN, ldap.ScopeWholeSubtree, "(&(objectClass=posixAccount)(objectClass=inetOrgPerson))", nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(res.Entries) != 1 {
|
||||
return nil, errors.New("查询到不到用户信息")
|
||||
}
|
||||
user := &GetLdapUserInfoRsp{}
|
||||
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 {
|
||||
continue
|
||||
}
|
||||
user.UserId, err = strconv.ParseInt(attr.Values[0], 10, 64)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return nil, errors.New("用户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 "userPassword": //密码
|
||||
user.Password = strings.Join(attr.Values, ",")
|
||||
case "postalCode": //状态
|
||||
if len(attr.Values) == 0 {
|
||||
continue
|
||||
}
|
||||
user.Status, err = strconv.ParseInt(attr.Values[0], 10, 64)
|
||||
if err != nil {
|
||||
return nil, errors.New("用户状态转数字失败")
|
||||
}
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
if user.UserId == 0 {
|
||||
return nil, errors.New("查询到的不是用户信息!!!")
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
Reference in New Issue
Block a user