Skip to content

Commit

Permalink
Return a UserList from /api/v1/admin/users (go-gitea#6629)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeripath committed Apr 15, 2019
1 parent e8ca2da commit e8320a4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
23 changes: 23 additions & 0 deletions integrations/api_admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
11 changes: 9 additions & 2 deletions routers/api/v1/admin/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
16 changes: 16 additions & 0 deletions routers/api/v1/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
13 changes: 2 additions & 11 deletions routers/api/v1/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}{
Expand Down

0 comments on commit e8320a4

Please sign in to comment.