Skip to content

Commit de06c3c

Browse files
committed
fix
1 parent 321cbcb commit de06c3c

File tree

4 files changed

+63
-22
lines changed

4 files changed

+63
-22
lines changed

models/perm/access/repo_permission.go

+36-12
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ type Permission struct {
2525
units []*repo_model.RepoUnit
2626
unitsMode map[unit.Type]perm_model.AccessMode
2727

28-
everyoneAccessMode map[unit.Type]perm_model.AccessMode
28+
everyoneAccessMode map[unit.Type]perm_model.AccessMode
29+
anonymousAccessMode map[unit.Type]perm_model.AccessMode
2930
}
3031

3132
// IsOwner returns true if current user is the owner of repository.
@@ -39,7 +40,7 @@ func (p *Permission) IsAdmin() bool {
3940
}
4041

4142
// HasAnyUnitAccess returns true if the user might have at least one access mode to any unit of this repository.
42-
// It doesn't count the "everyone access mode".
43+
// It doesn't count the "public(anonymous/everyone) access mode".
4344
func (p *Permission) HasAnyUnitAccess() bool {
4445
for _, v := range p.unitsMode {
4546
if v >= perm_model.AccessModeRead {
@@ -49,7 +50,12 @@ func (p *Permission) HasAnyUnitAccess() bool {
4950
return p.AccessMode >= perm_model.AccessModeRead
5051
}
5152

52-
func (p *Permission) HasAnyUnitAccessOrEveryoneAccess() bool {
53+
func (p *Permission) HasAnyUnitAccessOrPublicAccess() bool {
54+
for _, v := range p.anonymousAccessMode {
55+
if v >= perm_model.AccessModeRead {
56+
return true
57+
}
58+
}
5359
for _, v := range p.everyoneAccessMode {
5460
if v >= perm_model.AccessModeRead {
5561
return true
@@ -73,14 +79,16 @@ func (p *Permission) GetFirstUnitRepoID() int64 {
7379
}
7480

7581
// UnitAccessMode returns current user access mode to the specify unit of the repository
76-
// It also considers "everyone access mode"
82+
// It also considers "public (anonymous/everyone) access mode"
7783
func (p *Permission) UnitAccessMode(unitType unit.Type) perm_model.AccessMode {
7884
// if the units map contains the access mode, use it, but admin/owner mode could override it
7985
if m, ok := p.unitsMode[unitType]; ok {
8086
return util.Iif(p.AccessMode >= perm_model.AccessModeAdmin, p.AccessMode, m)
8187
}
8288
// if the units map does not contain the access mode, return the default access mode if the unit exists
83-
unitDefaultAccessMode := max(p.AccessMode, p.everyoneAccessMode[unitType])
89+
unitDefaultAccessMode := p.AccessMode
90+
unitDefaultAccessMode = max(unitDefaultAccessMode, p.anonymousAccessMode[unitType])
91+
unitDefaultAccessMode = max(unitDefaultAccessMode, p.everyoneAccessMode[unitType])
8492
hasUnit := slices.ContainsFunc(p.units, func(u *repo_model.RepoUnit) bool { return u.Type == unitType })
8593
return util.Iif(hasUnit, unitDefaultAccessMode, perm_model.AccessModeNone)
8694
}
@@ -171,27 +179,38 @@ func (p *Permission) LogString() string {
171179
format += "\n\tunitsMode[%-v]: %-v"
172180
args = append(args, key.LogString(), value.LogString())
173181
}
182+
format += "\n\tanonymousAccessMode: %-v"
183+
args = append(args, p.anonymousAccessMode)
174184
format += "\n\teveryoneAccessMode: %-v"
175185
args = append(args, p.everyoneAccessMode)
176186
format += "\n\t]>"
177187
return fmt.Sprintf(format, args...)
178188
}
179189

190+
func applyPublicAccessPermission(unitType unit.Type, accessMode perm_model.AccessMode, modeMap *map[unit.Type]perm_model.AccessMode) {
191+
if accessMode >= perm_model.AccessModeRead && accessMode > (*modeMap)[unitType] {
192+
if *modeMap == nil {
193+
*modeMap = make(map[unit.Type]perm_model.AccessMode)
194+
}
195+
(*modeMap)[unitType] = accessMode
196+
}
197+
}
198+
180199
func finalProcessRepoUnitPermission(user *user_model.User, perm *Permission) {
200+
// apply public (anonymous) access permissions
201+
for _, u := range perm.units {
202+
applyPublicAccessPermission(u.Type, u.AnonymousAccessMode, &perm.anonymousAccessMode)
203+
}
204+
181205
if user == nil || user.ID <= 0 {
182206
// for anonymous access, it could be:
183207
// AccessMode is None or Read, units has repo units, unitModes is nil
184208
return
185209
}
186210

187-
// apply everyone access permissions
211+
// apply public (everyone) access permissions
188212
for _, u := range perm.units {
189-
if u.EveryoneAccessMode >= perm_model.AccessModeRead && u.EveryoneAccessMode > perm.everyoneAccessMode[u.Type] {
190-
if perm.everyoneAccessMode == nil {
191-
perm.everyoneAccessMode = make(map[unit.Type]perm_model.AccessMode)
192-
}
193-
perm.everyoneAccessMode[u.Type] = u.EveryoneAccessMode
194-
}
213+
applyPublicAccessPermission(u.Type, u.EveryoneAccessMode, &perm.everyoneAccessMode)
195214
}
196215

197216
if perm.unitsMode == nil {
@@ -209,6 +228,11 @@ func finalProcessRepoUnitPermission(user *user_model.User, perm *Permission) {
209228
break
210229
}
211230
}
231+
for t := range perm.anonymousAccessMode {
232+
if shouldKeep = shouldKeep || u.Type == t; shouldKeep {
233+
break
234+
}
235+
}
212236
for t := range perm.everyoneAccessMode {
213237
if shouldKeep = shouldKeep || u.Type == t; shouldKeep {
214238
break

models/perm/access/repo_permission_test.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,21 @@ func TestHasAnyUnitAccess(t *testing.T) {
2222
units: []*repo_model.RepoUnit{{Type: unit.TypeWiki}},
2323
}
2424
assert.False(t, perm.HasAnyUnitAccess())
25-
assert.False(t, perm.HasAnyUnitAccessOrEveryoneAccess())
25+
assert.False(t, perm.HasAnyUnitAccessOrPublicAccess())
2626

2727
perm = Permission{
2828
units: []*repo_model.RepoUnit{{Type: unit.TypeWiki}},
2929
everyoneAccessMode: map[unit.Type]perm_model.AccessMode{unit.TypeIssues: perm_model.AccessModeRead},
3030
}
3131
assert.False(t, perm.HasAnyUnitAccess())
32-
assert.True(t, perm.HasAnyUnitAccessOrEveryoneAccess())
32+
assert.True(t, perm.HasAnyUnitAccessOrPublicAccess())
33+
34+
perm = Permission{
35+
units: []*repo_model.RepoUnit{{Type: unit.TypeWiki}},
36+
anonymousAccessMode: map[unit.Type]perm_model.AccessMode{unit.TypeIssues: perm_model.AccessModeRead},
37+
}
38+
assert.False(t, perm.HasAnyUnitAccess())
39+
assert.True(t, perm.HasAnyUnitAccessOrPublicAccess())
3340

3441
perm = Permission{
3542
AccessMode: perm_model.AccessModeRead,
@@ -43,7 +50,7 @@ func TestHasAnyUnitAccess(t *testing.T) {
4350
assert.True(t, perm.HasAnyUnitAccess())
4451
}
4552

46-
func TestApplyEveryoneRepoPermission(t *testing.T) {
53+
func TestApplyPublicAccessRepoPermission(t *testing.T) {
4754
perm := Permission{
4855
AccessMode: perm_model.AccessModeNone,
4956
units: []*repo_model.RepoUnit{
@@ -53,6 +60,15 @@ func TestApplyEveryoneRepoPermission(t *testing.T) {
5360
finalProcessRepoUnitPermission(nil, &perm)
5461
assert.False(t, perm.CanRead(unit.TypeWiki))
5562

63+
perm = Permission{
64+
AccessMode: perm_model.AccessModeNone,
65+
units: []*repo_model.RepoUnit{
66+
{Type: unit.TypeWiki, AnonymousAccessMode: perm_model.AccessModeRead},
67+
},
68+
}
69+
finalProcessRepoUnitPermission(nil, &perm)
70+
assert.True(t, perm.CanRead(unit.TypeWiki))
71+
5672
perm = Permission{
5773
AccessMode: perm_model.AccessModeNone,
5874
units: []*repo_model.RepoUnit{

models/repo/repo_unit.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ func (err ErrUnitTypeNotExist) Unwrap() error {
4242

4343
// RepoUnit describes all units of a repository
4444
type RepoUnit struct { //revive:disable-line:exported
45-
ID int64
46-
RepoID int64 `xorm:"INDEX(s)"`
47-
Type unit.Type `xorm:"INDEX(s)"`
48-
Config convert.Conversion `xorm:"TEXT"`
49-
CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"`
50-
EveryoneAccessMode perm.AccessMode `xorm:"NOT NULL DEFAULT 0"`
45+
ID int64
46+
RepoID int64 `xorm:"INDEX(s)"`
47+
Type unit.Type `xorm:"INDEX(s)"`
48+
Config convert.Conversion `xorm:"TEXT"`
49+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"`
50+
AnonymousAccessMode perm.AccessMode `xorm:"NOT NULL DEFAULT 0"`
51+
EveryoneAccessMode perm.AccessMode `xorm:"NOT NULL DEFAULT 0"`
5152
}
5253

5354
func init() {

services/context/repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func repoAssignment(ctx *Context, repo *repo_model.Repository) {
346346
return
347347
}
348348

349-
if !ctx.Repo.Permission.HasAnyUnitAccessOrEveryoneAccess() && !canWriteAsMaintainer(ctx) {
349+
if !ctx.Repo.Permission.HasAnyUnitAccessOrPublicAccess() && !canWriteAsMaintainer(ctx) {
350350
if ctx.FormString("go-get") == "1" {
351351
EarlyResponseForGoGetMeta(ctx)
352352
return

0 commit comments

Comments
 (0)