diff options
author | Unknwon <u@gogs.io> | 2016-03-25 18:04:02 -0400 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2016-03-25 18:04:02 -0400 |
commit | b1d41cfa603aee63711fbc23bb02fced5a4c54e3 (patch) | |
tree | dc79e8f9c8dec5ab324b27d10108667e7b4d9090 /routers | |
parent | 9dda9ef07c03000cb961a80e3c75459c58602009 (diff) | |
download | gitea-b1d41cfa603aee63711fbc23bb02fced5a4c54e3.tar.gz gitea-b1d41cfa603aee63711fbc23bb02fced5a4c54e3.zip |
#1692 add admin APIs to add/remove a user from teams
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/admin/org_team.go | 34 | ||||
-rw-r--r-- | routers/api/v1/api.go | 46 | ||||
-rw-r--r-- | routers/api/v1/org/org.go | 12 | ||||
-rw-r--r-- | routers/api/v1/org/team.go | 7 |
4 files changed, 75 insertions, 24 deletions
diff --git a/routers/api/v1/admin/org_team.go b/routers/api/v1/admin/org_team.go index 4c67b5743a..9b5411b12e 100644 --- a/routers/api/v1/admin/org_team.go +++ b/routers/api/v1/admin/org_team.go @@ -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) +} diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 08e6a75153..34a441f417 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -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()) diff --git a/routers/api/v1/org/org.go b/routers/api/v1/org/org.go index b4e52d4862..5b1ea007e3 100644 --- a/routers/api/v1/org/org.go +++ b/routers/api/v1/org/org.go @@ -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 diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go index fad0d2f703..70f8d84206 100644 --- a/routers/api/v1/org/team.go +++ b/routers/api/v1/org/team.go @@ -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 |