2014-06-29 15:30:41 -05: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 08:33:49 -05:00
|
|
|
package org
|
|
|
|
|
|
|
|
import (
|
2014-06-29 15:30:41 -05:00
|
|
|
"github.com/gogits/gogs/models"
|
2014-07-02 15:42:16 -05:00
|
|
|
"github.com/gogits/gogs/modules/auth"
|
2014-06-29 15:30:41 -05:00
|
|
|
"github.com/gogits/gogs/modules/base"
|
2014-07-02 15:42:16 -05:00
|
|
|
"github.com/gogits/gogs/modules/log"
|
2014-06-27 08:33:49 -05:00
|
|
|
"github.com/gogits/gogs/modules/middleware"
|
|
|
|
)
|
|
|
|
|
2014-06-29 15:30:41 -05:00
|
|
|
const (
|
2014-08-16 03:21:17 -05:00
|
|
|
TEAMS base.TplName = "org/team/teams"
|
|
|
|
TEAM_NEW base.TplName = "org/team/new"
|
2014-06-29 15:30:41 -05:00
|
|
|
)
|
|
|
|
|
2014-07-26 01:28:04 -05:00
|
|
|
func Teams(ctx *middleware.Context) {
|
2014-08-16 03:21:17 -05:00
|
|
|
org := ctx.Org.Organization
|
|
|
|
ctx.Data["Title"] = org.FullName
|
|
|
|
ctx.Data["PageIsOrgTeams"] = true
|
2014-06-29 15:30:41 -05:00
|
|
|
|
2014-08-16 03:21:17 -05:00
|
|
|
if err := org.GetTeams(); err != nil {
|
|
|
|
ctx.Handle(500, "GetTeams", err)
|
2014-06-29 15:30:41 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, t := range org.Teams {
|
2014-08-16 03:21:17 -05:00
|
|
|
if err := t.GetMembers(); err != nil {
|
|
|
|
ctx.Handle(500, "GetMembers", err)
|
2014-06-29 15:30:41 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.Data["Teams"] = org.Teams
|
|
|
|
|
|
|
|
ctx.HTML(200, TEAMS)
|
2014-06-27 08:33:49 -05:00
|
|
|
}
|
|
|
|
|
2014-08-16 03:21:17 -05:00
|
|
|
func TeamsAction(ctx *middleware.Context) {
|
|
|
|
var err error
|
|
|
|
switch ctx.Params(":action") {
|
|
|
|
case "join":
|
2014-08-16 03:58:32 -05:00
|
|
|
err = ctx.Org.Team.AddMember(ctx.User.Id)
|
2014-08-16 03:21:17 -05:00
|
|
|
case "leave":
|
2014-08-16 03:58:32 -05:00
|
|
|
err = ctx.Org.Team.RemoveMember(ctx.User.Id)
|
2014-07-02 15:42:16 -05:00
|
|
|
}
|
|
|
|
|
2014-08-16 03:21:17 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error(4, "Action(%s): %v", ctx.Params(":action"), err)
|
|
|
|
ctx.JSON(200, map[string]interface{}{
|
|
|
|
"ok": false,
|
|
|
|
"err": err.Error(),
|
|
|
|
})
|
2014-07-02 15:42:16 -05:00
|
|
|
return
|
|
|
|
}
|
2014-08-16 03:21:17 -05:00
|
|
|
ctx.Redirect(ctx.Org.OrgLink + "/teams")
|
|
|
|
}
|
2014-07-02 15:42:16 -05:00
|
|
|
|
2014-08-16 03:21:17 -05:00
|
|
|
func NewTeam(ctx *middleware.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Org.Organization.FullName
|
|
|
|
ctx.Data["PageIsOrgTeams"] = true
|
|
|
|
ctx.Data["PageIsOrgTeamsNew"] = true
|
|
|
|
ctx.Data["Team"] = &models.Team{}
|
2014-07-02 15:42:16 -05:00
|
|
|
ctx.HTML(200, TEAM_NEW)
|
|
|
|
}
|
|
|
|
|
2014-07-26 01:28:04 -05:00
|
|
|
func NewTeamPost(ctx *middleware.Context, form auth.CreateTeamForm) {
|
2014-08-16 03:21:17 -05:00
|
|
|
ctx.Data["Title"] = ctx.Org.Organization.FullName
|
|
|
|
ctx.Data["PageIsOrgTeams"] = true
|
|
|
|
ctx.Data["PageIsOrgTeamsNew"] = true
|
|
|
|
ctx.Data["Team"] = &models.Team{}
|
2014-07-02 15:42:16 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-08-16 03:21:17 -05:00
|
|
|
org := ctx.Org.Organization
|
|
|
|
|
2014-07-02 15:42:16 -05:00
|
|
|
t := &models.Team{
|
|
|
|
OrgId: org.Id,
|
|
|
|
Name: form.TeamName,
|
|
|
|
Description: form.Description,
|
|
|
|
Authorize: auth,
|
|
|
|
}
|
2014-08-16 03:21:17 -05:00
|
|
|
if err := models.NewTeam(t); err != nil {
|
|
|
|
switch err {
|
|
|
|
case models.ErrTeamNameIllegal:
|
|
|
|
ctx.Data["Err_TeamName"] = true
|
|
|
|
ctx.RenderWithErr(ctx.Tr("form.illegal_team_name"), TEAM_NEW, &form)
|
|
|
|
case models.ErrTeamAlreadyExist:
|
2014-07-02 15:42:16 -05:00
|
|
|
ctx.Data["Err_TeamName"] = true
|
2014-08-16 03:21:17 -05:00
|
|
|
ctx.RenderWithErr(ctx.Tr("form.team_name_been_taken"), TEAM_NEW, &form)
|
|
|
|
default:
|
|
|
|
ctx.Handle(500, "NewTeam", err)
|
2014-07-02 15:42:16 -05:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2014-08-16 03:21:17 -05:00
|
|
|
log.Trace("Team created: %s/%s", org.Name, t.Name)
|
|
|
|
ctx.Redirect(ctx.Org.OrgLink + "/teams/" + t.LowerName)
|
2014-06-27 08:33:49 -05:00
|
|
|
}
|
2014-06-27 09:04:04 -05:00
|
|
|
|
2014-07-26 01:28:04 -05:00
|
|
|
func EditTeam(ctx *middleware.Context) {
|
|
|
|
ctx.Data["Title"] = "Organization " + ctx.Params(":org") + " Edit Team"
|
2014-06-29 15:30:41 -05:00
|
|
|
ctx.HTML(200, "org/edit_team")
|
2014-06-27 09:04:04 -05:00
|
|
|
}
|
2014-07-07 05:13:42 -05:00
|
|
|
|
2014-07-26 01:28:04 -05:00
|
|
|
func SingleTeam(ctx *middleware.Context) {
|
|
|
|
ctx.Data["Title"] = "single-team" + ctx.Params(":org")
|
|
|
|
ctx.HTML(200, "org/team")
|
2014-07-07 05:13:42 -05:00
|
|
|
}
|