@@ -282,8 +282,8 @@ func teamUnitsRepoCond(id string, userID, orgID, teamID int64, units ...unit.Typ
282
282
))
283
283
}
284
284
285
- // userCollaborationRepoCond returns user as collabrators repositories list
286
- func userCollaborationRepoCond (idStr string , userID int64 ) builder.Cond {
285
+ // userAccessRepoCond returns a condition for selecting all repositories a user has access to
286
+ func userAccessRepoCond (idStr string , userID int64 ) builder.Cond {
287
287
return builder .In (idStr , builder .Select ("repo_id" ).
288
288
From ("`access`" ).
289
289
Where (builder .And (
@@ -293,6 +293,17 @@ func userCollaborationRepoCond(idStr string, userID int64) builder.Cond {
293
293
)
294
294
}
295
295
296
+ // userCollaborationRepoCond returns a condition for selecting all repositories a user is collaborator in
297
+ func userCollaborationRepoCond (idStr string , userID int64 ) builder.Cond {
298
+ return builder .In (idStr , builder .Select ("repo_id" ).
299
+ From ("`collaboration`" ).
300
+ Where (builder .And (
301
+ builder.Eq {"`collaboration`.user_id" : userID },
302
+ builder.Gt {"`collaboration`.mode" : int (perm .AccessModeNone )},
303
+ )),
304
+ )
305
+ }
306
+
296
307
// userOrgTeamRepoCond selects repos that the given user has access to through team membership
297
308
func userOrgTeamRepoCond (idStr string , userID int64 ) builder.Cond {
298
309
return builder .In (idStr , userOrgTeamRepoBuilder (userID ))
@@ -310,7 +321,13 @@ func userOrgTeamRepoBuilder(userID int64) *builder.Builder {
310
321
func userOrgTeamUnitRepoBuilder (userID int64 , unitType unit.Type ) * builder.Builder {
311
322
return userOrgTeamRepoBuilder (userID ).
312
323
Join ("INNER" , "team_unit" , "`team_unit`.team_id = `team_repo`.team_id" ).
313
- Where (builder.Eq {"`team_unit`.`type`" : unitType })
324
+ Where (builder.Eq {"`team_unit`.`type`" : unitType }).
325
+ And (builder.Gt {"`team_unit`.`access_mode`" : int (perm .AccessModeNone )})
326
+ }
327
+
328
+ // userOrgTeamUnitRepoCond returns a condition to select repo ids where user's teams can access the special unit.
329
+ func userOrgTeamUnitRepoCond (idStr string , userID int64 , unitType unit.Type ) builder.Cond {
330
+ return builder .In (idStr , userOrgTeamUnitRepoBuilder (userID , unitType ))
314
331
}
315
332
316
333
// userOrgUnitRepoCond selects repos that the given user has access to through org and the special unit
@@ -363,7 +380,7 @@ func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond {
363
380
if opts .Private {
364
381
if opts .Actor != nil && ! opts .Actor .IsAdmin && opts .Actor .ID != opts .OwnerID {
365
382
// OK we're in the context of a User
366
- cond = cond .And (accessibleRepositoryCondition (opts .Actor ))
383
+ cond = cond .And (accessibleRepositoryCondition (opts .Actor , unit . TypeInvalid ))
367
384
}
368
385
} else {
369
386
// Not looking at private organisations and users
@@ -409,7 +426,7 @@ func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond {
409
426
// 2. But we can see because of:
410
427
builder .Or (
411
428
// A. We have access
412
- userCollaborationRepoCond ("`repository`.id" , opts .OwnerID ),
429
+ userAccessRepoCond ("`repository`.id" , opts .OwnerID ),
413
430
// B. We are in a team for
414
431
userOrgTeamRepoCond ("`repository`.id" , opts .OwnerID ),
415
432
// C. Public repositories in organizations that we are member of
@@ -483,7 +500,7 @@ func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond {
483
500
}
484
501
485
502
if opts .Actor != nil && opts .Actor .IsRestricted {
486
- cond = cond .And (accessibleRepositoryCondition (opts .Actor ))
503
+ cond = cond .And (accessibleRepositoryCondition (opts .Actor , unit . TypeInvalid ))
487
504
}
488
505
489
506
if opts .Archived != util .OptionalBoolNone {
@@ -570,7 +587,7 @@ func searchRepositoryByCondition(opts *SearchRepoOptions, cond builder.Cond) (db
570
587
}
571
588
572
589
// accessibleRepositoryCondition takes a user a returns a condition for checking if a repository is accessible
573
- func accessibleRepositoryCondition (user * user_model.User ) builder.Cond {
590
+ func accessibleRepositoryCondition (user * user_model.User , unitType unit. Type ) builder.Cond {
574
591
cond := builder .NewCond ()
575
592
576
593
if user == nil || ! user .IsRestricted || user .ID <= 0 {
@@ -590,13 +607,24 @@ func accessibleRepositoryCondition(user *user_model.User) builder.Cond {
590
607
}
591
608
592
609
if user != nil {
610
+ // 2. Be able to see all repositories that we have access to
611
+ // 3. Be able to see all repositories through team membership(s)
612
+ if unitType == unit .TypeInvalid {
613
+ // Regardless of UnitType
614
+ cond = cond .Or (
615
+ userAccessRepoCond ("`repository`.id" , user .ID ),
616
+ userOrgTeamRepoCond ("`repository`.id" , user .ID ),
617
+ )
618
+ } else {
619
+ // For a specific UnitType
620
+ cond = cond .Or (
621
+ userAccessRepoCond ("`repository`.id" , user .ID ),
622
+ userOrgTeamUnitRepoCond ("`repository`.id" , user .ID , unitType ),
623
+ )
624
+ }
593
625
cond = cond .Or (
594
- // 2. Be able to see all repositories that we have access to
595
- userCollaborationRepoCond ("`repository`.id" , user .ID ),
596
- // 3. Repositories that we directly own
626
+ // 4. Repositories that we directly own
597
627
builder.Eq {"`repository`.owner_id" : user .ID },
598
- // 4. Be able to see all repositories that we are in a team
599
- userOrgTeamRepoCond ("`repository`.id" , user .ID ),
600
628
// 5. Be able to see all public repos in private organizations that we are an org_user of
601
629
userOrgPublicRepoCond (user .ID ),
602
630
)
@@ -641,18 +669,18 @@ func SearchRepositoryIDs(opts *SearchRepoOptions) ([]int64, int64, error) {
641
669
// AccessibleRepoIDsQuery queries accessible repository ids. Usable as a subquery wherever repo ids need to be filtered.
642
670
func AccessibleRepoIDsQuery (user * user_model.User ) * builder.Builder {
643
671
// NB: Please note this code needs to still work if user is nil
644
- return builder .Select ("id" ).From ("repository" ).Where (accessibleRepositoryCondition (user ))
672
+ return builder .Select ("id" ).From ("repository" ).Where (accessibleRepositoryCondition (user , unit . TypeInvalid ))
645
673
}
646
674
647
- // FindUserAccessibleRepoIDs find all accessible repositories' ID by user's id
648
- func FindUserAccessibleRepoIDs (user * user_model.User ) ([]int64 , error ) {
675
+ // FindUserCodeAccessibleRepoIDs find all accessible repositories' ID by user's id
676
+ func FindUserCodeAccessibleRepoIDs (user * user_model.User ) ([]int64 , error ) {
649
677
repoIDs := make ([]int64 , 0 , 10 )
650
678
if err := db .GetEngine (db .DefaultContext ).
651
679
Table ("repository" ).
652
680
Cols ("id" ).
653
- Where (accessibleRepositoryCondition (user )).
681
+ Where (accessibleRepositoryCondition (user , unit . TypeCode )).
654
682
Find (& repoIDs ); err != nil {
655
- return nil , fmt .Errorf ("FindUserAccesibleRepoIDs : %v" , err )
683
+ return nil , fmt .Errorf ("FindUserCodeAccesibleRepoIDs : %v" , err )
656
684
}
657
685
return repoIDs , nil
658
686
}
0 commit comments