Skip to content

Commit fd6034a

Browse files
authored
Add units to team (#947)
* add units to team * fix lint * finish team setting backend * finished permission controll on routes * fix import blank line * add unit check on ssh/http pull and push and fix test failed * fix fixtures data * remove unused code
1 parent 5db5e16 commit fd6034a

File tree

18 files changed

+322
-69
lines changed

18 files changed

+322
-69
lines changed

cmd/serv.go

+8
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,10 @@ func runServ(c *cli.Context) error {
143143
reponame := strings.ToLower(strings.TrimSuffix(rr[1], ".git"))
144144

145145
isWiki := false
146+
unitType := models.UnitTypeCode
146147
if strings.HasSuffix(reponame, ".wiki") {
147148
isWiki = true
149+
unitType = models.UnitTypeWiki
148150
reponame = reponame[:len(reponame)-5]
149151
}
150152

@@ -248,6 +250,12 @@ func runServ(c *cli.Context) error {
248250
user.Name, requestedMode, repoPath)
249251
}
250252

253+
if !repo.CheckUnitUser(user.ID, unitType) {
254+
fail("You do not have allowed for this action",
255+
"User %s does not have allowed access to repository %s 's code",
256+
user.Name, repoPath)
257+
}
258+
251259
os.Setenv(models.EnvPusherName, user.Name)
252260
os.Setenv(models.EnvPusherID, fmt.Sprintf("%d", user.ID))
253261
}

models/fixtures/repo_unit.yml

+8
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,12 @@
1212
type: 2
1313
index: 0
1414
config: "{}"
15+
created_unix: 946684810
16+
17+
-
18+
id: 3
19+
repo_id: 1
20+
type: 7
21+
index: 0
22+
config: "{}"
1523
created_unix: 946684810

models/fixtures/team.yml

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
authorize: 4 # owner
77
num_repos: 2
88
num_members: 1
9+
unit_types: '[1,2,3,4,5,6,7,8,9]'
910

1011
-
1112
id: 2
@@ -15,6 +16,7 @@
1516
authorize: 2 # write
1617
num_repos: 1
1718
num_members: 2
19+
unit_types: '[1,2,3,4,5,6,7,8,9]'
1820

1921
-
2022
id: 3
@@ -24,6 +26,7 @@
2426
authorize: 4 # owner
2527
num_repos: 0
2628
num_members: 1
29+
unit_types: '[1,2,3,4,5,6,7,8,9]'
2730

2831
-
2932
id: 4
@@ -33,3 +36,4 @@
3336
authorize: 4 # owner
3437
num_repos: 0
3538
num_members: 1
39+
unit_types: '[1,2,3,4,5,6,7,8,9]'

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ var migrations = []Migration{
112112
NewMigration("add primary key to external login user", addExternalLoginUserPK),
113113
// 31 -> 32
114114
NewMigration("add field for login source synchronization", addLoginSourceSyncEnabledColumn),
115+
// v32 -> v33
116+
NewMigration("add units for team", addUnitsToRepoTeam),
115117
}
116118

117119
// Migrate database to current version

models/migrations/v31.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017 The Gogs Authors. All rights reserved.
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
22
// Use of this source code is governed by a MIT-style
33
// license that can be found in the LICENSE file.
44

models/migrations/v32.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import "github.com/go-xorm/xorm"
8+
9+
func addUnitsToRepoTeam(x *xorm.Engine) error {
10+
type Team struct {
11+
UnitTypes []int `xorm:"json"`
12+
}
13+
14+
err := x.Sync(new(Team))
15+
if err != nil {
16+
return err
17+
}
18+
19+
_, err = x.Update(&Team{
20+
UnitTypes: []int{1, 2, 3, 4, 5, 6, 7, 8, 9},
21+
})
22+
return err
23+
}

models/org_team.go

+22
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ type Team struct {
2424
Members []*User `xorm:"-"`
2525
NumRepos int
2626
NumMembers int
27+
UnitTypes []UnitType `xorm:"json"`
28+
}
29+
30+
// GetUnitTypes returns unit types the team owned, empty means all the unit types
31+
func (t *Team) GetUnitTypes() []UnitType {
32+
if len(t.UnitTypes) == 0 {
33+
return allRepUnitTypes
34+
}
35+
return t.UnitTypes
2736
}
2837

2938
// IsOwnerTeam returns true if team is owner team.
@@ -183,6 +192,19 @@ func (t *Team) RemoveRepository(repoID int64) error {
183192
return sess.Commit()
184193
}
185194

195+
// EnableUnit returns if the team enables unit type t
196+
func (t *Team) EnableUnit(tp UnitType) bool {
197+
if len(t.UnitTypes) == 0 {
198+
return true
199+
}
200+
for _, u := range t.UnitTypes {
201+
if u == tp {
202+
return true
203+
}
204+
}
205+
return false
206+
}
207+
186208
// IsUsableTeamName tests if a name could be as team name
187209
func IsUsableTeamName(name string) error {
188210
switch name {

models/repo.go

+56-2
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,61 @@ func (repo *Repository) getUnits(e Engine) (err error) {
329329
return err
330330
}
331331

332-
func getUnitsByRepoID(e Engine, repoID int64) (units []*RepoUnit, err error) {
333-
return units, e.Where("repo_id = ?", repoID).Find(&units)
332+
// CheckUnitUser check whether user could visit the unit of this repository
333+
func (repo *Repository) CheckUnitUser(userID int64, unitType UnitType) bool {
334+
if err := repo.getUnitsByUserID(x, userID); err != nil {
335+
return false
336+
}
337+
338+
for _, unit := range repo.Units {
339+
if unit.Type == unitType {
340+
return true
341+
}
342+
}
343+
return false
344+
}
345+
346+
// LoadUnitsByUserID loads units according userID's permissions
347+
func (repo *Repository) LoadUnitsByUserID(userID int64) error {
348+
return repo.getUnitsByUserID(x, userID)
349+
}
350+
351+
func (repo *Repository) getUnitsByUserID(e Engine, userID int64) (err error) {
352+
if repo.Units != nil {
353+
return nil
354+
}
355+
356+
err = repo.getUnits(e)
357+
if err != nil {
358+
return err
359+
}
360+
361+
if !repo.Owner.IsOrganization() || userID == 0 {
362+
return nil
363+
}
364+
365+
teams, err := getUserTeams(e, repo.OwnerID, userID)
366+
if err != nil {
367+
return err
368+
}
369+
370+
var allTypes = make(map[UnitType]struct{}, len(allRepUnitTypes))
371+
for _, team := range teams {
372+
for _, unitType := range team.UnitTypes {
373+
allTypes[unitType] = struct{}{}
374+
}
375+
}
376+
377+
// unique
378+
var newRepoUnits = make([]*RepoUnit, 0, len(repo.Units))
379+
for _, u := range repo.Units {
380+
if _, ok := allTypes[u.Type]; ok {
381+
newRepoUnits = append(newRepoUnits, u)
382+
}
383+
}
384+
385+
repo.Units = newRepoUnits
386+
return nil
334387
}
335388

336389
// EnableUnit if this repository enabled some unit
@@ -1595,6 +1648,7 @@ func DeleteRepository(uid, repoID int64) error {
15951648
&Release{RepoID: repoID},
15961649
&Collaboration{RepoID: repoID},
15971650
&PullRequest{BaseRepoID: repoID},
1651+
&RepoUnit{RepoID: repoID},
15981652
); err != nil {
15991653
return fmt.Errorf("deleteBeans: %v", err)
16001654
}

models/repo_unit.go

+8
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,11 @@ func (r *RepoUnit) ExternalWikiConfig() *ExternalWikiConfig {
135135
func (r *RepoUnit) ExternalTrackerConfig() *ExternalTrackerConfig {
136136
return r.Config.(*ExternalTrackerConfig)
137137
}
138+
139+
func getUnitsByRepoID(e Engine, repoID int64) (units []*RepoUnit, err error) {
140+
return units, e.Where("repo_id = ?", repoID).Find(&units)
141+
}
142+
143+
func getUnitsByRepoIDAndIDs(e Engine, repoID int64, types []UnitType) (units []*RepoUnit, err error) {
144+
return units, e.Where("repo_id = ?", repoID).In("`type`", types).Find(&units)
145+
}

models/unit.go

+50-30
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,40 @@ const (
2020
UnitTypeExternalTracker // 9 ExternalTracker
2121
)
2222

23+
var (
24+
// allRepUnitTypes contains all the unit types
25+
allRepUnitTypes = []UnitType{
26+
UnitTypeCode,
27+
UnitTypeIssues,
28+
UnitTypePullRequests,
29+
UnitTypeCommits,
30+
UnitTypeReleases,
31+
UnitTypeWiki,
32+
UnitTypeSettings,
33+
UnitTypeExternalWiki,
34+
UnitTypeExternalTracker,
35+
}
36+
37+
// defaultRepoUnits contains the default unit types
38+
defaultRepoUnits = []UnitType{
39+
UnitTypeCode,
40+
UnitTypeIssues,
41+
UnitTypePullRequests,
42+
UnitTypeCommits,
43+
UnitTypeReleases,
44+
UnitTypeWiki,
45+
UnitTypeSettings,
46+
}
47+
48+
// MustRepoUnits contains the units could be disabled currently
49+
MustRepoUnits = []UnitType{
50+
UnitTypeCode,
51+
UnitTypeCommits,
52+
UnitTypeReleases,
53+
UnitTypeSettings,
54+
}
55+
)
56+
2357
// Unit is a tab page of one repository
2458
type Unit struct {
2559
Type UnitType
@@ -29,99 +63,85 @@ type Unit struct {
2963
Idx int
3064
}
3165

66+
// CanDisable returns if this unit could be disabled.
67+
func (u *Unit) CanDisable() bool {
68+
return u.Type != UnitTypeSettings
69+
}
70+
3271
// Enumerate all the units
3372
var (
3473
UnitCode = Unit{
3574
UnitTypeCode,
3675
"repo.code",
3776
"/",
38-
"repo.code_desc",
77+
"repo.code.desc",
3978
0,
4079
}
4180

4281
UnitIssues = Unit{
4382
UnitTypeIssues,
4483
"repo.issues",
4584
"/issues",
46-
"repo.issues_desc",
85+
"repo.issues.desc",
4786
1,
4887
}
4988

5089
UnitExternalTracker = Unit{
5190
UnitTypeExternalTracker,
52-
"repo.issues",
91+
"repo.ext_issues",
5392
"/issues",
54-
"repo.issues_desc",
93+
"repo.ext_issues.desc",
5594
1,
5695
}
5796

5897
UnitPullRequests = Unit{
5998
UnitTypePullRequests,
6099
"repo.pulls",
61100
"/pulls",
62-
"repo.pulls_desc",
101+
"repo.pulls.desc",
63102
2,
64103
}
65104

66105
UnitCommits = Unit{
67106
UnitTypeCommits,
68107
"repo.commits",
69108
"/commits/master",
70-
"repo.commits_desc",
109+
"repo.commits.desc",
71110
3,
72111
}
73112

74113
UnitReleases = Unit{
75114
UnitTypeReleases,
76115
"repo.releases",
77116
"/releases",
78-
"repo.releases_desc",
117+
"repo.releases.desc",
79118
4,
80119
}
81120

82121
UnitWiki = Unit{
83122
UnitTypeWiki,
84123
"repo.wiki",
85124
"/wiki",
86-
"repo.wiki_desc",
125+
"repo.wiki.desc",
87126
5,
88127
}
89128

90129
UnitExternalWiki = Unit{
91130
UnitTypeExternalWiki,
92-
"repo.wiki",
131+
"repo.ext_wiki",
93132
"/wiki",
94-
"repo.wiki_desc",
133+
"repo.ext_wiki.desc",
95134
5,
96135
}
97136

98137
UnitSettings = Unit{
99138
UnitTypeSettings,
100139
"repo.settings",
101140
"/settings",
102-
"repo.settings_desc",
141+
"repo.settings.desc",
103142
6,
104143
}
105144

106-
// defaultRepoUnits contains all the default unit types
107-
defaultRepoUnits = []UnitType{
108-
UnitTypeCode,
109-
UnitTypeIssues,
110-
UnitTypePullRequests,
111-
UnitTypeCommits,
112-
UnitTypeReleases,
113-
UnitTypeWiki,
114-
UnitTypeSettings,
115-
}
116-
117-
// MustRepoUnits contains the units could be disabled currently
118-
MustRepoUnits = []UnitType{
119-
UnitTypeCode,
120-
UnitTypeCommits,
121-
UnitTypeReleases,
122-
UnitTypeSettings,
123-
}
124-
125145
// Units contains all the units
126146
Units = map[UnitType]Unit{
127147
UnitTypeCode: UnitCode,

0 commit comments

Comments
 (0)