@@ -73,12 +73,19 @@ func (p *Permission) GetFirstUnitRepoID() int64 {
7373}
7474
7575// UnitAccessMode returns current user access mode to the specify unit of the repository
76- // It also considers "everyone access mode"
7776func (p * Permission ) UnitAccessMode (unitType unit.Type ) perm_model.AccessMode {
7877 // if the units map contains the access mode, use it, but admin/owner mode could override it
7978 if m , ok := p .unitsMode [unitType ]; ok {
8079 return util .Iif (p .AccessMode >= perm_model .AccessModeAdmin , p .AccessMode , m )
8180 }
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+ }
8289 // if the units map does not contain the access mode, return the default access mode if the unit exists
8390 unitDefaultAccessMode := max (p .AccessMode , p .everyoneAccessMode [unitType ])
8491 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
98105 return p .UnitAccessMode (unitType ) >= mode
99106}
100107
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+
101113// CanAccessAny returns true if user has mode access to any of the units of the repository
102114func (p * Permission ) CanAccessAny (mode perm_model.AccessMode , unitTypes ... unit.Type ) bool {
103115 for _ , u := range unitTypes {
@@ -177,6 +189,7 @@ func (p *Permission) LogString() string {
177189
178190func applyEveryoneRepoPermission (user * user_model.User , perm * Permission ) {
179191 if user == nil || user .ID <= 0 {
192+ perm .units = nil
180193 return
181194 }
182195 for _ , u := range perm .units {
@@ -187,6 +200,25 @@ func applyEveryoneRepoPermission(user *user_model.User, perm *Permission) {
187200 perm .everyoneAccessMode [u .Type ] = u .EveryoneAccessMode
188201 }
189202 }
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+ }
190222}
191223
192224// GetUserRepoPermission returns the user permissions to the repository
@@ -195,9 +227,7 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
195227 if err == nil {
196228 applyEveryoneRepoPermission (user , & perm )
197229 }
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 )
201231 }()
202232
203233 if err = repo .LoadUnits (ctx ); err != nil {
@@ -294,16 +324,6 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
294324 }
295325 }
296326
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-
307327 return perm , err
308328}
309329
0 commit comments