#1692 add admin APIs to add/remove a user from teams
This commit is contained in:
parent
9dda9ef07c
commit
b1d41cfa60
11 changed files with 712 additions and 639 deletions
|
@ -14,13 +14,8 @@ import (
|
|||
)
|
||||
|
||||
func CreateTeam(ctx *context.APIContext, form api.CreateTeamOption) {
|
||||
org := user.GetUserByParamsName(ctx, ":orgname")
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
|
||||
team := &models.Team{
|
||||
OrgID: org.Id,
|
||||
OrgID: ctx.Org.Organization.Id,
|
||||
Name: form.Name,
|
||||
Description: form.Description,
|
||||
Authorize: models.ParseAccessMode(form.Permission),
|
||||
|
@ -36,3 +31,30 @@ func CreateTeam(ctx *context.APIContext, form api.CreateTeamOption) {
|
|||
|
||||
ctx.JSON(201, convert.ToTeam(team))
|
||||
}
|
||||
|
||||
func AddTeamMember(ctx *context.APIContext) {
|
||||
u := user.GetUserByParams(ctx)
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
if err := ctx.Org.Team.AddMember(u.Id); err != nil {
|
||||
ctx.Error(500, "AddMember", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Status(204)
|
||||
}
|
||||
|
||||
func RemoveTeamMember(ctx *context.APIContext) {
|
||||
u := user.GetUserByParams(ctx)
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
|
||||
if err := ctx.Org.Team.RemoveMember(u.Id); err != nil {
|
||||
ctx.Error(500, "RemoveMember", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Status(204)
|
||||
}
|
||||
|
|
|
@ -110,6 +110,42 @@ func ReqAdmin() macaron.Handler {
|
|||
}
|
||||
}
|
||||
|
||||
func OrgAssignment(args ...bool) macaron.Handler {
|
||||
var (
|
||||
assignTeam bool
|
||||
)
|
||||
|
||||
if len(args) > 0 {
|
||||
assignTeam = args[0]
|
||||
}
|
||||
return func(ctx *context.APIContext) {
|
||||
org, err := models.GetUserByName(ctx.Params(":orgname"))
|
||||
if err != nil {
|
||||
if models.IsErrUserNotExist(err) {
|
||||
ctx.Status(404)
|
||||
} else {
|
||||
ctx.Error(500, "GetUserByName", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
ctx.Org = &context.APIOrganization{
|
||||
Organization: org,
|
||||
}
|
||||
|
||||
if assignTeam {
|
||||
ctx.Org.Team, err = models.GetTeamByID(ctx.ParamsInt64(":teamid"))
|
||||
if err != nil {
|
||||
if models.IsErrUserNotExist(err) {
|
||||
ctx.Status(404)
|
||||
} else {
|
||||
ctx.Error(500, "GetTeamById", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterRoutes registers all v1 APIs routes to web application.
|
||||
// FIXME: custom form error response
|
||||
func RegisterRoutes(m *macaron.Macaron) {
|
||||
|
@ -208,7 +244,7 @@ func RegisterRoutes(m *macaron.Macaron) {
|
|||
m.Group("/orgs/:orgname", func() {
|
||||
m.Combo("").Get(org.Get).Patch(bind(api.EditOrgOption{}), org.Edit)
|
||||
m.Combo("/teams").Get(org.ListTeams)
|
||||
})
|
||||
}, OrgAssignment())
|
||||
|
||||
m.Any("/*", func(ctx *context.Context) {
|
||||
ctx.Error(404)
|
||||
|
@ -228,7 +264,13 @@ func RegisterRoutes(m *macaron.Macaron) {
|
|||
})
|
||||
|
||||
m.Group("/orgs/:orgname", func() {
|
||||
m.Combo("/teams").Post(bind(api.CreateTeamOption{}), admin.CreateTeam)
|
||||
m.Group("/teams", func() {
|
||||
m.Post("", OrgAssignment(), bind(api.CreateTeamOption{}), admin.CreateTeam)
|
||||
|
||||
m.Group("/:teamid", func() {
|
||||
m.Combo("/memberships/:username").Put(admin.AddTeamMember).Delete(admin.RemoveTeamMember)
|
||||
}, OrgAssignment(true))
|
||||
})
|
||||
})
|
||||
}, ReqAdmin())
|
||||
}, context.APIContexter())
|
||||
|
|
|
@ -42,20 +42,12 @@ func ListUserOrgs(ctx *context.APIContext) {
|
|||
|
||||
// https://github.com/gogits/go-gogs-client/wiki/Organizations#get-an-organization
|
||||
func Get(ctx *context.APIContext) {
|
||||
org := user.GetUserByParamsName(ctx, ":orgname")
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
ctx.JSON(200, convert.ToOrganization(org))
|
||||
ctx.JSON(200, convert.ToOrganization(ctx.Org.Organization))
|
||||
}
|
||||
|
||||
// https://github.com/gogits/go-gogs-client/wiki/Organizations#edit-an-organization
|
||||
func Edit(ctx *context.APIContext, form api.EditOrgOption) {
|
||||
org := user.GetUserByParamsName(ctx, ":orgname")
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
|
||||
org := ctx.Org.Organization
|
||||
if !org.IsOwnedBy(ctx.User.Id) {
|
||||
ctx.Status(403)
|
||||
return
|
||||
|
|
|
@ -9,15 +9,10 @@ import (
|
|||
|
||||
"github.com/gogits/gogs/modules/context"
|
||||
"github.com/gogits/gogs/routers/api/v1/convert"
|
||||
"github.com/gogits/gogs/routers/api/v1/user"
|
||||
)
|
||||
|
||||
func ListTeams(ctx *context.APIContext) {
|
||||
org := user.GetUserByParamsName(ctx, ":orgname")
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
|
||||
org := ctx.Org.Organization
|
||||
if err := org.GetTeams(); err != nil {
|
||||
ctx.Error(500, "GetTeams", err)
|
||||
return
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue