Skip to content

Commit 501fb22

Browse files
authored
Add option to use paged LDAP search when synchronizing users (#3895)
1 parent 7467ff3 commit 501fb22

File tree

7 files changed

+59
-2
lines changed

7 files changed

+59
-2
lines changed

modules/auth/auth_form.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type AuthenticationForm struct {
2525
AttributeSurname string
2626
AttributeMail string
2727
AttributesInBind bool
28+
UsePagedSearch bool
29+
SearchPageSize int
2830
Filter string
2931
AdminFilter string
3032
IsActive bool

modules/auth/ldap/ldap.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Source struct {
4242
AttributeSurname string // Surname attribute
4343
AttributeMail string // E-mail attribute
4444
AttributesInBind bool // fetch attributes in bind context (not user)
45+
SearchPageSize uint32 // Search with paging page size
4546
Filter string // Query filter to validate entry
4647
AdminFilter string // Query filter to check if user is admin
4748
Enabled bool // if this source is disabled
@@ -269,6 +270,11 @@ func (ls *Source) SearchEntry(name, passwd string, directBind bool) *SearchResul
269270
}
270271
}
271272

273+
// UsePagedSearch returns if need to use paged search
274+
func (ls *Source) UsePagedSearch() bool {
275+
return ls.SearchPageSize > 0
276+
}
277+
272278
// SearchEntries : search an LDAP source for all users matching userFilter
273279
func (ls *Source) SearchEntries() []*SearchResult {
274280
l, err := dial(ls)
@@ -298,7 +304,12 @@ func (ls *Source) SearchEntries() []*SearchResult {
298304
[]string{ls.AttributeUsername, ls.AttributeName, ls.AttributeSurname, ls.AttributeMail},
299305
nil)
300306

301-
sr, err := l.Search(search)
307+
var sr *ldap.SearchResult
308+
if ls.UsePagedSearch() {
309+
sr, err = l.SearchWithPaging(search, ls.SearchPageSize)
310+
} else {
311+
sr, err = l.Search(search)
312+
}
302313
if err != nil {
303314
log.Error(4, "LDAP Search failed unexpectedly! (%v)", err)
304315
return nil

options/locale/locale_en-US.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,8 @@ auths.attribute_name = First Name Attribute
13521352
auths.attribute_surname = Surname Attribute
13531353
auths.attribute_mail = Email Attribute
13541354
auths.attributes_in_bind = Fetch Attributes in Bind DN Context
1355+
auths.use_paged_search = Use paged search
1356+
auths.search_page_size = Page size
13551357
auths.filter = User Filter
13561358
auths.admin_filter = Admin Filter
13571359
auths.ms_ad_sa = MS AD Search Attributes

public/js/index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,16 @@ function initAdmin() {
11381138
}
11391139
}
11401140

1141+
function onUsePagedSearchChange() {
1142+
if ($('#use_paged_search').prop('checked')) {
1143+
$('.search-page-size').show()
1144+
.find('input').attr('required', 'required');
1145+
} else {
1146+
$('.search-page-size').hide()
1147+
.find('input').removeAttr('required');
1148+
}
1149+
}
1150+
11411151
function onOAuth2Change() {
11421152
$('.open_id_connect_auto_discovery_url, .oauth2_use_custom_url').hide();
11431153
$('.open_id_connect_auto_discovery_url input[required]').removeAttr('required');
@@ -1191,7 +1201,7 @@ function initAdmin() {
11911201
// New authentication
11921202
if ($('.admin.new.authentication').length > 0) {
11931203
$('#auth_type').change(function () {
1194-
$('.ldap, .dldap, .smtp, .pam, .oauth2, .has-tls').hide();
1204+
$('.ldap, .dldap, .smtp, .pam, .oauth2, .has-tls .search-page-size').hide();
11951205

11961206
$('.ldap input[required], .dldap input[required], .smtp input[required], .pam input[required], .oauth2 input[required], .has-tls input[required]').removeAttr('required');
11971207

@@ -1223,9 +1233,13 @@ function initAdmin() {
12231233
if (authType == '2' || authType == '5') {
12241234
onSecurityProtocolChange()
12251235
}
1236+
if (authType == '2') {
1237+
onUsePagedSearchChange();
1238+
}
12261239
});
12271240
$('#auth_type').change();
12281241
$('#security_protocol').change(onSecurityProtocolChange);
1242+
$('#use_paged_search').change(onUsePagedSearchChange);
12291243
$('#oauth2_provider').change(onOAuth2Change);
12301244
$('#oauth2_use_custom_url').change(onOAuth2UseCustomURLChange);
12311245
}
@@ -1234,6 +1248,9 @@ function initAdmin() {
12341248
var authType = $('#auth_type').val();
12351249
if (authType == '2' || authType == '5') {
12361250
$('#security_protocol').change(onSecurityProtocolChange);
1251+
if (authType == '2') {
1252+
$('#use_paged_search').change(onUsePagedSearchChange);
1253+
}
12371254
} else if (authType == '6') {
12381255
$('#oauth2_provider').change(onOAuth2Change);
12391256
$('#oauth2_use_custom_url').change(onOAuth2UseCustomURLChange);

routers/admin/auths.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ func NewAuthSource(ctx *context.Context) {
9191
}
9292

9393
func parseLDAPConfig(form auth.AuthenticationForm) *models.LDAPConfig {
94+
var pageSize uint32
95+
if form.UsePagedSearch {
96+
pageSize = uint32(form.SearchPageSize)
97+
}
9498
return &models.LDAPConfig{
9599
Source: &ldap.Source{
96100
Name: form.Name,
@@ -107,6 +111,7 @@ func parseLDAPConfig(form auth.AuthenticationForm) *models.LDAPConfig {
107111
AttributeSurname: form.AttributeSurname,
108112
AttributeMail: form.AttributeMail,
109113
AttributesInBind: form.AttributesInBind,
114+
SearchPageSize: pageSize,
110115
Filter: form.Filter,
111116
AdminFilter: form.AdminFilter,
112117
Enabled: true,

templates/admin/auth/edit.tmpl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@
9191
<input id="attribute_mail" name="attribute_mail" value="{{$cfg.AttributeMail}}" placeholder="e.g. mail" required>
9292
</div>
9393
{{if .Source.IsLDAP}}
94+
<div class="inline field">
95+
<div class="ui checkbox">
96+
<label for="use_paged_search"><strong>{{.i18n.Tr "admin.auths.use_paged_search"}}</strong></label>
97+
<input id="use_paged_search" name="use_paged_search" type="checkbox" {{if $cfg.UsePagedSearch}}checked{{end}}>
98+
</div>
99+
</div>
100+
<div class="field required search-page-size{{if not $cfg.UsePagedSearch}} hide{{end}}">
101+
<label for="search_page_size">{{.i18n.Tr "admin.auths.search_page_size"}}</label>
102+
<input id="search_page_size" name="search_page_size" value="{{if $cfg.UsePagedSearch}}{{$cfg.SearchPageSize}}{{end}}">
103+
</div>
94104
<div class="inline field">
95105
<div class="ui checkbox">
96106
<label><strong>{{.i18n.Tr "admin.auths.attributes_in_bind"}}</strong></label>

templates/admin/auth/source/ldap.tmpl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,14 @@
6262
<label for="attribute_mail">{{.i18n.Tr "admin.auths.attribute_mail"}}</label>
6363
<input id="attribute_mail" name="attribute_mail" value="{{.attribute_mail}}" placeholder="e.g. mail">
6464
</div>
65+
<div class="ldap inline field {{if not (eq .type 2)}}hide{{end}}">
66+
<div class="ui checkbox">
67+
<label for="use_paged_search"><strong>{{.i18n.Tr "admin.auths.use_paged_search"}}</strong></label>
68+
<input id="use_paged_search" name="use_paged_search" class="use-paged-search" type="checkbox" {{if .use_paged_search}}checked{{end}}>
69+
</div>
70+
</div>
71+
<div class="ldap field search-page-size required {{if or (not (eq .type 2)) (not .use_paged_search)}}hide{{end}}">
72+
<label for="search_page_size">{{.i18n.Tr "admin.auths.search_page_size"}}</label>
73+
<input id="search_page_size" name="search_page_size" value="{{.search_page_size}}">
74+
</div>
6575
</div>

0 commit comments

Comments
 (0)