Skip to content

Commit 0da3e73

Browse files
committed
Team permission allow different unit has different permission
1 parent 34b5436 commit 0da3e73

File tree

5 files changed

+91
-21
lines changed

5 files changed

+91
-21
lines changed

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,8 @@ var migrations = []Migration{
359359
NewMigration("Drop table remote_version (if exists)", dropTableRemoteVersion),
360360
// v202 -> v203
361361
NewMigration("Create key/value table for user settings", createUserSettingsTable),
362+
// v203 -> v204
363+
NewMigration("Add column authorize column for team_unit table", addAuthorizeColForTeamUnit),
362364
}
363365

364366
// GetCurrentDBVersion returns the current db version

models/migrations/v203.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2021 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 (
8+
"fmt"
9+
10+
"xorm.io/xorm"
11+
)
12+
13+
func addAuthorizeColForTeamUnit(x *xorm.Engine) error {
14+
type TeamUnit struct {
15+
ID int64 `xorm:"pk autoincr"`
16+
OrgID int64 `xorm:"INDEX"`
17+
TeamID int64 `xorm:"UNIQUE(s)"`
18+
Type int `xorm:"UNIQUE(s)"`
19+
Authorize int
20+
}
21+
22+
if err := x.Sync2(new(TeamUnit)); err != nil {
23+
return fmt.Errorf("sync2: %v", err)
24+
}
25+
26+
// migrate old permission
27+
_, err := x.Exec("UPDATE team_unit SET authorize = (SELECT authorize FROM team WHERE team.id = team_unit.team_id)")
28+
return err
29+
30+
}

models/org_team.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,19 @@ func (t *Team) UnitEnabled(tp unit.Type) bool {
441441
return t.unitEnabled(db.GetEngine(db.DefaultContext), tp)
442442
}
443443

444+
func (t *Team) unitAccessMode(e db.Engine, tp unit.Type) AccessMode {
445+
if err := t.getUnits(e); err != nil {
446+
log.Warn("Error loading team (ID: %d) units: %s", t.ID, err.Error())
447+
}
448+
449+
for _, unit := range t.Units {
450+
if unit.Type == tp {
451+
return unit.Authorize
452+
}
453+
}
454+
return AccessModeNone
455+
}
456+
444457
func (t *Team) unitEnabled(e db.Engine, tp unit.Type) bool {
445458
if err := t.getUnits(e); err != nil {
446459
log.Warn("Error loading team (ID: %d) units: %s", t.ID, err.Error())
@@ -1020,10 +1033,11 @@ func GetTeamsWithAccessToRepo(orgID, repoID int64, mode perm.AccessMode) ([]*Tea
10201033

10211034
// TeamUnit describes all units of a repository
10221035
type TeamUnit struct {
1023-
ID int64 `xorm:"pk autoincr"`
1024-
OrgID int64 `xorm:"INDEX"`
1025-
TeamID int64 `xorm:"UNIQUE(s)"`
1026-
Type unit.Type `xorm:"UNIQUE(s)"`
1036+
ID int64 `xorm:"pk autoincr"`
1037+
OrgID int64 `xorm:"INDEX"`
1038+
TeamID int64 `xorm:"UNIQUE(s)"`
1039+
Type unit.Type `xorm:"UNIQUE(s)"`
1040+
Authorize AccessMode
10271041
}
10281042

10291043
// Unit returns Unit

models/repo_permission.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,11 @@ func getUserRepoPermission(e db.Engine, repo *Repository, user *user_model.User)
245245
for _, u := range repo.Units {
246246
var found bool
247247
for _, team := range teams {
248-
if team.unitEnabled(e, u.Type) {
248+
teamMode := team.unitAccessMode(e, u.Type)
249+
if teamMode > AccessModeNone {
249250
m := perm.UnitsMode[u.Type]
250-
if m < team.Authorize {
251-
perm.UnitsMode[u.Type] = team.Authorize
251+
if m < teamMode {
252+
perm.UnitsMode[u.Type] = teamMode
252253
}
253254
found = true
254255
}

templates/org/team/new.tmpl

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,44 @@
8181
<div class="team-units required grouped field"{{if eq .Team.Authorize 3}} style="display: none"{{end}}>
8282
<label>{{.i18n.Tr "org.team_unit_desc"}}</label>
8383
<br>
84-
{{range $t, $unit := $.Units}}
85-
{{if $unit.Type.UnitGlobalDisabled}}
86-
<div class="field tooltip" data-content="{{$.i18n.Tr "repo.unit_disabled"}}">
87-
{{else}}
88-
<div class="field">
89-
{{end}}
90-
<div class="ui toggle checkbox">
91-
<input type="checkbox" class="hidden" name="units" value="{{$unit.Type.Value}}"{{if or (eq $.Team.ID 0) ($.Team.UnitEnabled $unit.Type)}} checked{{end}}>
92-
<label>{{$.i18n.Tr $unit.NameKey}}{{if $unit.Type.UnitGlobalDisabled}} {{$.i18n.Tr "org.team_unit_disabled"}}{{end}}</label>
93-
<span class="help">{{$.i18n.Tr $unit.DescKey}}</span>
94-
</div>
95-
</div>
96-
{{end}}
84+
<table class="ui celled table">
85+
<thead>
86+
<tr><th>Unit</th><th>{{.i18n.Tr "org.teams.none_access"}}</th><th>{{.i18n.Tr "org.teams.read_access"}}</th><th>{{.i18n.Tr "org.teams.write_access"}}</th></tr>
87+
</thead>
88+
<tbody>
89+
{{range $t, $unit := $.Units}}
90+
<tr><td>
91+
{{if $unit.Type.UnitGlobalDisabled}}
92+
<div class="field tooltip" data-content="{{$.i18n.Tr "repo.unit_disabled"}}">
93+
{{else}}
94+
<div class="field">
95+
{{end}}
96+
<div class="ui">
97+
<label>{{$.i18n.Tr $unit.NameKey}}{{if $unit.Type.UnitGlobalDisabled}} {{$.i18n.Tr "org.team_unit_disabled"}}{{end}}</label>
98+
<span class="help">{{$.i18n.Tr $unit.DescKey}}</span>
99+
</div>
100+
</div>
101+
</td>
102+
<td>
103+
104+
<div class="ui radio checkbox">
105+
<input type="radio" class="hidden" name="unit_{{$t}}" value="{{$unit.Type.Value}}"{{if or (eq $.Team.ID 0) ($.Team.UnitEnabled $unit.Type)}} checked{{end}}>
106+
</div>
107+
</td>
108+
<td>
109+
<div class="ui radio checkbox">
110+
<input type="radio" class="hidden" name="unit_{{$t}}" value="{{$unit.Type.Value}}"{{if or (eq $.Team.ID 0) ($.Team.UnitEnabled $unit.Type)}} checked{{end}}>
111+
</div>
112+
</td>
113+
<td>
114+
<div class="ui radio checkbox">
115+
<input type="radio" class="hidden" name="unit_{{$t}}" value="{{$unit.Type.Value}}"{{if or (eq $.Team.ID 0) ($.Team.UnitEnabled $unit.Type)}} checked{{end}}>
116+
</div>
117+
</td>
118+
{{end}}
119+
</tbody>
120+
</table>
97121
</div>
98-
<div class="ui divider"></div>
99122
{{end}}
100123

101124
<div class="field">

0 commit comments

Comments
 (0)