From e6f927f61af927156798390e64f17dd6755697e7 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Mon, 21 Mar 2016 12:47:54 -0400 Subject: [PATCH] #1692 api: admin list and create team under organization --- README.md | 2 +- gogs.go | 2 +- models/access.go | 27 +++++++++ routers/api/v1/admin/{orgs.go => org.go} | 0 routers/api/v1/admin/org_team.go | 56 +++++++++++++++++++ routers/api/v1/admin/{repos.go => repo.go} | 0 routers/api/v1/admin/{users.go => user.go} | 0 routers/api/v1/api.go | 4 ++ routers/api/v1/convert/convert.go | 9 +++ routers/api/v1/repo/{hooks.go => hook.go} | 0 routers/api/v1/repo/{keys.go => key.go} | 0 .../api/v1/user/{followers.go => follower.go} | 0 routers/api/v1/user/{keys.go => key.go} | 0 routers/org/teams.go | 16 +----- templates/.VERSION | 2 +- 15 files changed, 100 insertions(+), 18 deletions(-) rename routers/api/v1/admin/{orgs.go => org.go} (100%) create mode 100644 routers/api/v1/admin/org_team.go rename routers/api/v1/admin/{repos.go => repo.go} (100%) rename routers/api/v1/admin/{users.go => user.go} (100%) rename routers/api/v1/repo/{hooks.go => hook.go} (100%) rename routers/api/v1/repo/{keys.go => key.go} (100%) rename routers/api/v1/user/{followers.go => follower.go} (100%) rename routers/api/v1/user/{keys.go => key.go} (100%) diff --git a/README.md b/README.md index 0886903bb..935be1808 100644 --- a/README.md +++ b/README.md @@ -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.13 +##### Current version: 0.9.14 | Web | UI | Preview | |:-------------:|:-------:|:-------:| diff --git a/gogs.go b/gogs.go index cc3c436ff..1e2573b2d 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.9.13.0321" +const APP_VER = "0.9.14.0321" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/access.go b/models/access.go index 447777ad5..b4c1349b6 100644 --- a/models/access.go +++ b/models/access.go @@ -20,6 +20,33 @@ const ( ACCESS_MODE_OWNER // 4 ) +func (mode AccessMode) String() string { + switch mode { + case ACCESS_MODE_READ: + return "read" + case ACCESS_MODE_WRITE: + return "write" + case ACCESS_MODE_ADMIN: + return "admin" + case ACCESS_MODE_OWNER: + return "owner" + default: + return "none" + } +} + +// ParseAccessMode returns corresponding access mode to given permission string. +func ParseAccessMode(permission string) AccessMode { + switch permission { + case "write": + return ACCESS_MODE_WRITE + case "admin": + return ACCESS_MODE_ADMIN + default: + return ACCESS_MODE_READ + } +} + // Access represents the highest access level of a user to the repository. The only access type // that is not in this table is the real owner of a repository. In case of an organization // repository, the members of the owners team are in this table. diff --git a/routers/api/v1/admin/orgs.go b/routers/api/v1/admin/org.go similarity index 100% rename from routers/api/v1/admin/orgs.go rename to routers/api/v1/admin/org.go diff --git a/routers/api/v1/admin/org_team.go b/routers/api/v1/admin/org_team.go new file mode 100644 index 000000000..618dd9a94 --- /dev/null +++ b/routers/api/v1/admin/org_team.go @@ -0,0 +1,56 @@ +// 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 admin + +import ( + api "github.com/gogits/go-gogs-client" + + "github.com/gogits/gogs/models" + "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 + } + + if err := org.GetTeams(); err != nil { + ctx.Error(500, "GetTeams", err) + return + } + + apiTeams := make([]*api.Team, len(org.Teams)) + for i := range org.Teams { + apiTeams[i] = convert.ToTeam(org.Teams[i]) + } + ctx.JSON(200, apiTeams) +} + +func CreateTeam(ctx *context.APIContext, form api.CreateTeamOption) { + org := user.GetUserByParamsName(ctx, ":orgname") + if ctx.Written() { + return + } + + team := &models.Team{ + OrgID: org.Id, + Name: form.Name, + Description: form.Description, + Authorize: models.ParseAccessMode(form.Permission), + } + if err := models.NewTeam(team); err != nil { + if models.IsErrTeamAlreadyExist(err) { + ctx.Error(422, "NewTeam", err) + } else { + ctx.Error(500, "NewTeam", err) + } + return + } + + ctx.JSON(200, convert.ToTeam(team)) +} diff --git a/routers/api/v1/admin/repos.go b/routers/api/v1/admin/repo.go similarity index 100% rename from routers/api/v1/admin/repos.go rename to routers/api/v1/admin/repo.go diff --git a/routers/api/v1/admin/users.go b/routers/api/v1/admin/user.go similarity index 100% rename from routers/api/v1/admin/users.go rename to routers/api/v1/admin/user.go diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 59625efdf..744289ac0 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -223,6 +223,10 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo) }) }) + + m.Group("/orgs/:orgname", func() { + m.Combo("/teams").Get(admin.ListTeams).Post(bind(api.CreateTeamOption{}), admin.CreateTeam) + }) }, ReqAdmin()) }, context.APIContexter()) } diff --git a/routers/api/v1/convert/convert.go b/routers/api/v1/convert/convert.go index 7e3e380bd..8eca5f4ec 100644 --- a/routers/api/v1/convert/convert.go +++ b/routers/api/v1/convert/convert.go @@ -196,3 +196,12 @@ func ToOrganization(org *models.User) *api.Organization { Location: org.Location, } } + +func ToTeam(team *models.Team) *api.Team { + return &api.Team{ + ID: team.ID, + Name: team.Name, + Description: team.Description, + Permission: team.Authorize.String(), + } +} diff --git a/routers/api/v1/repo/hooks.go b/routers/api/v1/repo/hook.go similarity index 100% rename from routers/api/v1/repo/hooks.go rename to routers/api/v1/repo/hook.go diff --git a/routers/api/v1/repo/keys.go b/routers/api/v1/repo/key.go similarity index 100% rename from routers/api/v1/repo/keys.go rename to routers/api/v1/repo/key.go diff --git a/routers/api/v1/user/followers.go b/routers/api/v1/user/follower.go similarity index 100% rename from routers/api/v1/user/followers.go rename to routers/api/v1/user/follower.go diff --git a/routers/api/v1/user/keys.go b/routers/api/v1/user/key.go similarity index 100% rename from routers/api/v1/user/keys.go rename to routers/api/v1/user/key.go diff --git a/routers/org/teams.go b/routers/org/teams.go index e8ae8291b..430f08ec2 100644 --- a/routers/org/teams.go +++ b/routers/org/teams.go @@ -154,25 +154,11 @@ func NewTeamPost(ctx *context.Context, form auth.CreateTeamForm) { ctx.Data["PageIsOrgTeams"] = true ctx.Data["PageIsOrgTeamsNew"] = true - // Validate permission level. - var auth models.AccessMode - switch form.Permission { - case "read": - auth = models.ACCESS_MODE_READ - case "write": - auth = models.ACCESS_MODE_WRITE - case "admin": - auth = models.ACCESS_MODE_ADMIN - default: - ctx.Error(401) - return - } - t := &models.Team{ OrgID: ctx.Org.Organization.Id, Name: form.TeamName, Description: form.Description, - Authorize: auth, + Authorize: models.ParseAccessMode(form.Permission), } ctx.Data["Team"] = t diff --git a/templates/.VERSION b/templates/.VERSION index f983a7d61..10cd7f56e 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.9.13.0321 \ No newline at end of file +0.9.14.0321 \ No newline at end of file