#1692 add admin APIs to add/remove a user from teams

This commit is contained in:
Unknwon
2016-03-25 18:04:02 -04:00
parent 9dda9ef07c
commit b1d41cfa60
11 changed files with 712 additions and 639 deletions

View File

@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra
![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true)
##### Current version: 0.9.15
##### Current version: 0.9.16
| Web | UI | Preview |
|:-------------:|:-------:|:-------:|

View File

@ -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())

File diff suppressed because it is too large Load Diff

618
models/org_team.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -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)

View 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
}

View File

@ -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)
}

View File

@ -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())

View File

@ -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

View File

@ -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

View File

@ -1 +1 @@
0.9.15.0323
0.9.16.0325