Return a UserList from /api/v1/admin/users (#6629)

tokarchuk/v1.17
zeripath 6 years ago committed by techknowlogick
parent 74fc63682c
commit 837116875e
  1. 23
      integrations/api_admin_test.go
  2. 11
      routers/api/v1/admin/user.go
  3. 16
      routers/api/v1/convert/convert.go
  4. 13
      routers/api/v1/user/user.go

@ -106,3 +106,26 @@ func TestAPISudoUserForbidden(t *testing.T) {
req := NewRequest(t, "GET", urlStr) req := NewRequest(t, "GET", urlStr)
session.MakeRequest(t, req, http.StatusForbidden) 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))
}

@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/routers/api/v1/convert"
"code.gitea.io/gitea/routers/api/v1/user" "code.gitea.io/gitea/routers/api/v1/user"
api "code.gitea.io/sdk/gitea" api "code.gitea.io/sdk/gitea"
) )
@ -319,8 +320,14 @@ func GetAllUsers(ctx *context.APIContext) {
PageSize: -1, PageSize: -1,
}) })
if err != nil { if err != nil {
ctx.Error(500, "SearchUsers", err) ctx.Error(500, "GetAllUsers", err)
return 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)
} }

@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
api "code.gitea.io/sdk/gitea" api "code.gitea.io/sdk/gitea"
@ -216,3 +217,18 @@ func ToTeam(team *models.Team) *api.Team {
Units: team.GetUnitNames(), 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
}

@ -10,7 +10,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context" "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" api "code.gitea.io/sdk/gitea"
"github.com/Unknwon/com" "github.com/Unknwon/com"
@ -67,16 +67,7 @@ func Search(ctx *context.APIContext) {
results := make([]*api.User, len(users)) results := make([]*api.User, len(users))
for i := range users { for i := range users {
results[i] = &api.User{ results[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User.IsAdmin)
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
}
} }
ctx.JSON(200, map[string]interface{}{ ctx.JSON(200, map[string]interface{}{

Loading…
Cancel
Save