Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(admin): return pagination totals for user lists (#21540)

adwsf0ff22abb445
diff --git a/internal/admin/handler.go b/internal/admin/handler.go
index f8ffc2696d..f4e578ac17 100644
--- a/internal/admin/handler.go
+++ b/internal/admin/handler.go
@@ -210,28 +210,21 @@ func (h *Handler) ListUsers(c *gin.Context) {
ctx := c.Request.Context()
var users []map[string]interface{}
+ var total int64
switch common.GetRAGFlowType() {
case common.OpenSourceVersion:
- users, err = h.service.ListUsers(ctx, pageInt, pageSizeInt, name, status, sort, orderBy)
- if err != nil {
- common.ErrorWithCode(c, common.CodeServerError, err.Error())
- return
- }
-
- common.SuccessWithData(c, users, "List users")
- return
+ users, total, err = h.service.ListUsers(ctx, pageInt, pageSizeInt, name, status, sort, orderBy)
case common.EnterpriseEdition:
- users, err = h.service.ListUsersEE(ctx, pageInt, pageSizeInt, name, status, role, sort, orderBy, plan, topInt, daysInt, quotaPtr)
- if err != nil {
- common.ErrorWithCode(c, common.CodeServerError, err.Error())
- return
- }
- common.SuccessWithData(c, users, "List users")
- return
+ users, total, err = h.service.ListUsersEE(ctx, pageInt, pageSizeInt, name, status, role, sort, orderBy, plan, topInt, daysInt, quotaPtr)
default:
common.ErrorWithCode(c, common.CodeBadRequest, "Invalid RAGFlow type")
return
}
+ if err != nil {
+ common.ErrorWithCode(c, common.CodeServerError, err.Error())
+ return
+ }
+ common.SuccessWithDataAndTotal(c, users, total, "List users")
}
// CreateUserHTTPRequest create user request
diff --git a/internal/admin/service.go b/internal/admin/service.go
index b6171b440c..b08f81ce24 100644
--- a/internal/admin/service.go
+++ b/internal/admin/service.go
@@ -145,11 +145,11 @@ func generateRandomHex(n int) string {
return hex.EncodeToString(bytes)
}
-// ListUsers list all users
-func (s *Service) ListUsers(ctx context.Context, pageIndex, pageSize int, name, status, sort, orderBy string) ([]map[string]interface{}, error) {
- users, _, err := s.userDAO.List(ctx, dao.DB, (pageIndex-1)*pageSize, pageSize, name, status, sort, orderBy)
+// ListUsers returns a page of users and the total number of users.
+func (s *Service) ListUsers(ctx context.Context, pageIndex, pageSize int, name, status, sort, orderBy string) ([]map[string]interface{}, int64, error) {
+ users, total, err := s.userDAO.List(ctx, dao.DB, (pageIndex-1)*pageSize, pageSize, name, status, sort, orderBy)
if err != nil {
- return nil, err
+ return nil, 0, err
}
result := make([]map[string]interface{}, 0, len(users))
@@ -162,7 +162,7 @@ func (s *Service) ListUsers(ctx context.Context, pageIndex, pageSize int, name,
"is_superuser": user.IsSuperuser,
})
}
- return result, nil
+ return result, total, nil
}
// CreateUser create a new user
diff --git a/internal/admin/service_ee.go b/internal/admin/service_ee.go
index a0ecdcbda0..7846981b1c 100644
--- a/internal/admin/service_ee.go
+++ b/internal/admin/service_ee.go
@@ -803,7 +803,7 @@ func (s *Service) ShowUsersActivity(ctx context.Context, days, windows *int) (ma
return result, nil
}
-func (s *Service) ListUsersEE(ctx context.Context, pageIndex, pageSize int, name string, status, role, sort, orderBy, plan string, top, days int, quota *int) ([]map[string]interface{}, error) {
+func (s *Service) ListUsersEE(ctx context.Context, pageIndex, pageSize int, name string, status, role, sort, orderBy, plan string, top, days int, quota *int) ([]map[string]interface{}, int64, error) {
item := map[string]interface{}{}
item["pageIndex"] = pageIndex
item["pageSize"] = pageSize
@@ -823,7 +823,7 @@ func (s *Service) ListUsersEE(ctx context.Context, pageIndex, pageSize int, name
var result []map[string]interface{}
result = append(result, item)
- return result, nil
+ return result, 0, nil
}
// ListUsersReports list users reports for enterprise edition
diff --git a/internal/admin/user_detail_test.go b/internal/admin/user_detail_test.go
index 059e6933ef..940b475be2 100644
--- a/internal/admin/user_detail_test.go
+++ b/internal/admin/user_detail_test.go
@@ -18,12 +18,14 @@ package admin
import (
"encoding/json"
+ "fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"ragflow/internal/common"
+ "ragflow/internal/dao"
"ragflow/internal/entity"
"ragflow/internal/ingestion/testutil"
@@ -31,8 +33,59 @@ import (
)
type adminTestResponse struct {
- Code int `json:"code"`
- Data json.RawMessage `json:"data"`
+ Code int `json:"code"`
+ Data json.RawMessage `json:"data"`
+ Total *int64 `json:"total"`
+}
+
+func TestListUsersReturnsTotalForEachPage(t *testing.T) {
+ handler := setupAdminUserDetailTest(t)
+ for i := 0; i < 11; i++ {
+ user := entity.User{ID: fmt.Sprintf("user-%02d", i), Email: fmt.Sprintf("user-%[email protected]", i), Nickname: "User", IsActive: "1"}
+ if err := dao.DB.Create(&user).Error; err != nil {
+ t.Fatal(err)
+ }
+ }
+ for _, tt := range []struct {
+ query string
+ want int
+ }{
+ {"?page=1&page_size=10", 10},
+ {"?page=2&page_size=10", 2},
+ {"?page=3&page_size=10", 0},
+ } {
+ t.Run(tt.query, func(t *testing.T) {
+ recorder := httptest.NewRecorder()
+ ctx, _ := gin.CreateTestContext(recorder)
+ ctx.Request = httptest.NewRequest(http.MethodGet, "/api/v1/admin/users"+tt.query, nil)
+ handler.ListUsers(ctx)
+ var response adminTestResponse
+ if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
+ t.Fatal(err)
+ }
+ var users []map[string]interface{}
+ if err := json.Unmarshal(response.Data, &users); err != nil {
+ t.Fatal(err)
+ }
+ if recorder.Code != http.StatusOK || response.Code != int(common.CodeSuccess) || response.Total == nil || *response.Total != 12 || len(users) != tt.want {
+ t.Fatalf("unexpected response: %s", recorder.Body.String())
+ }
+ })
+ }
+ if err := dao.DB.Where("id <> ?", "").Delete(&entity.User{}).Error; err != nil {
+ t.Fatal(err)
+ }
+ recorder := httptest.NewRecorder()
+ ctx, _ := gin.CreateTestContext(recorder)
+ ctx.Request = httptest.NewRequest(http.MethodGet, "/api/v1/admin/users", nil)
+ handler.ListUsers(ctx)
+ var response adminTestResponse
+ if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
+ t.Fatal(err)
+ }
+ if response.Total == nil || *response.Total != 0 {
+ t.Fatalf("empty list missing total: %s", recorder.Body.String())
+ }
}
func setupAdminUserDetailTest(t *testing.T) *Handler {