#1692 add admin APIs to add/remove a user from teams
This commit is contained in:
@ -3,7 +3,7 @@ Gogs - Go Git Service [
|
||||
|
||||
##### Current version: 0.9.15
|
||||
##### Current version: 0.9.16
|
||||
|
||||
| Web | UI | Preview |
|
||||
|:-------------:|:-------:|:-------:|
|
||||
|
2
gogs.go
2
gogs.go
@ -17,7 +17,7 @@ import (
|
||||
"github.com/gogits/gogs/modules/setting"
|
||||
)
|
||||
|
||||
const APP_VER = "0.9.15.0323"
|
||||
const APP_VER = "0.9.16.0325"
|
||||
|
||||
func init() {
|
||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||
|
612
models/org.go
612
models/org.go
File diff suppressed because it is too large
Load Diff
618
models/org_team.go
Normal file
618
models/org_team.go
Normal file
File diff suppressed because it is too large
Load Diff
@ -18,6 +18,7 @@ import (
|
||||
|
||||
type APIContext struct {
|
||||
*Context
|
||||
Org *APIOrganization
|
||||
}
|
||||
|
||||
// Error responses error message to client with given message.
|
||||
@ -40,6 +41,7 @@ func (ctx *APIContext) Error(status int, title string, obj interface{}) {
|
||||
})
|
||||
}
|
||||
|
||||
// SetLinkHeader sets pagination link header by given totol number and page size.
|
||||
func (ctx *APIContext) SetLinkHeader(total, pageSize int) {
|
||||
page := paginater.New(total, pageSize, ctx.QueryInt("page"), 0)
|
||||
links := make([]string, 0, 4)
|
||||
|
14
modules/context/api_org.go
Normal file
14
modules/context/api_org.go
Normal file
@ -0,0 +1,14 @@
|
||||
// Copyright 2016 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package context
|
||||
|
||||
import (
|
||||
"github.com/gogits/gogs/models"
|
||||
)
|
||||
|
||||
type APIOrganization struct {
|
||||
Organization *models.User
|
||||
Team *models.Team
|
||||
}
|
@ -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
|
||||
|
@ -1 +1 @@
|
||||
0.9.15.0323
|
||||
0.9.16.0325
|
Reference in New Issue
Block a user