From b9c5a3acc3ab0d410309e852bf87cd0f2659d4c2 Mon Sep 17 00:00:00 2001 From: zeripath Date: Mon, 15 Apr 2019 22:38:41 +0100 Subject: [PATCH] Return a UserList from /api/v1/admin/users (#6629) (#6642) --- integrations/api_admin_test.go | 23 +++++++++++++++++++++++ routers/api/v1/admin/user.go | 11 +++++++++-- routers/api/v1/convert/convert.go | 16 ++++++++++++++++ routers/api/v1/user/user.go | 13 ++----------- 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/integrations/api_admin_test.go b/integrations/api_admin_test.go index b8dded9c11..a7bbde4c53 100644 --- a/integrations/api_admin_test.go +++ b/integrations/api_admin_test.go @@ -106,3 +106,26 @@ func TestAPISudoUserForbidden(t *testing.T) { req := NewRequest(t, "GET", urlStr) session.MakeRequest(t, req, http.StatusForbidden) } + +func TestAPIListUsers(t *testing.T) { + prepareTestEnv(t) + adminUsername := "user1" + session := loginUser(t, adminUsername) + token := getTokenForLoggedInUser(t, session) + + urlStr := fmt.Sprintf("/api/v1/admin/users?token=%s", token) + req := NewRequest(t, "GET", urlStr) + resp := session.MakeRequest(t, req, http.StatusOK) + var users []api.User + DecodeJSON(t, resp, &users) + + found := false + for _, user := range users { + if user.UserName == adminUsername { + found = true + } + } + assert.True(t, found) + numberOfUsers := models.GetCount(t, &models.User{}, "type = 0") + assert.Equal(t, numberOfUsers, len(users)) +} diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go index cf52d649ad..4c1bbf1c0b 100644 --- a/routers/api/v1/admin/user.go +++ b/routers/api/v1/admin/user.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/routers/api/v1/convert" "code.gitea.io/gitea/routers/api/v1/user" api "code.gitea.io/sdk/gitea" ) @@ -319,8 +320,14 @@ func GetAllUsers(ctx *context.APIContext) { PageSize: -1, }) if err != nil { - ctx.Error(500, "SearchUsers", err) + ctx.Error(500, "GetAllUsers", err) return } - ctx.JSON(200, &users) + + results := make([]*api.User, len(users)) + for i := range users { + results[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User.IsAdmin) + } + + ctx.JSON(200, &results) } diff --git a/routers/api/v1/convert/convert.go b/routers/api/v1/convert/convert.go index 6569d21ab8..66356451fc 100644 --- a/routers/api/v1/convert/convert.go +++ b/routers/api/v1/convert/convert.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/git" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/util" ) @@ -217,3 +218,18 @@ func ToTeam(team *models.Team) *api.Team { Units: team.GetUnitNames(), } } + +// ToUser convert models.User to api.User +func ToUser(user *models.User, signed, admin bool) *api.User { + result := &api.User{ + ID: user.ID, + UserName: user.Name, + AvatarURL: user.AvatarLink(), + FullName: markup.Sanitize(user.FullName), + IsAdmin: user.IsAdmin, + } + if signed && (!user.KeepEmailPrivate || admin) { + result.Email = user.Email + } + return result +} diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index fe1a14c3b5..aeb2b60f63 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -10,7 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/markup" + "code.gitea.io/gitea/routers/api/v1/convert" api "code.gitea.io/sdk/gitea" "github.com/Unknwon/com" @@ -67,16 +67,7 @@ func Search(ctx *context.APIContext) { results := make([]*api.User, len(users)) for i := range users { - results[i] = &api.User{ - ID: users[i].ID, - UserName: users[i].Name, - AvatarURL: users[i].AvatarLink(), - FullName: markup.Sanitize(users[i].FullName), - IsAdmin: users[i].IsAdmin, - } - if ctx.IsSigned && (!users[i].KeepEmailPrivate || ctx.User.IsAdmin) { - results[i].Email = users[i].Email - } + results[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User.IsAdmin) } ctx.JSON(200, map[string]interface{}{