@@ -73,12 +73,19 @@ func (p *Permission) GetFirstUnitRepoID() int64 {
73
73
}
74
74
75
75
// UnitAccessMode returns current user access mode to the specify unit of the repository
76
- // It also considers "everyone access mode"
77
76
func (p * Permission ) UnitAccessMode (unitType unit.Type ) perm_model.AccessMode {
78
77
// if the units map contains the access mode, use it, but admin/owner mode could override it
79
78
if m , ok := p .unitsMode [unitType ]; ok {
80
79
return util .Iif (p .AccessMode >= perm_model .AccessModeAdmin , p .AccessMode , m )
81
80
}
81
+ return p .AccessMode
82
+ }
83
+
84
+ // UnitAccessModeWithEveryone works like UnitAccessMode, it also considers "everyone access mode"
85
+ func (p * Permission ) UnitAccessModeWithEveryone (unitType unit.Type ) perm_model.AccessMode {
86
+ if m := p .UnitAccessMode (unitType ); m > perm_model .AccessModeNone {
87
+ return m
88
+ }
82
89
// if the units map does not contain the access mode, return the default access mode if the unit exists
83
90
unitDefaultAccessMode := max (p .AccessMode , p .everyoneAccessMode [unitType ])
84
91
hasUnit := slices .ContainsFunc (p .units , func (u * repo_model.RepoUnit ) bool { return u .Type == unitType })
@@ -98,6 +105,11 @@ func (p *Permission) CanAccess(mode perm_model.AccessMode, unitType unit.Type) b
98
105
return p .UnitAccessMode (unitType ) >= mode
99
106
}
100
107
108
+ // CanAccessWithEveryone works like CanAccess but also considers "everyone access mode"
109
+ func (p * Permission ) CanAccessWithEveryone (mode perm_model.AccessMode , unitType unit.Type ) bool {
110
+ return p .UnitAccessModeWithEveryone (unitType ) >= mode
111
+ }
112
+
101
113
// CanAccessAny returns true if user has mode access to any of the units of the repository
102
114
func (p * Permission ) CanAccessAny (mode perm_model.AccessMode , unitTypes ... unit.Type ) bool {
103
115
for _ , u := range unitTypes {
@@ -177,6 +189,7 @@ func (p *Permission) LogString() string {
177
189
178
190
func applyEveryoneRepoPermission (user * user_model.User , perm * Permission ) {
179
191
if user == nil || user .ID <= 0 {
192
+ perm .units = nil
180
193
return
181
194
}
182
195
for _ , u := range perm .units {
@@ -187,6 +200,25 @@ func applyEveryoneRepoPermission(user *user_model.User, perm *Permission) {
187
200
perm .everyoneAccessMode [u .Type ] = u .EveryoneAccessMode
188
201
}
189
202
}
203
+ // remove no permission units
204
+ origPermUnits := perm .units
205
+ perm .units = make ([]* repo_model.RepoUnit , 0 , len (perm .units ))
206
+ for _ , u := range origPermUnits {
207
+ shouldKeep := false
208
+ for t := range perm .unitsMode {
209
+ if shouldKeep = u .Type == t ; shouldKeep {
210
+ break
211
+ }
212
+ }
213
+ for t := range perm .everyoneAccessMode {
214
+ if shouldKeep = shouldKeep || u .Type == t ; shouldKeep {
215
+ break
216
+ }
217
+ }
218
+ if shouldKeep {
219
+ perm .units = append (perm .units , u )
220
+ }
221
+ }
190
222
}
191
223
192
224
// GetUserRepoPermission returns the user permissions to the repository
@@ -195,9 +227,7 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
195
227
if err == nil {
196
228
applyEveryoneRepoPermission (user , & perm )
197
229
}
198
- if log .IsTrace () {
199
- log .Trace ("Permission Loaded for user %-v in repo %-v, permissions: %-+v" , user , repo , perm )
200
- }
230
+ log .Trace ("Permission Loaded for user %-v in repo %-v, permissions: %-+v" , user , repo , perm )
201
231
}()
202
232
203
233
if err = repo .LoadUnits (ctx ); err != nil {
@@ -294,16 +324,6 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
294
324
}
295
325
}
296
326
297
- // remove no permission units
298
- perm .units = make ([]* repo_model.RepoUnit , 0 , len (repo .Units ))
299
- for t := range perm .unitsMode {
300
- for _ , u := range repo .Units {
301
- if u .Type == t {
302
- perm .units = append (perm .units , u )
303
- }
304
- }
305
- }
306
-
307
327
return perm , err
308
328
}
309
329
0 commit comments