1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
// 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.
package org
import (
"github.com/go-martini/martini"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/middleware"
)
const (
TEAMS base.TplName = "org/teams"
TEAM_NEW base.TplName = "org/team_new"
)
func Teams(ctx *middleware.Context, params martini.Params) {
ctx.Data["Title"] = "Organization " + params["org"] + " Teams"
org, err := models.GetUserByName(params["org"])
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)
}
func NewTeam(ctx *middleware.Context, params martini.Params) {
org, err := models.GetUserByName(params["org"])
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, params martini.Params, form auth.CreateTeamForm) {
org, err := models.GetUserByName(params["org"])
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)
}
func EditTeam(ctx *middleware.Context, params martini.Params) {
ctx.Data["Title"] = "Organization " + params["org"] + " Edit Team"
ctx.HTML(200, "org/edit_team")
}
|