Skip to content

Commit faf11c0

Browse files
author
Cairry
committed
Merge branch 'master' of https://github.com/opsre/WatchAlert
2 parents 2d27977 + 5fd8db4 commit faf11c0

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

internal/models/settings.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ type LdapConfig struct {
5353
UserPrefix string `json:"userPrefix"`
5454
DefaultUserRole string `json:"defaultUserRole"`
5555
Cronjob string `json:"cronjob"`
56+
// Filter 用于限制允许登录的用户范围,例如: (&(objectClass=person)(memberOf=cn=jms,ou=groups,dc=test,dc=com))
57+
Filter string `json:"filter"`
5658
}
5759

5860
type OidcConfig struct {

internal/services/ldap.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ func (l ldapService) ListUsers() ([]ldapUser, error) {
7373
pages := 0
7474
pagingControl := ldap.NewControlPaging(pageSize)
7575

76+
listFilter := "(objectClass=person)"
77+
if l.ldapConfig.Filter != "" {
78+
listFilter = fmt.Sprintf("(&%s(objectClass=person))", l.ldapConfig.Filter)
79+
}
80+
7681
for {
7782
pages++
7883

@@ -82,7 +87,7 @@ func (l ldapService) ListUsers() ([]ldapUser, error) {
8287
ldap.ScopeWholeSubtree,
8388
ldap.NeverDerefAliases,
8489
0, 0, false,
85-
"(objectClass=person)",
90+
listFilter,
8691
[]string{"sAMAccountName", "cn", "mail", "mobile"},
8792
[]ldap.Control{pagingControl},
8893
)
@@ -190,12 +195,17 @@ func (l ldapService) Login(username, password string) error {
190195
defer auth.Close()
191196

192197
// 先搜索用户,获取真实的DN
198+
loginFilter := fmt.Sprintf("(sAMAccountName=%s)", ldap.EscapeFilter(username))
199+
if l.ldapConfig.Filter != "" {
200+
loginFilter = fmt.Sprintf("(&%s(sAMAccountName=%s))", l.ldapConfig.Filter, ldap.EscapeFilter(username))
201+
}
202+
193203
searchRequest := ldap.NewSearchRequest(
194204
l.ldapConfig.BaseDN,
195205
ldap.ScopeWholeSubtree,
196206
ldap.NeverDerefAliases,
197207
1, 0, false,
198-
fmt.Sprintf("(sAMAccountName=%s)", ldap.EscapeFilter(username)),
208+
loginFilter,
199209
[]string{"dn"},
200210
nil,
201211
)

0 commit comments

Comments
 (0)