@@ -25,7 +25,8 @@ type Permission struct {
25
25
units []* repo_model.RepoUnit
26
26
unitsMode map [unit.Type ]perm_model.AccessMode
27
27
28
- everyoneAccessMode map [unit.Type ]perm_model.AccessMode
28
+ everyoneAccessMode map [unit.Type ]perm_model.AccessMode
29
+ anonymousAccessMode map [unit.Type ]perm_model.AccessMode
29
30
}
30
31
31
32
// IsOwner returns true if current user is the owner of repository.
@@ -39,7 +40,7 @@ func (p *Permission) IsAdmin() bool {
39
40
}
40
41
41
42
// 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".
43
44
func (p * Permission ) HasAnyUnitAccess () bool {
44
45
for _ , v := range p .unitsMode {
45
46
if v >= perm_model .AccessModeRead {
@@ -49,7 +50,12 @@ func (p *Permission) HasAnyUnitAccess() bool {
49
50
return p .AccessMode >= perm_model .AccessModeRead
50
51
}
51
52
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
+ }
53
59
for _ , v := range p .everyoneAccessMode {
54
60
if v >= perm_model .AccessModeRead {
55
61
return true
@@ -73,14 +79,16 @@ func (p *Permission) GetFirstUnitRepoID() int64 {
73
79
}
74
80
75
81
// 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"
77
83
func (p * Permission ) UnitAccessMode (unitType unit.Type ) perm_model.AccessMode {
78
84
// if the units map contains the access mode, use it, but admin/owner mode could override it
79
85
if m , ok := p .unitsMode [unitType ]; ok {
80
86
return util .Iif (p .AccessMode >= perm_model .AccessModeAdmin , p .AccessMode , m )
81
87
}
82
88
// 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 ])
84
92
hasUnit := slices .ContainsFunc (p .units , func (u * repo_model.RepoUnit ) bool { return u .Type == unitType })
85
93
return util .Iif (hasUnit , unitDefaultAccessMode , perm_model .AccessModeNone )
86
94
}
@@ -171,27 +179,38 @@ func (p *Permission) LogString() string {
171
179
format += "\n \t unitsMode[%-v]: %-v"
172
180
args = append (args , key .LogString (), value .LogString ())
173
181
}
182
+ format += "\n \t anonymousAccessMode: %-v"
183
+ args = append (args , p .anonymousAccessMode )
174
184
format += "\n \t everyoneAccessMode: %-v"
175
185
args = append (args , p .everyoneAccessMode )
176
186
format += "\n \t ]>"
177
187
return fmt .Sprintf (format , args ... )
178
188
}
179
189
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
+
180
199
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
+
181
205
if user == nil || user .ID <= 0 {
182
206
// for anonymous access, it could be:
183
207
// AccessMode is None or Read, units has repo units, unitModes is nil
184
208
return
185
209
}
186
210
187
- // apply everyone access permissions
211
+ // apply public ( everyone) access permissions
188
212
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 )
195
214
}
196
215
197
216
if perm .unitsMode == nil {
@@ -209,6 +228,11 @@ func finalProcessRepoUnitPermission(user *user_model.User, perm *Permission) {
209
228
break
210
229
}
211
230
}
231
+ for t := range perm .anonymousAccessMode {
232
+ if shouldKeep = shouldKeep || u .Type == t ; shouldKeep {
233
+ break
234
+ }
235
+ }
212
236
for t := range perm .everyoneAccessMode {
213
237
if shouldKeep = shouldKeep || u .Type == t ; shouldKeep {
214
238
break
0 commit comments