fix: return all users on a list when no search param was provided

This commit is contained in:
kolaente 2022-08-02 15:02:00 +02:00
parent 8f27e7e619
commit c51ee94ad1
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
3 changed files with 17 additions and 6 deletions

View file

@ -102,6 +102,9 @@ func ListUsersFromList(s *xorm.Session, l *List, search string) (users []*user.U
cond = builder.In("id", uids)
}
users, err = user.ListUsers(s, search, cond)
users, err = user.ListUsers(s, search, &user.ListUserOpts{
AdditionalCond: cond,
ReturnAllIfNoSearchProvided: true,
})
return
}

View file

@ -445,7 +445,7 @@ func TestListUsers(t *testing.T) {
s := db.NewSession()
defer s.Close()
all, err := ListUsers(s, "user7@example.com", builder.In("id", 7))
all, err := ListUsers(s, "user7@example.com", &ListUserOpts{AdditionalCond: builder.In("id", 7)})
assert.NoError(t, err)
assert.Len(t, all, 1)
assert.Equal(t, int64(7), all[0].ID)

View file

@ -23,13 +23,21 @@ import (
"xorm.io/xorm"
)
type ListUserOpts struct {
AdditionalCond builder.Cond
ReturnAllIfNoSearchProvided bool
}
// ListUsers returns a list with all users, filtered by an optional search string
func ListUsers(s *xorm.Session, search string, additionalCond builder.Cond) (users []*User, err error) {
func ListUsers(s *xorm.Session, search string, opts *ListUserOpts) (users []*User, err error) {
if opts == nil {
opts = &ListUserOpts{}
}
// Prevent searching for placeholders
search = strings.ReplaceAll(search, "%", "")
if search == "" || strings.ReplaceAll(search, " ", "") == "" {
if (search == "" || strings.ReplaceAll(search, " ", "") == "") && !opts.ReturnAllIfNoSearchProvided {
return
}
@ -45,10 +53,10 @@ func ListUsers(s *xorm.Session, search string, additionalCond builder.Cond) (use
),
)
if additionalCond != nil {
if opts.AdditionalCond != nil {
cond = builder.And(
cond,
additionalCond,
opts.AdditionalCond,
)
}