2014-06-30 02:00:41 +05:30
|
|
|
// 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 19:03:49 +05:30
|
|
|
package org
|
|
|
|
|
|
|
|
import (
|
2014-06-30 02:00:41 +05:30
|
|
|
"github.com/gogits/gogs/models"
|
2014-07-03 02:12:16 +05:30
|
|
|
"github.com/gogits/gogs/modules/auth"
|
2014-06-30 02:00:41 +05:30
|
|
|
"github.com/gogits/gogs/modules/base"
|
2014-07-03 02:12:16 +05:30
|
|
|
"github.com/gogits/gogs/modules/log"
|
2014-06-27 19:03:49 +05:30
|
|
|
"github.com/gogits/gogs/modules/middleware"
|
|
|
|
)
|
|
|
|
|
2014-06-30 02:00:41 +05:30
|
|
|
const (
|
2014-07-03 02:12:16 +05:30
|
|
|
TEAMS base.TplName = "org/teams"
|
|
|
|
TEAM_NEW base.TplName = "org/team_new"
|
2014-06-30 02:00:41 +05:30
|
|
|
)
|
|
|
|
|
2014-07-26 11:58:04 +05:30
|
|
|
func Teams(ctx *middleware.Context) {
|
|
|
|
ctx.Data["Title"] = "Organization " + ctx.Params(":org") + " Teams"
|
2014-06-30 02:00:41 +05:30
|
|
|
|
2014-07-26 11:58:04 +05:30
|
|
|
org, err := models.GetUserByName(ctx.Params(":org"))
|
2014-06-30 02:00:41 +05:30
|
|
|
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 19:03:49 +05:30
|
|
|
}
|
|
|
|
|
2014-07-26 11:58:04 +05:30
|
|
|
func NewTeam(ctx *middleware.Context) {
|
|
|
|
org, err := models.GetUserByName(ctx.Params(":org"))
|
2014-07-03 02:12:16 +05:30
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-07-26 11:58:04 +05:30
|
|
|
func NewTeamPost(ctx *middleware.Context, form auth.CreateTeamForm) {
|
|
|
|
org, err := models.GetUserByName(ctx.Params(":org"))
|
2014-07-03 02:12:16 +05:30
|
|
|
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 19:03:49 +05:30
|
|
|
}
|
2014-06-27 19:34:04 +05:30
|
|
|
|
2014-07-26 11:58:04 +05:30
|
|
|
func EditTeam(ctx *middleware.Context) {
|
|
|
|
ctx.Data["Title"] = "Organization " + ctx.Params(":org") + " Edit Team"
|
2014-06-30 02:00:41 +05:30
|
|
|
ctx.HTML(200, "org/edit_team")
|
2014-06-27 19:34:04 +05:30
|
|
|
}
|
2014-07-07 15:43:42 +05:30
|
|
|
|
2014-07-26 11:58:04 +05:30
|
|
|
func SingleTeam(ctx *middleware.Context) {
|
|
|
|
ctx.Data["Title"] = "single-team" + ctx.Params(":org")
|
|
|
|
ctx.HTML(200, "org/team")
|
2014-07-07 15:43:42 +05:30
|
|
|
}
|