mirror of https://github.com/gogits/gogs.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
272 lines
6.2 KiB
272 lines
6.2 KiB
11 years ago
|
// 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.
|
||
|
|
||
11 years ago
|
package org
|
||
|
|
||
|
import (
|
||
10 years ago
|
"path"
|
||
|
|
||
10 years ago
|
"github.com/Unknwon/com"
|
||
8 years ago
|
log "gopkg.in/clog.v1"
|
||
10 years ago
|
|
||
11 years ago
|
"github.com/gogits/gogs/models"
|
||
8 years ago
|
"github.com/gogits/gogs/models/errors"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/context"
|
||
|
"github.com/gogits/gogs/pkg/form"
|
||
11 years ago
|
)
|
||
|
|
||
11 years ago
|
const (
|
||
8 years ago
|
TEAMS = "org/team/teams"
|
||
|
TEAM_NEW = "org/team/new"
|
||
|
TEAM_MEMBERS = "org/team/members"
|
||
|
TEAM_REPOSITORIES = "org/team/repositories"
|
||
11 years ago
|
)
|
||
|
|
||
8 years ago
|
func Teams(c *context.Context) {
|
||
|
org := c.Org.Organization
|
||
|
c.Data["Title"] = org.FullName
|
||
|
c.Data["PageIsOrgTeams"] = true
|
||
11 years ago
|
|
||
|
for _, t := range org.Teams {
|
||
11 years ago
|
if err := t.GetMembers(); err != nil {
|
||
8 years ago
|
c.Handle(500, "GetMembers", err)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
}
|
||
8 years ago
|
c.Data["Teams"] = org.Teams
|
||
11 years ago
|
|
||
8 years ago
|
c.HTML(200, TEAMS)
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func TeamsAction(c *context.Context) {
|
||
|
uid := com.StrTo(c.Query("uid")).MustInt64()
|
||
10 years ago
|
if uid == 0 {
|
||
8 years ago
|
c.Redirect(c.Org.OrgLink + "/teams")
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
page := c.Query("page")
|
||
11 years ago
|
var err error
|
||
8 years ago
|
switch c.Params(":action") {
|
||
11 years ago
|
case "join":
|
||
8 years ago
|
if !c.Org.IsOwner {
|
||
|
c.Error(404)
|
||
10 years ago
|
return
|
||
|
}
|
||
8 years ago
|
err = c.Org.Team.AddMember(c.User.ID)
|
||
11 years ago
|
case "leave":
|
||
8 years ago
|
err = c.Org.Team.RemoveMember(c.User.ID)
|
||
10 years ago
|
case "remove":
|
||
8 years ago
|
if !c.Org.IsOwner {
|
||
|
c.Error(404)
|
||
10 years ago
|
return
|
||
|
}
|
||
8 years ago
|
err = c.Org.Team.RemoveMember(uid)
|
||
10 years ago
|
page = "team"
|
||
|
case "add":
|
||
8 years ago
|
if !c.Org.IsOwner {
|
||
|
c.Error(404)
|
||
10 years ago
|
return
|
||
|
}
|
||
8 years ago
|
uname := c.Query("uname")
|
||
10 years ago
|
var u *models.User
|
||
|
u, err = models.GetUserByName(uname)
|
||
|
if err != nil {
|
||
8 years ago
|
if errors.IsUserNotExist(err) {
|
||
8 years ago
|
c.Flash.Error(c.Tr("form.user_not_exist"))
|
||
|
c.Redirect(c.Org.OrgLink + "/teams/" + c.Org.Team.LowerName)
|
||
10 years ago
|
} else {
|
||
8 years ago
|
c.Handle(500, " GetUserByName", err)
|
||
10 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
err = c.Org.Team.AddMember(u.ID)
|
||
10 years ago
|
page = "team"
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
if err != nil {
|
||
10 years ago
|
if models.IsErrLastOrgOwner(err) {
|
||
8 years ago
|
c.Flash.Error(c.Tr("form.last_org_owner"))
|
||
10 years ago
|
} else {
|
||
8 years ago
|
log.Error(3, "Action(%s): %v", c.Params(":action"), err)
|
||
|
c.JSON(200, map[string]interface{}{
|
||
10 years ago
|
"ok": false,
|
||
|
"err": err.Error(),
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
switch page {
|
||
|
case "team":
|
||
8 years ago
|
c.Redirect(c.Org.OrgLink + "/teams/" + c.Org.Team.LowerName)
|
||
10 years ago
|
default:
|
||
8 years ago
|
c.Redirect(c.Org.OrgLink + "/teams")
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
8 years ago
|
func TeamsRepoAction(c *context.Context) {
|
||
|
if !c.Org.IsOwner {
|
||
|
c.Error(404)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
|
var err error
|
||
8 years ago
|
switch c.Params(":action") {
|
||
10 years ago
|
case "add":
|
||
8 years ago
|
repoName := path.Base(c.Query("repo_name"))
|
||
10 years ago
|
var repo *models.Repository
|
||
8 years ago
|
repo, err = models.GetRepositoryByName(c.Org.Organization.ID, repoName)
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
if errors.IsRepoNotExist(err) {
|
||
8 years ago
|
c.Flash.Error(c.Tr("org.teams.add_nonexistent_repo"))
|
||
|
c.Redirect(c.Org.OrgLink + "/teams/" + c.Org.Team.LowerName + "/repositories")
|
||
10 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Handle(500, "GetRepositoryByName", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
8 years ago
|
err = c.Org.Team.AddRepository(repo)
|
||
10 years ago
|
case "remove":
|
||
8 years ago
|
err = c.Org.Team.RemoveRepository(com.StrTo(c.Query("repoid")).MustInt64())
|
||
10 years ago
|
}
|
||
|
|
||
|
if err != nil {
|
||
8 years ago
|
log.Error(3, "Action(%s): '%s' %v", c.Params(":action"), c.Org.Team.Name, err)
|
||
|
c.Handle(500, "TeamsRepoAction", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Redirect(c.Org.OrgLink + "/teams/" + c.Org.Team.LowerName + "/repositories")
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func NewTeam(c *context.Context) {
|
||
|
c.Data["Title"] = c.Org.Organization.FullName
|
||
|
c.Data["PageIsOrgTeams"] = true
|
||
|
c.Data["PageIsOrgTeamsNew"] = true
|
||
|
c.Data["Team"] = &models.Team{}
|
||
|
c.HTML(200, TEAM_NEW)
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func NewTeamPost(c *context.Context, f form.CreateTeam) {
|
||
|
c.Data["Title"] = c.Org.Organization.FullName
|
||
|
c.Data["PageIsOrgTeams"] = true
|
||
|
c.Data["PageIsOrgTeamsNew"] = true
|
||
11 years ago
|
|
||
|
t := &models.Team{
|
||
8 years ago
|
OrgID: c.Org.Organization.ID,
|
||
8 years ago
|
Name: f.TeamName,
|
||
|
Description: f.Description,
|
||
|
Authorize: models.ParseAccessMode(f.Permission),
|
||
11 years ago
|
}
|
||
8 years ago
|
c.Data["Team"] = t
|
||
9 years ago
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.HTML(200, TEAM_NEW)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
11 years ago
|
if err := models.NewTeam(t); err != nil {
|
||
8 years ago
|
c.Data["Err_TeamName"] = true
|
||
9 years ago
|
switch {
|
||
|
case models.IsErrTeamAlreadyExist(err):
|
||
8 years ago
|
c.RenderWithErr(c.Tr("form.team_name_been_taken"), TEAM_NEW, &f)
|
||
8 years ago
|
case models.IsErrNameReserved(err):
|
||
8 years ago
|
c.RenderWithErr(c.Tr("org.form.team_name_reserved", err.(models.ErrNameReserved).Name), TEAM_NEW, &f)
|
||
11 years ago
|
default:
|
||
8 years ago
|
c.Handle(500, "NewTeam", err)
|
||
11 years ago
|
}
|
||
|
return
|
||
|
}
|
||
8 years ago
|
log.Trace("Team created: %s/%s", c.Org.Organization.Name, t.Name)
|
||
|
c.Redirect(c.Org.OrgLink + "/teams/" + t.LowerName)
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
8 years ago
|
func TeamMembers(c *context.Context) {
|
||
|
c.Data["Title"] = c.Org.Team.Name
|
||
|
c.Data["PageIsOrgTeams"] = true
|
||
|
if err := c.Org.Team.GetMembers(); err != nil {
|
||
|
c.Handle(500, "GetMembers", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.HTML(200, TEAM_MEMBERS)
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func TeamRepositories(c *context.Context) {
|
||
|
c.Data["Title"] = c.Org.Team.Name
|
||
|
c.Data["PageIsOrgTeams"] = true
|
||
|
if err := c.Org.Team.GetRepositories(); err != nil {
|
||
|
c.Handle(500, "GetRepositories", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.HTML(200, TEAM_REPOSITORIES)
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func EditTeam(c *context.Context) {
|
||
|
c.Data["Title"] = c.Org.Organization.FullName
|
||
|
c.Data["PageIsOrgTeams"] = true
|
||
|
c.Data["team_name"] = c.Org.Team.Name
|
||
|
c.Data["desc"] = c.Org.Team.Description
|
||
|
c.HTML(200, TEAM_NEW)
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
8 years ago
|
func EditTeamPost(c *context.Context, f form.CreateTeam) {
|
||
|
t := c.Org.Team
|
||
|
c.Data["Title"] = c.Org.Organization.FullName
|
||
|
c.Data["PageIsOrgTeams"] = true
|
||
|
c.Data["Team"] = t
|
||
10 years ago
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.HTML(200, TEAM_NEW)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
|
isAuthChanged := false
|
||
|
if !t.IsOwnerTeam() {
|
||
|
// Validate permission level.
|
||
10 years ago
|
var auth models.AccessMode
|
||
8 years ago
|
switch f.Permission {
|
||
10 years ago
|
case "read":
|
||
10 years ago
|
auth = models.ACCESS_MODE_READ
|
||
10 years ago
|
case "write":
|
||
10 years ago
|
auth = models.ACCESS_MODE_WRITE
|
||
10 years ago
|
case "admin":
|
||
10 years ago
|
auth = models.ACCESS_MODE_ADMIN
|
||
10 years ago
|
default:
|
||
8 years ago
|
c.Error(401)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
t.Name = f.TeamName
|
||
10 years ago
|
if t.Authorize != auth {
|
||
|
isAuthChanged = true
|
||
|
t.Authorize = auth
|
||
|
}
|
||
|
}
|
||
8 years ago
|
t.Description = f.Description
|
||
10 years ago
|
if err := models.UpdateTeam(t, isAuthChanged); err != nil {
|
||
8 years ago
|
c.Data["Err_TeamName"] = true
|
||
9 years ago
|
switch {
|
||
|
case models.IsErrTeamAlreadyExist(err):
|
||
8 years ago
|
c.RenderWithErr(c.Tr("form.team_name_been_taken"), TEAM_NEW, &f)
|
||
9 years ago
|
default:
|
||
8 years ago
|
c.Handle(500, "UpdateTeam", err)
|
||
10 years ago
|
}
|
||
|
return
|
||
|
}
|
||
8 years ago
|
c.Redirect(c.Org.OrgLink + "/teams/" + t.LowerName)
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func DeleteTeam(c *context.Context) {
|
||
|
if err := models.DeleteTeam(c.Org.Team); err != nil {
|
||
|
c.Flash.Error("DeleteTeam: " + err.Error())
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Flash.Success(c.Tr("org.teams.delete_team_success"))
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
c.JSON(200, map[string]interface{}{
|
||
|
"redirect": c.Org.OrgLink + "/teams",
|
||
9 years ago
|
})
|
||
11 years ago
|
}
|