Skip to content

Commit f292923

Browse files
committed
Finish the interface and the logic
1 parent 0da3e73 commit f292923

File tree

5 files changed

+87
-58
lines changed

5 files changed

+87
-58
lines changed

models/org_team.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -441,30 +441,35 @@ 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 {
444+
func (t *Team) unitEnabled(e db.Engine, tp unit.Type) bool {
445445
if err := t.getUnits(e); err != nil {
446446
log.Warn("Error loading team (ID: %d) units: %s", t.ID, err.Error())
447447
}
448448

449449
for _, unit := range t.Units {
450450
if unit.Type == tp {
451-
return unit.Authorize
451+
return true
452452
}
453453
}
454-
return AccessModeNone
454+
return false
455455
}
456456

457-
func (t *Team) unitEnabled(e db.Engine, tp unit.Type) bool {
457+
// UnitAccessMode returns if the team has the given unit type enabled
458+
func (t *Team) UnitAccessMode(tp unit.Type) AccessMode {
459+
return t.unitAccessMode(db.GetEngine(db.DefaultContext), tp)
460+
}
461+
462+
func (t *Team) unitAccessMode(e db.Engine, tp unit.Type) AccessMode {
458463
if err := t.getUnits(e); err != nil {
459464
log.Warn("Error loading team (ID: %d) units: %s", t.ID, err.Error())
460465
}
461466

462467
for _, unit := range t.Units {
463468
if unit.Type == tp {
464-
return true
469+
return unit.Authorize
465470
}
466471
}
467-
return false
472+
return AccessModeNone
468473
}
469474

470475
// IsUsableTeamName tests if a name could be as team name

options/locale/locale_en-US.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,6 +2234,10 @@ teams.leave = Leave
22342234
teams.leave.detail = Leave %s?
22352235
teams.can_create_org_repo = Create repositories
22362236
teams.can_create_org_repo_helper = Members can create new repositories in organization. Creator will get administrator access to the new repository.
2237+
teams.none_access = None
2238+
teams.none_access_helper = Members can view or do any other action on this unit.
2239+
teams.general_access = General Access
2240+
teams.general_access_helper = Members permissions will be decided by below permission table.
22372241
teams.read_access = Read Access
22382242
teams.read_access_helper = Members can view and clone team repositories.
22392243
teams.write_access = Write Access
@@ -2857,5 +2861,6 @@ error.probable_bad_signature = "WARNING! Although there is a key with this ID in
28572861
error.probable_bad_default_signature = "WARNING! Although the default key has this ID it does not verify this commit! This commit is SUSPICIOUS."
28582862
28592863
[units]
2864+
unit = Unit
28602865
error.no_unit_allowed_repo = You are not allowed to access any section of this repository.
28612866
error.unit_not_allowed = You are not allowed to access this repository section.

routers/web/org/teams.go

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import (
99
"net/http"
1010
"net/url"
1111
"path"
12+
"strconv"
1213
"strings"
1314

1415
"code.gitea.io/gitea/models"
1516
"code.gitea.io/gitea/models/perm"
17+
"code.gitea.io/gitea/models/unit"
1618
unit_model "code.gitea.io/gitea/models/unit"
1719
user_model "code.gitea.io/gitea/models/user"
1820
"code.gitea.io/gitea/modules/base"
@@ -231,6 +233,16 @@ func NewTeamPost(ctx *context.Context) {
231233
ctx.Data["PageIsOrgTeamsNew"] = true
232234
ctx.Data["Units"] = unit_model.Units
233235
var includesAllRepositories = form.RepoAccess == "all"
236+
var unitPerms = make(map[unit.Type]models.AccessMode)
237+
for k, v := range ctx.Req.Form {
238+
if strings.HasPrefix(k, "unit_") {
239+
t, _ := strconv.Atoi(k[5:])
240+
if t > 0 {
241+
vv, _ := strconv.Atoi(v[0])
242+
unitPerms[unit.Type(t)] = models.AccessMode(vv)
243+
}
244+
}
245+
}
234246

235247
t := &models.Team{
236248
OrgID: ctx.Org.Organization.ID,
@@ -242,11 +254,12 @@ func NewTeamPost(ctx *context.Context) {
242254
}
243255

244256
if t.Authorize < perm.AccessModeOwner {
245-
var units = make([]*models.TeamUnit, 0, len(form.Units))
246-
for _, tp := range form.Units {
257+
var units = make([]*models.TeamUnit, 0, len(unitPerms))
258+
for tp, perm := range unitPerms {
247259
units = append(units, &models.TeamUnit{
248-
OrgID: ctx.Org.Organization.ID,
249-
Type: tp,
260+
OrgID: ctx.Org.Organization.ID,
261+
Type: tp,
262+
Authorize: perm,
250263
})
251264
}
252265
t.Units = units
@@ -259,7 +272,7 @@ func NewTeamPost(ctx *context.Context) {
259272
return
260273
}
261274

262-
if t.Authorize < perm.AccessModeAdmin && len(form.Units) == 0 {
275+
if t.Authorize < perm.AccessModeAdmin && len(unitPerms) == 0 {
263276
ctx.RenderWithErr(ctx.Tr("form.team_no_units_error"), tplTeamNew, &form)
264277
return
265278
}
@@ -321,6 +334,17 @@ func EditTeamPost(ctx *context.Context) {
321334
ctx.Data["Team"] = t
322335
ctx.Data["Units"] = unit_model.Units
323336

337+
var unitPerms = make(map[unit.Type]models.AccessMode)
338+
for k, v := range ctx.Req.Form {
339+
if strings.HasPrefix(k, "unit_") {
340+
t, _ := strconv.Atoi(k[5:])
341+
if t > 0 {
342+
vv, _ := strconv.Atoi(v[0])
343+
unitPerms[unit.Type(t)] = models.AccessMode(vv)
344+
}
345+
}
346+
}
347+
324348
isAuthChanged := false
325349
isIncludeAllChanged := false
326350
var includesAllRepositories = form.RepoAccess == "all"
@@ -341,12 +365,13 @@ func EditTeamPost(ctx *context.Context) {
341365
}
342366
t.Description = form.Description
343367
if t.Authorize < perm.AccessModeOwner {
344-
var units = make([]models.TeamUnit, 0, len(form.Units))
345-
for _, tp := range form.Units {
368+
var units = make([]models.TeamUnit, 0, len(unitPerms))
369+
for tp, perm := range unitPerms {
346370
units = append(units, models.TeamUnit{
347-
OrgID: t.OrgID,
348-
TeamID: t.ID,
349-
Type: tp,
371+
OrgID: t.OrgID,
372+
TeamID: t.ID,
373+
Type: tp,
374+
Authorize: perm,
350375
})
351376
}
352377
err := models.UpdateTeamUnits(t, units)
@@ -362,7 +387,7 @@ func EditTeamPost(ctx *context.Context) {
362387
return
363388
}
364389

365-
if t.Authorize < perm.AccessModeAdmin && len(form.Units) == 0 {
390+
if t.Authorize < perm.AccessModeAdmin && len(unitPerms) == 0 {
366391
ctx.RenderWithErr(ctx.Tr("form.team_no_units_error"), tplTeamNew, &form)
367392
return
368393
}

services/forms/org.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package forms
88
import (
99
"net/http"
1010

11-
"code.gitea.io/gitea/models/unit"
1211
"code.gitea.io/gitea/modules/context"
1312
"code.gitea.io/gitea/modules/structs"
1413
"code.gitea.io/gitea/modules/web/middleware"
@@ -66,7 +65,6 @@ type CreateTeamForm struct {
6665
TeamName string `binding:"Required;AlphaDashDot;MaxSize(30)"`
6766
Description string `binding:"MaxSize(255)"`
6867
Permission string
69-
Units []unit.Type
7068
RepoAccess string
7169
CanCreateOrgRepo bool
7270
}

templates/org/team/new.tmpl

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,9 @@
5656
<br>
5757
<div class="field">
5858
<div class="ui radio checkbox">
59-
<input type="radio" name="permission" value="read" {{if or .PageIsOrgTeamsNew (eq .Team.Authorize 1)}}checked{{end}}>
60-
<label>{{.i18n.Tr "org.teams.read_access"}}</label>
61-
<span class="help">{{.i18n.Tr "org.teams.read_access_helper"}}</span>
62-
</div>
63-
</div>
64-
<div class="field">
65-
<div class="ui radio checkbox">
66-
<input type="radio" name="permission" value="write" {{if eq .Team.Authorize 2}}checked{{end}}>
67-
<label>{{.i18n.Tr "org.teams.write_access"}}</label>
68-
<span class="help">{{.i18n.Tr "org.teams.write_access_helper"}}</span>
59+
<input type="radio" name="permission" value="{{if .PageIsOrgTeamsNew}}read{{else}}{{.Team.Authorize}}{{end}}" {{if or .PageIsOrgTeamsNew (eq .Team.Authorize 1) (eq .Team.Authorize 2)}}checked{{end}}>
60+
<label>{{.i18n.Tr "org.teams.general_access"}}</label>
61+
<span class="help">{{.i18n.Tr "org.teams.general_access_helper"}}</span>
6962
</div>
7063
</div>
7164
<div class="field">
@@ -80,41 +73,44 @@
8073

8174
<div class="team-units required grouped field"{{if eq .Team.Authorize 3}} style="display: none"{{end}}>
8275
<label>{{.i18n.Tr "org.team_unit_desc"}}</label>
83-
<br>
8476
<table class="ui celled table">
8577
<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>
78+
<tr><th>{{.i18n.Tr "units.unit"}}</th>
79+
<th><div data-content="{{.i18n.Tr "org.teams.none_access_helper"}}">{{.i18n.Tr "org.teams.none_access"}}</div></th>
80+
<th>{{.i18n.Tr "org.teams.read_access"}}</th>
81+
<th>{{.i18n.Tr "org.teams.write_access"}}</th></tr>
8782
</thead>
8883
<tbody>
8984
{{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}}>
85+
<tr>
86+
<td>
87+
{{if $unit.Type.UnitGlobalDisabled}}
88+
<div class="field tooltip" data-content="{{$.i18n.Tr "repo.unit_disabled"}}">
89+
{{else}}
90+
<div class="field">
91+
{{end}}
92+
<div class="ui">
93+
<label>{{$.i18n.Tr $unit.NameKey}}{{if $unit.Type.UnitGlobalDisabled}} {{$.i18n.Tr "org.team_unit_disabled"}}{{end}}</label>
94+
<span class="help">{{$.i18n.Tr $unit.DescKey}}</span>
95+
</div>
11696
</div>
117-
</td>
97+
</td>
98+
<td>
99+
<div class="ui radio checkbox">
100+
<input type="radio" class="hidden" name="unit_{{$unit.Type.Value}}" value="0"{{if or (eq $.Team.ID 0) (eq ($.Team.UnitAccessMode $unit.Type) 0)}} checked{{end}}>
101+
</div>
102+
</td>
103+
<td>
104+
<div class="ui radio checkbox">
105+
<input type="radio" class="hidden" name="unit_{{$unit.Type.Value}}" value="1"{{if or (eq $.Team.ID 0) (eq ($.Team.UnitAccessMode $unit.Type) 1)}} checked{{end}}>
106+
</div>
107+
</td>
108+
<td>
109+
<div class="ui radio checkbox">
110+
<input type="radio" class="hidden" name="unit_{{$unit.Type.Value}}" value="2"{{if or (eq $.Team.ID 0) (eq ($.Team.UnitAccessMode $unit.Type) 2)}} checked{{end}}>
111+
</div>
112+
</td>
113+
</tr>
118114
{{end}}
119115
</tbody>
120116
</table>

0 commit comments

Comments
 (0)