Refactor admin user filter query parameters (#18965) (#18975)

Only pass `status_filter` on admin page
Use a more general method to pass query parameters, remove hard-coded keys

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Otto Richter (fnetX) 2022-03-02 19:57:18 +01:00 committed by GitHub
parent f9b6404950
commit 578f19a682
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 11 deletions

View file

@ -20,6 +20,7 @@ import (
// SearchUserOptions contains the options for searching // SearchUserOptions contains the options for searching
type SearchUserOptions struct { type SearchUserOptions struct {
db.ListOptions db.ListOptions
Keyword string Keyword string
Type UserType Type UserType
UID int64 UID int64
@ -33,6 +34,8 @@ type SearchUserOptions struct {
IsRestricted util.OptionalBool IsRestricted util.OptionalBool
IsTwoFactorEnabled util.OptionalBool IsTwoFactorEnabled util.OptionalBool
IsProhibitLogin util.OptionalBool IsProhibitLogin util.OptionalBool
ExtraParamStrings map[string]string
} }
func (opts *SearchUserOptions) toSearchQueryBase() *xorm.Session { func (opts *SearchUserOptions) toSearchQueryBase() *xorm.Session {

View file

@ -55,12 +55,3 @@ func (p *Pagination) SetDefaultParams(ctx *Context) {
p.AddParam(ctx, "tab", "TabName") p.AddParam(ctx, "tab", "TabName")
p.AddParam(ctx, "t", "queryType") p.AddParam(ctx, "t", "queryType")
} }
// SetUserFilterParams sets common pagination params for user filtering, e.g. the admin userlist
func (p *Pagination) SetUserFilterParams(ctx *Context) {
p.AddParamString("status_filter[is_active]", ctx.FormString("status_filter[is_active]"))
p.AddParamString("status_filter[is_admin]", ctx.FormString("status_filter[is_admin]"))
p.AddParamString("status_filter[is_restricted]", ctx.FormString("status_filter[is_restricted]"))
p.AddParamString("status_filter[is_2fa_enabled]", ctx.FormString("status_filter[is_2fa_enabled]"))
p.AddParamString("status_filter[is_prohibit_login]", ctx.FormString("status_filter[is_prohibit_login]"))
}

View file

@ -41,10 +41,16 @@ func Users(ctx *context.Context) {
ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminUsers"] = true ctx.Data["PageIsAdminUsers"] = true
extraParamStrings := map[string]string{}
statusFilterKeys := []string{"is_active", "is_admin", "is_restricted", "is_2fa_enabled", "is_prohibit_login"} statusFilterKeys := []string{"is_active", "is_admin", "is_restricted", "is_2fa_enabled", "is_prohibit_login"}
statusFilterMap := map[string]string{} statusFilterMap := map[string]string{}
for _, filterKey := range statusFilterKeys { for _, filterKey := range statusFilterKeys {
statusFilterMap[filterKey] = ctx.FormString("status_filter[" + filterKey + "]") paramKey := "status_filter[" + filterKey + "]"
paramVal := ctx.FormString(paramKey)
statusFilterMap[filterKey] = paramVal
if paramVal != "" {
extraParamStrings[paramKey] = paramVal
}
} }
sortType := ctx.FormString("sort") sortType := ctx.FormString("sort")
@ -68,6 +74,7 @@ func Users(ctx *context.Context) {
IsRestricted: util.OptionalBoolParse(statusFilterMap["is_restricted"]), IsRestricted: util.OptionalBoolParse(statusFilterMap["is_restricted"]),
IsTwoFactorEnabled: util.OptionalBoolParse(statusFilterMap["is_2fa_enabled"]), IsTwoFactorEnabled: util.OptionalBoolParse(statusFilterMap["is_2fa_enabled"]),
IsProhibitLogin: util.OptionalBoolParse(statusFilterMap["is_prohibit_login"]), IsProhibitLogin: util.OptionalBoolParse(statusFilterMap["is_prohibit_login"]),
ExtraParamStrings: extraParamStrings,
}, tplUsers) }, tplUsers)
} }

View file

@ -84,7 +84,9 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions,
pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5) pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
pager.SetDefaultParams(ctx) pager.SetDefaultParams(ctx)
pager.SetUserFilterParams(ctx) for paramKey, paramVal := range opts.ExtraParamStrings {
pager.AddParamString(paramKey, paramVal)
}
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplName) ctx.HTML(http.StatusOK, tplName)