Skip to content

Commit c5014a7

Browse files
zeripathguillep2klunny
committed
Add option to prevent LDAP from deactivating everything on empty search (#9879)
* Add option to prevent LDAP from deactivating everything on empty search * Update options/locale/locale_en-US.ini Co-Authored-By: guillep2k <[email protected]> Co-authored-by: guillep2k <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 54c2854 commit c5014a7

File tree

7 files changed

+26
-0
lines changed

7 files changed

+26
-0
lines changed

cmd/admin_auth_ldap.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ var (
6161
Name: "admin-filter",
6262
Usage: "An LDAP filter specifying if a user should be given administrator privileges.",
6363
},
64+
cli.BoolFlag{
65+
Name: "allow-deactivate-all",
66+
Usage: "Allow empty search results to deactivate all users.",
67+
},
6468
cli.StringFlag{
6569
Name: "username-attribute",
6670
Usage: "The attribute of the user’s LDAP record containing the user name.",
@@ -231,6 +235,9 @@ func parseLdapConfig(c *cli.Context, config *models.LDAPConfig) error {
231235
if c.IsSet("admin-filter") {
232236
config.Source.AdminFilter = c.String("admin-filter")
233237
}
238+
if c.IsSet("allow-deactivate-all") {
239+
config.Source.AllowDeactivateAll = c.Bool("allow-deactivate-all")
240+
}
234241
return nil
235242
}
236243

models/user.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,15 @@ func SyncExternalUsers(ctx context.Context) {
17801780
continue
17811781
}
17821782

1783+
if len(sr) == 0 {
1784+
if !s.LDAP().AllowDeactivateAll {
1785+
log.Error("LDAP search found no entries but did not report an error. Refusing to deactivate all users")
1786+
continue
1787+
} else {
1788+
log.Warn("LDAP search found no entries but did not report an error. All users will be deactivated as per settings")
1789+
}
1790+
}
1791+
17831792
for _, su := range sr {
17841793
select {
17851794
case <-ctx.Done():

modules/auth/auth_form.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type AuthenticationForm struct {
3030
SearchPageSize int
3131
Filter string
3232
AdminFilter string
33+
AllowDeactivateAll bool
3334
IsActive bool
3435
IsSyncEnabled bool
3536
SMTPAuth string

modules/auth/ldap/ldap.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type Source struct {
4747
Filter string // Query filter to validate entry
4848
AdminFilter string // Query filter to check if user is admin
4949
Enabled bool // if this source is disabled
50+
AllowDeactivateAll bool // Allow an empty search response to deactivate all users from this source
5051
}
5152

5253
// SearchResult : user data

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,7 @@ auths.attribute_surname = Surname Attribute
18511851
auths.attribute_mail = Email Attribute
18521852
auths.attribute_ssh_public_key = Public SSH Key Attribute
18531853
auths.attributes_in_bind = Fetch Attributes in Bind DN Context
1854+
auths.allow_deactivate_all = Allow an empty search result to deactivate all users
18541855
auths.use_paged_search = Use Paged Search
18551856
auths.search_page_size = Page Size
18561857
auths.filter = User Filter

routers/admin/auths.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ func parseLDAPConfig(form auth.AuthenticationForm) *models.LDAPConfig {
130130
SearchPageSize: pageSize,
131131
Filter: form.Filter,
132132
AdminFilter: form.AdminFilter,
133+
AllowDeactivateAll: form.AllowDeactivateAll,
133134
Enabled: true,
134135
},
135136
}

templates/admin/auth/edit.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@
112112
</div>
113113
</div>
114114
{{end}}
115+
<div class="inline field">
116+
<div class="ui checkbox">
117+
<label for="allow_deactivate_all"><strong>{{.i18n.Tr "admin.auths.allow_deactivate_all"}}</strong></label>
118+
<input id="allow_deactivate_all" name="allow_deactivate_all" type="checkbox" {{if $cfg.AllowDeactivateAll}}checked{{end}}>
119+
</div>
120+
</div>
115121
{{end}}
116122

117123
<!-- SMTP -->

0 commit comments

Comments
 (0)