Skip to content

Commit cb45a28

Browse files
zeripathguillep2klunny
committed
Add option to prevent LDAP from deactivating everything on empty search (go-gitea#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 f96c1a2 commit cb45a28

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
@@ -1760,6 +1760,15 @@ func SyncExternalUsers(ctx context.Context) {
17601760
continue
17611761
}
17621762

1763+
if len(sr) == 0 {
1764+
if !s.LDAP().AllowDeactivateAll {
1765+
log.Error("LDAP search found no entries but did not report an error. Refusing to deactivate all users")
1766+
continue
1767+
} else {
1768+
log.Warn("LDAP search found no entries but did not report an error. All users will be deactivated as per settings")
1769+
}
1770+
}
1771+
17631772
for _, su := range sr {
17641773
select {
17651774
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
@@ -1826,6 +1826,7 @@ auths.attribute_surname = Surname Attribute
18261826
auths.attribute_mail = Email Attribute
18271827
auths.attribute_ssh_public_key = Public SSH Key Attribute
18281828
auths.attributes_in_bind = Fetch Attributes in Bind DN Context
1829+
auths.allow_deactivate_all = Allow an empty search result to deactivate all users
18291830
auths.use_paged_search = Use Paged Search
18301831
auths.search_page_size = Page Size
18311832
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)