Skip to content

Commit 1698005

Browse files
committed
add support for repo open issues or wiki
1 parent f21ae12 commit 1698005

File tree

8 files changed

+80
-29
lines changed

8 files changed

+80
-29
lines changed

models/repo_permission.go

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,51 @@ func (p *Permission) IsAdmin() bool {
2323

2424
// HasAccess returns true if the current user has at least read access to any unit of this repository
2525
func (p *Permission) HasAccess() bool {
26+
for _, u := range p.Units {
27+
if u.AllowAnonymous {
28+
return true
29+
}
30+
}
31+
2632
if p.UnitsMode == nil {
2733
return p.AccessMode >= AccessModeRead
2834
}
2935
return len(p.UnitsMode) > 0
3036
}
3137

32-
// UnitAccessMode returns current user accessmode to the specify unit of the repository
38+
// UnitAccessMode returns the unit's minial accessmode to be accessed
3339
func (p *Permission) UnitAccessMode(unitType UnitType) AccessMode {
34-
if p.UnitsMode == nil {
35-
for _, u := range p.Units {
36-
if u.Type == unitType {
37-
return p.AccessMode
40+
var found bool
41+
for _, u := range p.Units {
42+
if u.Type == unitType {
43+
if u.AllowAnonymous {
44+
return AccessModeRead
3845
}
46+
found = true
47+
}
48+
}
49+
50+
if p.UnitsMode == nil {
51+
if found {
52+
return p.AccessMode
3953
}
4054
return AccessModeNone
4155
}
4256
return p.UnitsMode[unitType]
4357
}
4458

59+
// CanAnonymousAccess returns true if anonymous access is enabled
60+
func (p *Permission) CanAnonymousAccess(unitType UnitType) bool {
61+
for _, u := range p.Units {
62+
if u.Type == unitType {
63+
if u.AllowAnonymous {
64+
return true
65+
}
66+
}
67+
}
68+
return false
69+
}
70+
4571
// CanAccess returns true if user has mode access to the unit of the repository
4672
func (p *Permission) CanAccess(mode AccessMode, unitType UnitType) bool {
4773
return p.UnitAccessMode(unitType) >= mode
@@ -96,22 +122,19 @@ func GetUserRepoPermission(repo *Repository, user *User) (Permission, error) {
96122
}
97123

98124
func getUserRepoPermission(e Engine, repo *Repository, user *User) (perm Permission, err error) {
99-
// anonymous user visit private repo.
100-
// TODO: anonymous user visit public unit of private repo???
101-
if user == nil && repo.IsPrivate {
102-
perm.AccessMode = AccessModeNone
103-
return
104-
}
105-
125+
// always load repo unit so that we can read unit's allow_anonymous
106126
if err = repo.getUnits(e); err != nil {
107127
return
108128
}
109-
110129
perm.Units = repo.Units
111130

112-
// anonymous visit public repo
131+
// for anonymous user
113132
if user == nil {
114-
perm.AccessMode = AccessModeRead
133+
if repo.IsPrivate {
134+
perm.AccessMode = AccessModeNone
135+
} else {
136+
perm.AccessMode = AccessModeRead
137+
}
115138
return
116139
}
117140

models/repo_unit.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ import (
1616

1717
// RepoUnit describes all units of a repository
1818
type RepoUnit struct {
19-
ID int64
20-
RepoID int64 `xorm:"INDEX(s)"`
21-
Type UnitType `xorm:"INDEX(s)"`
22-
Config core.Conversion `xorm:"TEXT"`
23-
CreatedUnix util.TimeStamp `xorm:"INDEX CREATED"`
19+
ID int64
20+
RepoID int64 `xorm:"INDEX(s)"`
21+
Type UnitType `xorm:"INDEX(s)"`
22+
Config core.Conversion `xorm:"TEXT"`
23+
AllowAnonymous bool
24+
CreatedUnix util.TimeStamp `xorm:"INDEX CREATED"`
2425
}
2526

2627
// UnitConfig describes common unit config

modules/auth/repo_form.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ type RepoSettingForm struct {
117117
EnableTimetracker bool
118118
AllowOnlyContributorsToTrackTime bool
119119
EnableIssueDependencies bool
120+
AllowAnonymousIssues bool
121+
AllowAnonymousUncyclo bool
120122
IsArchived bool
121123

122124
// Admin settings

modules/context/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func repoAssignment(ctx *Context, repo *models.Repository) {
219219
}
220220

221221
// Check access.
222-
if ctx.Repo.Permission.AccessMode == models.AccessModeNone {
222+
if !ctx.Repo.Permission.HasAccess() {
223223
if ctx.Query("go-get") == "1" {
224224
EarlyResponseForGoGetMeta(ctx)
225225
return

options/locale/locale_en-US.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,8 @@ settings.tracker_issue_style = External Issue Tracker Number Format
10211021
settings.tracker_issue_style.numeric = Numeric
10221022
settings.tracker_issue_style.alphanumeric = Alphanumeric
10231023
settings.tracker_url_format_desc = Use the placeholders <code>{user}</code>, <code>{repo}</code> and <code>{index}</code> for the username, repository name and issue index.
1024+
settings.anonymous_access_issues = Allow Anonymous Access Issues
1025+
settings.anonymous_access_wiki = Allow Anonymous Access Uncyclo
10241026
settings.enable_timetracker = Enable Time Tracking
10251027
settings.allow_only_contributors_to_track_time = Let Only Contributors Track Time
10261028
settings.pulls_desc = Enable Repository Pull Requests

routers/repo/setting.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,10 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
178178
})
179179
} else {
180180
units = append(units, models.RepoUnit{
181-
RepoID: repo.ID,
182-
Type: models.UnitTypeUncyclo,
183-
Config: new(models.UnitConfig),
181+
RepoID: repo.ID,
182+
Type: models.UnitTypeUncyclo,
183+
Config: new(models.UnitConfig),
184+
AllowAnonymous: form.AllowAnonymousUncyclo,
184185
})
185186
}
186187
}
@@ -215,6 +216,7 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
215216
AllowOnlyContributorsToTrackTime: form.AllowOnlyContributorsToTrackTime,
216217
EnableDependencies: form.EnableIssueDependencies,
217218
},
219+
AllowAnonymous: form.AllowAnonymousIssues,
218220
})
219221
}
220222
}

routers/repo/view.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,15 @@ func Home(ctx *context.Context) {
299299
var firstUnit *models.Unit
300300
for _, repoUnit := range ctx.Repo.Units {
301301
if repoUnit.Type == models.UnitTypeCode {
302-
renderCode(ctx)
303-
return
302+
if ctx.Repo.CanRead(models.UnitTypeCode) {
303+
renderCode(ctx)
304+
return
305+
}
306+
continue
304307
}
305308

306309
unit, ok := models.Units[repoUnit.Type]
307-
if ok && (firstUnit == nil || !firstUnit.IsLessThan(unit)) {
310+
if ok && ctx.Repo.CanRead(repoUnit.Type) && (firstUnit == nil || !firstUnit.IsLessThan(unit)) {
308311
firstUnit = &unit
309312
}
310313
}

templates/repo/settings/options.tmpl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,23 @@
104104
<div class="field {{if not $isUncycloEnabled}}disabled{{end}}" id="wiki_box">
105105
<div class="field">
106106
<div class="ui radio checkbox">
107-
<input class="hidden enable-system-radio" tabindex="0" name="enable_external_wiki" type="radio" value="false" data-target="#external_wiki_box" {{if not (.Repository.UnitEnabled $.UnitTypeExternalUncyclo)}}checked{{end}}/>
107+
<input class="hidden enable-system-radio" tabindex="0" name="enable_external_wiki" type="radio" value="false" data-context="#internal_wiki_box" data-target="#external_wiki_box" {{if not (.Repository.UnitEnabled $.UnitTypeExternalUncyclo)}}checked{{end}}/>
108108
<label>{{.i18n.Tr "repo.settings.use_internal_wiki"}}</label>
109109
</div>
110110
</div>
111+
<div class="field {{if (.Repository.UnitEnabled $.UnitTypeExternalUncyclo)}}disabled{{end}}" id="internal_wiki_box">
112+
{{if .Repository.IsPrivate}}
113+
<div class="field">
114+
<div class="ui checkbox">
115+
<input name="allow_anonymous_wiki" type="checkbox" {{if .Permission.CanAnonymousAccess $.UnitTypeUncyclo}}checked{{end}}>
116+
<label>{{.i18n.Tr "repo.settings.anonymous_access_wiki"}}</label>
117+
</div>
118+
</div>
119+
{{end}}
120+
</div>
111121
<div class="field">
112122
<div class="ui radio checkbox">
113-
<input class="hidden enable-system-radio" tabindex="0" name="enable_external_wiki" type="radio" value="true" data-target="#external_wiki_box" {{if .Repository.UnitEnabled $.UnitTypeExternalUncyclo}}checked{{end}}/>
123+
<input class="hidden enable-system-radio" tabindex="0" name="enable_external_wiki" type="radio" value="true" data-context="#internal_wiki_box" data-target="#external_wiki_box" {{if .Repository.UnitEnabled $.UnitTypeExternalUncyclo}}checked{{end}}/>
114124
<label>{{.i18n.Tr "repo.settings.use_external_wiki"}}</label>
115125
</div>
116126
</div>
@@ -159,6 +169,14 @@
159169
<label>{{.i18n.Tr "repo.issues.dependency.setting"}}</label>
160170
</div>
161171
</div>
172+
{{if .Repository.IsPrivate}}
173+
<div class="field">
174+
<div class="ui checkbox">
175+
<input name="allow_anonymous_issues" type="checkbox" {{if .Permission.CanAnonymousAccess $.UnitTypeIssues}}checked{{end}}>
176+
<label>{{.i18n.Tr "repo.settings.anonymous_access_issues"}}</label>
177+
</div>
178+
</div>
179+
{{end}}
162180
</div>
163181
<div class="field">
164182
<div class="ui radio checkbox">

0 commit comments

Comments
 (0)