1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-25 08:59:31 -05:00

#1692 api: admin list and create team under organization

This commit is contained in:
Unknwon 2016-03-21 12:47:54 -04:00
parent 60ae8ac3d2
commit e6f927f61a
15 changed files with 100 additions and 18 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) ![](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 | | Web | UI | Preview |
|:-------------:|:-------:|:-------:| |:-------------:|:-------:|:-------:|

View file

@ -17,7 +17,7 @@ import (
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
) )
const APP_VER = "0.9.13.0321" const APP_VER = "0.9.14.0321"
func init() { func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

View file

@ -20,6 +20,33 @@ const (
ACCESS_MODE_OWNER // 4 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 // 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 // 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. // repository, the members of the owners team are in this table.

View file

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

View file

@ -223,6 +223,10 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo) 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()) }, ReqAdmin())
}, context.APIContexter()) }, context.APIContexter())
} }

View file

@ -196,3 +196,12 @@ func ToOrganization(org *models.User) *api.Organization {
Location: org.Location, 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(),
}
}

View file

@ -154,25 +154,11 @@ func NewTeamPost(ctx *context.Context, form auth.CreateTeamForm) {
ctx.Data["PageIsOrgTeams"] = true ctx.Data["PageIsOrgTeams"] = true
ctx.Data["PageIsOrgTeamsNew"] = 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{ t := &models.Team{
OrgID: ctx.Org.Organization.Id, OrgID: ctx.Org.Organization.Id,
Name: form.TeamName, Name: form.TeamName,
Description: form.Description, Description: form.Description,
Authorize: auth, Authorize: models.ParseAccessMode(form.Permission),
} }
ctx.Data["Team"] = t ctx.Data["Team"] = t

View file

@ -1 +1 @@
0.9.13.0321 0.9.14.0321