forgejo/routers/org/teams.go

135 lines
3.1 KiB
Go
Raw Normal View History

2014-06-29 20:30:41 +00:00
// Copyright 2014 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.
2014-06-27 13:33:49 +00:00
package org
import (
2014-06-29 20:30:41 +00:00
"github.com/gogits/gogs/models"
2014-07-02 20:42:16 +00:00
"github.com/gogits/gogs/modules/auth"
2014-06-29 20:30:41 +00:00
"github.com/gogits/gogs/modules/base"
2014-07-02 20:42:16 +00:00
"github.com/gogits/gogs/modules/log"
2014-06-27 13:33:49 +00:00
"github.com/gogits/gogs/modules/middleware"
)
2014-06-29 20:30:41 +00:00
const (
2014-07-02 20:42:16 +00:00
TEAMS base.TplName = "org/teams"
TEAM_NEW base.TplName = "org/team_new"
2014-06-29 20:30:41 +00:00
)
func Teams(ctx *middleware.Context) {
ctx.Data["Title"] = "Organization " + ctx.Params(":org") + " Teams"
2014-06-29 20:30:41 +00:00
org, err := models.GetUserByName(ctx.Params(":org"))
2014-06-29 20:30:41 +00:00
if err != nil {
if err == models.ErrUserNotExist {
ctx.Handle(404, "org.Teams(GetUserByName)", err)
} else {
ctx.Handle(500, "org.Teams(GetUserByName)", err)
}
return
}
ctx.Data["Org"] = org
if err = org.GetTeams(); err != nil {
ctx.Handle(500, "org.Teams(GetTeams)", err)
return
}
for _, t := range org.Teams {
if err = t.GetMembers(); err != nil {
ctx.Handle(500, "org.Home(GetMembers)", err)
return
}
}
ctx.Data["Teams"] = org.Teams
ctx.HTML(200, TEAMS)
2014-06-27 13:33:49 +00:00
}
func NewTeam(ctx *middleware.Context) {
org, err := models.GetUserByName(ctx.Params(":org"))
2014-07-02 20:42:16 +00:00
if err != nil {
if err == models.ErrUserNotExist {
ctx.Handle(404, "org.NewTeam(GetUserByName)", err)
} else {
ctx.Handle(500, "org.NewTeam(GetUserByName)", err)
}
return
}
ctx.Data["Org"] = org
// Check ownership of organization.
if !org.IsOrgOwner(ctx.User.Id) {
ctx.Error(403)
return
}
ctx.HTML(200, TEAM_NEW)
}
func NewTeamPost(ctx *middleware.Context, form auth.CreateTeamForm) {
org, err := models.GetUserByName(ctx.Params(":org"))
2014-07-02 20:42:16 +00:00
if err != nil {
if err == models.ErrUserNotExist {
ctx.Handle(404, "org.NewTeamPost(GetUserByName)", err)
} else {
ctx.Handle(500, "org.NewTeamPost(GetUserByName)", err)
}
return
}
ctx.Data["Org"] = org
// Check ownership of organization.
if !org.IsOrgOwner(ctx.User.Id) {
ctx.Error(403)
return
}
if ctx.HasError() {
ctx.HTML(200, TEAM_NEW)
return
}
// Validate permission level.
var auth models.AuthorizeType
switch form.Permission {
case "read":
auth = models.ORG_READABLE
case "write":
auth = models.ORG_WRITABLE
case "admin":
auth = models.ORG_ADMIN
default:
ctx.Error(401)
return
}
t := &models.Team{
OrgId: org.Id,
Name: form.TeamName,
Description: form.Description,
Authorize: auth,
}
if err = models.NewTeam(t); err != nil {
if err == models.ErrTeamAlreadyExist {
ctx.Data["Err_TeamName"] = true
ctx.RenderWithErr("Team name has already been used", TEAM_NEW, &form)
} else {
ctx.Handle(500, "org.NewTeamPost(NewTeam)", err)
}
return
}
log.Trace("%s Team created: %s/%s", ctx.Req.RequestURI, org.Name, t.Name)
ctx.Redirect("/org/" + org.LowerName + "/teams/" + t.LowerName)
2014-06-27 13:33:49 +00:00
}
2014-06-27 14:04:04 +00:00
func EditTeam(ctx *middleware.Context) {
ctx.Data["Title"] = "Organization " + ctx.Params(":org") + " Edit Team"
2014-06-29 20:30:41 +00:00
ctx.HTML(200, "org/edit_team")
2014-06-27 14:04:04 +00:00
}
2014-07-07 10:13:42 +00:00
func SingleTeam(ctx *middleware.Context) {
ctx.Data["Title"] = "single-team" + ctx.Params(":org")
ctx.HTML(200, "org/team")
2014-07-07 10:13:42 +00:00
}