Skip to content

Commit 470c4a7

Browse files
authored
Merge pull request #233 from phachon/feature/v0.2
Feature/v0.2
2 parents 0b7bf57 + 08a021a commit 470c4a7

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

app/services/auth_login_ldap.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,28 @@ import (
88
"github.com/go-ldap/ldap/v3"
99
)
1010

11+
const (
12+
LdapDefaultAccountPattern = "(&(objectClass=User)(userPrincipalName=%s))"
13+
LdapDefaultAttributeGivenName = "displayName"
14+
)
15+
1116
var (
1217
LdapUserSearchNotFoundErr = errors.New("用户不存在或密码错误")
1318
)
1419

1520
type AuthLoginConfig struct {
16-
BaseDn string `json:"basedn"`
17-
BindUsername string `json:"bind_username"`
18-
BindPassword string `json:"bind_password"`
19-
AccountPattern string `json:"account_pattern"`
20-
GivenNameKey string `json:"given_name_key"`
21-
EmailKey string `json:"email_key"`
22-
MobileKey string `json:"mobile_key"`
23-
PhoneKey string `json:"phone_key"`
24-
DepartmentKey string `json:"department_key"`
25-
PositionKey string `json:"position_key"`
26-
LocationKey string `json:"location_key"`
27-
ImKey string `json:"im_key"`
21+
BaseDn string `json:"basedn"`
22+
BindUsername string `json:"bind_username"`
23+
BindPassword string `json:"bind_password"`
24+
AccountPattern string `json:"account_pattern"`
25+
GivenNameKey string `json:"given_name_key"`
26+
EmailKey string `json:"email_key"`
27+
MobileKey string `json:"mobile_key"`
28+
PhoneKey string `json:"phone_key"`
29+
DepartmentKey string `json:"department_key"`
30+
PositionKey string `json:"position_key"`
31+
LocationKey string `json:"location_key"`
32+
ImKey string `json:"im_key"`
2833
}
2934

3035
// AuthLoginLdapService ldap auth login
@@ -49,6 +54,12 @@ func (al *AuthLoginLdapService) InitConf(url string, conf string) error {
4954
return err
5055
}
5156
al.config = authLoginConfig
57+
if al.config.AccountPattern == "" {
58+
al.config.AccountPattern = LdapDefaultAccountPattern
59+
}
60+
if al.config.GivenNameKey == "" {
61+
al.config.GivenNameKey = LdapDefaultAttributeGivenName
62+
}
5263
return nil
5364
}
5465

@@ -82,18 +93,14 @@ func (al *AuthLoginLdapService) AuthLogin(username string, password string) (*Au
8293
}
8394

8495
// 搜索下用户信息
85-
accountPattern := "(&(objectClass=User)(userPrincipalName=%s))"
86-
if al.config.AccountPattern != "" {
87-
accountPattern = al.config.AccountPattern
88-
}
8996
searchRequest := ldap.NewSearchRequest(
9097
al.config.BaseDn,
9198
ldap.ScopeWholeSubtree,
9299
ldap.NeverDerefAliases,
93100
0,
94101
0,
95102
false,
96-
fmt.Sprintf(accountPattern, username),
103+
fmt.Sprintf(al.config.AccountPattern, username),
97104
al.GetAttributes(),
98105
nil,
99106
)

views/system/auth/doc.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ <h4><i class="fa fa-address-card-o"></i> 登录认证方式配置文档</h4>
3434
"bind_username": "readonly", // ldap bind dn; 用来获取查询权限的 bind 用户;非必填可以为空
3535
"bind_password": "password", // ldap bind dn password; bind 用户密码;非必填可以为空
3636
"account_pattern": "(&(objectClass=User)(userPrincipalName=%s))" // ldap search pattern; 非必填可以为空,默认值为(&(objectClass=User)(userPrincipalName=%s))
37-
"given_name_key": "displayName", // ldap 查询用户名对应的 key,必填
38-
"email_key": "mail", // ldap 查询邮箱对应的 key, 没有可为空
39-
"mobile_key": "mobile", // ldap 查询手机号对应的 key,没有可为空
40-
"phone_key": "telephoneNumber", // ldap 查询电话对应的 key,没有可为空
41-
"department_key": "department", // ldap 查询部门对应的 key,没有可为空
42-
"position_key": "Position", // ldap 查询职位对应的 key,没有可为空
43-
"location_key": "physicalDeliveryOfficeName", // ldap 查询位置对应的 key,没有可为空
44-
"im_key": "im" // ldap 查询 im 信息对应的 key,没有可为空
37+
"given_name_key": "displayName", // ldap 查询用户名对应的 key,非必填可以为空,默认为 displayName
38+
"email_key": "mail", // ldap 查询邮箱对应的 key, 非必填可以为空
39+
"mobile_key": "mobile", // ldap 查询手机号对应的 key,非必填可以为空
40+
"phone_key": "telephoneNumber", // ldap 查询电话对应的 key,非必填可以为空
41+
"department_key": "department", // ldap 查询部门对应的 key,非必填可以为空
42+
"position_key": "Position", // ldap 查询职位对应的 key,非必填可以为空
43+
"location_key": "physicalDeliveryOfficeName", // ldap 查询位置对应的 key,非必填可以为空
44+
"im_key": "im" // ldap 查询 im 信息对应的 key,非必填可以为空
4545
}
4646
</pre>
4747
示例:<code>{"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"}</code>

0 commit comments

Comments
 (0)