diff --git a/app/services/auth_login_ldap.go b/app/services/auth_login_ldap.go index 6f84a556..9eaefe76 100644 --- a/app/services/auth_login_ldap.go +++ b/app/services/auth_login_ldap.go @@ -8,23 +8,28 @@ import ( "github.com/go-ldap/ldap/v3" ) +const ( + LdapDefaultAccountPattern = "(&(objectClass=User)(userPrincipalName=%s))" + LdapDefaultAttributeGivenName = "displayName" +) + var ( LdapUserSearchNotFoundErr = errors.New("用户不存在或密码错误") ) type AuthLoginConfig struct { - BaseDn string `json:"basedn"` - BindUsername string `json:"bind_username"` - BindPassword string `json:"bind_password"` - AccountPattern string `json:"account_pattern"` - GivenNameKey string `json:"given_name_key"` - EmailKey string `json:"email_key"` - MobileKey string `json:"mobile_key"` - PhoneKey string `json:"phone_key"` - DepartmentKey string `json:"department_key"` - PositionKey string `json:"position_key"` - LocationKey string `json:"location_key"` - ImKey string `json:"im_key"` + BaseDn string `json:"basedn"` + BindUsername string `json:"bind_username"` + BindPassword string `json:"bind_password"` + AccountPattern string `json:"account_pattern"` + GivenNameKey string `json:"given_name_key"` + EmailKey string `json:"email_key"` + MobileKey string `json:"mobile_key"` + PhoneKey string `json:"phone_key"` + DepartmentKey string `json:"department_key"` + PositionKey string `json:"position_key"` + LocationKey string `json:"location_key"` + ImKey string `json:"im_key"` } // AuthLoginLdapService ldap auth login @@ -49,6 +54,12 @@ func (al *AuthLoginLdapService) InitConf(url string, conf string) error { return err } al.config = authLoginConfig + if al.config.AccountPattern == "" { + al.config.AccountPattern = LdapDefaultAccountPattern + } + if al.config.GivenNameKey == "" { + al.config.GivenNameKey = LdapDefaultAttributeGivenName + } return nil } @@ -82,10 +93,6 @@ func (al *AuthLoginLdapService) AuthLogin(username string, password string) (*Au } // 搜索下用户信息 - accountPattern := "(&(objectClass=User)(userPrincipalName=%s))" - if al.config.AccountPattern != "" { - accountPattern = al.config.AccountPattern - } searchRequest := ldap.NewSearchRequest( al.config.BaseDn, ldap.ScopeWholeSubtree, @@ -93,7 +100,7 @@ func (al *AuthLoginLdapService) AuthLogin(username string, password string) (*Au 0, 0, false, - fmt.Sprintf(accountPattern, username), + fmt.Sprintf(al.config.AccountPattern, username), al.GetAttributes(), nil, ) diff --git a/views/system/auth/doc.html b/views/system/auth/doc.html index c79a76a1..96ba0cf5 100644 --- a/views/system/auth/doc.html +++ b/views/system/auth/doc.html @@ -34,14 +34,14 @@

登录认证方式配置文档

"bind_username": "readonly", // ldap bind dn; 用来获取查询权限的 bind 用户;非必填可以为空 "bind_password": "password", // ldap bind dn password; bind 用户密码;非必填可以为空 "account_pattern": "(&(objectClass=User)(userPrincipalName=%s))" // ldap search pattern; 非必填可以为空,默认值为(&(objectClass=User)(userPrincipalName=%s)) - "given_name_key": "displayName", // ldap 查询用户名对应的 key,必填 - "email_key": "mail", // ldap 查询邮箱对应的 key, 没有可为空 - "mobile_key": "mobile", // ldap 查询手机号对应的 key,没有可为空 - "phone_key": "telephoneNumber", // ldap 查询电话对应的 key,没有可为空 - "department_key": "department", // ldap 查询部门对应的 key,没有可为空 - "position_key": "Position", // ldap 查询职位对应的 key,没有可为空 - "location_key": "physicalDeliveryOfficeName", // ldap 查询位置对应的 key,没有可为空 - "im_key": "im" // ldap 查询 im 信息对应的 key,没有可为空 + "given_name_key": "displayName", // ldap 查询用户名对应的 key,非必填可以为空,默认为 displayName + "email_key": "mail", // ldap 查询邮箱对应的 key, 非必填可以为空 + "mobile_key": "mobile", // ldap 查询手机号对应的 key,非必填可以为空 + "phone_key": "telephoneNumber", // ldap 查询电话对应的 key,非必填可以为空 + "department_key": "department", // ldap 查询部门对应的 key,非必填可以为空 + "position_key": "Position", // ldap 查询职位对应的 key,非必填可以为空 + "location_key": "physicalDeliveryOfficeName", // ldap 查询位置对应的 key,非必填可以为空 + "im_key": "im" // ldap 查询 im 信息对应的 key,非必填可以为空 } 示例:{"basedn":"dc=umich,dc=edu","bind_username":"readonly","bind_password":"password","given_name_key":"displayName","email_key":"mail","mobile_key":"mobile","phone_key":"telephoneNumber","department_key":"department","position_key":"Position","location_key":"physicalDeliveryOfficeName","im_key":"im"}