diff --git a/src/common/dao/dao_test.go b/src/common/dao/dao_test.go index 04e6dde23..fbd9f7bf6 100644 --- a/src/common/dao/dao_test.go +++ b/src/common/dao/dao_test.go @@ -318,6 +318,11 @@ func TestListUsers(t *testing.T) { if users2[0].Username != username { t.Errorf("The username in result list does not match, expected: %s, actual: %s", username, users2[0].Username) } + + users3, err := ListUsers(&models.UserQuery{Username: username, Pagination: &models.Pagination{Page: 2, Size: 1}}) + if len(users3) != 0 { + t.Errorf("Expect no user in list, but the acutal length is %d, the list: %+v", len(users3), users3) + } } func TestResetUserPassword(t *testing.T) { diff --git a/src/common/dao/user.go b/src/common/dao/user.go index f0317e5ff..9349c3477 100644 --- a/src/common/dao/user.go +++ b/src/common/dao/user.go @@ -106,10 +106,13 @@ func GetTotalOfUsers(query *models.UserQuery) (int64, error) { // ListUsers lists all users according to different conditions. func ListUsers(query *models.UserQuery) ([]models.User, error) { + qs := userQueryConditions(query) + if query != nil && query.Pagination != nil { + offset := (query.Pagination.Page - 1) * query.Pagination.Size + qs = qs.Offset(offset).Limit(query.Pagination.Size) + } users := []models.User{} - _, err := userQueryConditions(query).Limit(-1). - OrderBy("username"). - All(&users) + _, err := qs.OrderBy("username").All(&users) return users, err }