Skip to content

Commit 68bca62

Browse files
dioss-Machielsapkzeripath
authored andcommitted
Prevent empty LDAP search from deactivating all users (#9879) (#9890)
* Backport of #9879 (Add option to prevent LDAP from deactivating everything on empty search) * go fmtted Co-authored-by: Antoine GIRARD <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent c4e0f71 commit 68bca62

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
@@ -1715,6 +1715,15 @@ func SyncExternalUsers() {
17151715
continue
17161716
}
17171717

1718+
if len(sr) == 0 {
1719+
if !s.LDAP().AllowDeactivateAll {
1720+
log.Error("LDAP search found no entries but did not report an error. Refusing to deactivate all users")
1721+
continue
1722+
} else {
1723+
log.Warn("LDAP search found no entries but did not report an error. All users will be deactivated as per settings")
1724+
}
1725+
}
1726+
17181727
for _, su := range sr {
17191728
if len(su.Username) == 0 {
17201729
continue

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
@@ -1700,6 +1700,7 @@ auths.attribute_surname = Surname Attribute
17001700
auths.attribute_mail = Email Attribute
17011701
auths.attribute_ssh_public_key = Public SSH Key Attribute
17021702
auths.attributes_in_bind = Fetch Attributes in Bind DN Context
1703+
auths.allow_deactivate_all = Allow an empty search result to deactivate all users
17031704
auths.use_paged_search = Use Paged Search
17041705
auths.search_page_size = Page Size
17051706
auths.filter = User Filter

routers/admin/auths.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func parseLDAPConfig(form auth.AuthenticationForm) *models.LDAPConfig {
115115
SearchPageSize: pageSize,
116116
Filter: form.Filter,
117117
AdminFilter: form.AdminFilter,
118+
AllowDeactivateAll: form.AllowDeactivateAll,
118119
Enabled: true,
119120
},
120121
}

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)