Skip to content

Commit 1d57306

Browse files
hoitihwxiaoguanglafriks
authored andcommitted
In code search, get code unit accessible repos in one (main) query (go-gitea#19764)
* When non-admin users use code search, get code unit accessible repos in one main query * Modified some comments to match the changes * Removed unnecessary check for Access Mode in Collaboration table Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: Lauris BH <[email protected]>
1 parent 2523753 commit 1d57306

File tree

5 files changed

+112
-113
lines changed

5 files changed

+112
-113
lines changed

models/git/lfs.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/models/db"
1313
"code.gitea.io/gitea/models/perm"
1414
repo_model "code.gitea.io/gitea/models/repo"
15+
"code.gitea.io/gitea/models/unit"
1516
user_model "code.gitea.io/gitea/models/user"
1617
"code.gitea.io/gitea/modules/lfs"
1718
"code.gitea.io/gitea/modules/log"
@@ -213,7 +214,7 @@ func LFSObjectAccessible(user *user_model.User, oid string) (bool, error) {
213214
count, err := db.GetEngine(db.DefaultContext).Count(&LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}})
214215
return count > 0, err
215216
}
216-
cond := repo_model.AccessibleRepositoryCondition(user)
217+
cond := repo_model.AccessibleRepositoryCondition(user, unit.TypeInvalid)
217218
count, err := db.GetEngine(db.DefaultContext).Where(cond).Join("INNER", "repository", "`lfs_meta_object`.repository_id = `repository`.id").Count(&LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}})
218219
return count > 0, err
219220
}
@@ -244,7 +245,7 @@ func LFSAutoAssociate(metas []*LFSMetaObject, user *user_model.User, repoID int6
244245
newMetas := make([]*LFSMetaObject, 0, len(metas))
245246
cond := builder.In(
246247
"`lfs_meta_object`.repository_id",
247-
builder.Select("`repository`.id").From("repository").Where(repo_model.AccessibleRepositoryCondition(user)),
248+
builder.Select("`repository`.id").From("repository").Where(repo_model.AccessibleRepositoryCondition(user, unit.TypeInvalid)),
248249
)
249250
err = sess.Cols("oid").Where(cond).In("oid", oids...).GroupBy("oid").Find(&newMetas)
250251
if err != nil {

models/issues/issue.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,7 @@ func issuePullAccessibleRepoCond(repoIDstr string, userID int64, org *organizati
14301430
cond = cond.And(
14311431
builder.Or(
14321432
repo_model.UserOwnedRepoCond(userID), // owned repos
1433-
repo_model.UserCollaborationRepoCond(repoIDstr, userID), // collaboration repos
1433+
repo_model.UserAccessRepoCond(repoIDstr, userID), // user can access repo in a unit independent way
14341434
repo_model.UserAssignedRepoCond(repoIDstr, userID), // user has been assigned accessible public repos
14351435
repo_model.UserMentionedRepoCond(repoIDstr, userID), // user has been mentioned accessible public repos
14361436
repo_model.UserCreateIssueRepoCond(repoIDstr, userID, isPull), // user has created issue/pr accessible public repos
@@ -1499,7 +1499,7 @@ func GetRepoIDsForIssuesOptions(opts *IssuesOptions, user *user_model.User) ([]i
14991499

15001500
opts.setupSessionNoLimit(sess)
15011501

1502-
accessCond := repo_model.AccessibleRepositoryCondition(user)
1502+
accessCond := repo_model.AccessibleRepositoryCondition(user, unit.TypeInvalid)
15031503
if err := sess.Where(accessCond).
15041504
Distinct("issue.repo_id").
15051505
Table("issue").

models/org.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/models/organization"
1515
access_model "code.gitea.io/gitea/models/perm/access"
1616
repo_model "code.gitea.io/gitea/models/repo"
17+
"code.gitea.io/gitea/models/unit"
1718
user_model "code.gitea.io/gitea/models/user"
1819

1920
"xorm.io/builder"
@@ -54,7 +55,7 @@ func GetUserOrgsList(user *user_model.User) ([]*MinimalOrg, error) {
5455
Join("LEFT", builder.
5556
Select("id as repo_id, owner_id as repo_owner_id").
5657
From("repository").
57-
Where(repo_model.AccessibleRepositoryCondition(user)), "`repository`.repo_owner_id = `team`.org_id").
58+
Where(repo_model.AccessibleRepositoryCondition(user, unit.TypeInvalid)), "`repository`.repo_owner_id = `team`.org_id").
5859
Where("`team_user`.uid = ?", user.ID).
5960
GroupBy(groupByStr)
6061

models/repo/repo_list.go

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ func UserMentionedRepoCond(id string, userID int64) builder.Cond {
269269
)
270270
}
271271

272-
// UserCollaborationRepoCond returns user as collabrators repositories list
273-
func UserCollaborationRepoCond(idStr string, userID int64) builder.Cond {
272+
// UserAccessRepoCond returns a condition for selecting all repositories a user has unit independent access to
273+
func UserAccessRepoCond(idStr string, userID int64) builder.Cond {
274274
return builder.In(idStr, builder.Select("repo_id").
275275
From("`access`").
276276
Where(builder.And(
@@ -280,8 +280,18 @@ func UserCollaborationRepoCond(idStr string, userID int64) builder.Cond {
280280
)
281281
}
282282

283-
// userOrgTeamRepoCond selects repos that the given user has access to through team membership
284-
func userOrgTeamRepoCond(idStr string, userID int64) builder.Cond {
283+
// userCollaborationRepoCond returns a condition for selecting all repositories a user is collaborator in
284+
func UserCollaborationRepoCond(idStr string, userID int64) builder.Cond {
285+
return builder.In(idStr, builder.Select("repo_id").
286+
From("`collaboration`").
287+
Where(builder.And(
288+
builder.Eq{"`collaboration`.user_id": userID},
289+
)),
290+
)
291+
}
292+
293+
// UserOrgTeamRepoCond selects repos that the given user has access to through team membership
294+
func UserOrgTeamRepoCond(idStr string, userID int64) builder.Cond {
285295
return builder.In(idStr, userOrgTeamRepoBuilder(userID))
286296
}
287297

@@ -297,7 +307,13 @@ func userOrgTeamRepoBuilder(userID int64) *builder.Builder {
297307
func userOrgTeamUnitRepoBuilder(userID int64, unitType unit.Type) *builder.Builder {
298308
return userOrgTeamRepoBuilder(userID).
299309
Join("INNER", "team_unit", "`team_unit`.team_id = `team_repo`.team_id").
300-
Where(builder.Eq{"`team_unit`.`type`": unitType})
310+
Where(builder.Eq{"`team_unit`.`type`": unitType}).
311+
And(builder.Gt{"`team_unit`.`access_mode`": int(perm.AccessModeNone)})
312+
}
313+
314+
// userOrgTeamUnitRepoCond returns a condition to select repo ids where user's teams can access the special unit.
315+
func userOrgTeamUnitRepoCond(idStr string, userID int64, unitType unit.Type) builder.Cond {
316+
return builder.In(idStr, userOrgTeamUnitRepoBuilder(userID, unitType))
301317
}
302318

303319
// UserOrgUnitRepoCond selects repos that the given user has access to through org and the special unit
@@ -350,7 +366,7 @@ func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond {
350366
if opts.Private {
351367
if opts.Actor != nil && !opts.Actor.IsAdmin && opts.Actor.ID != opts.OwnerID {
352368
// OK we're in the context of a User
353-
cond = cond.And(AccessibleRepositoryCondition(opts.Actor))
369+
cond = cond.And(AccessibleRepositoryCondition(opts.Actor, unit.TypeInvalid))
354370
}
355371
} else {
356372
// Not looking at private organisations and users
@@ -395,10 +411,10 @@ func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond {
395411
builder.Neq{"owner_id": opts.OwnerID},
396412
// 2. But we can see because of:
397413
builder.Or(
398-
// A. We have access
399-
UserCollaborationRepoCond("`repository`.id", opts.OwnerID),
414+
// A. We have unit independent access
415+
UserAccessRepoCond("`repository`.id", opts.OwnerID),
400416
// B. We are in a team for
401-
userOrgTeamRepoCond("`repository`.id", opts.OwnerID),
417+
UserOrgTeamRepoCond("`repository`.id", opts.OwnerID),
402418
// C. Public repositories in organizations that we are member of
403419
userOrgPublicRepoCondPrivate(opts.OwnerID),
404420
),
@@ -479,7 +495,7 @@ func SearchRepositoryCondition(opts *SearchRepoOptions) builder.Cond {
479495
}
480496

481497
if opts.Actor != nil && opts.Actor.IsRestricted {
482-
cond = cond.And(AccessibleRepositoryCondition(opts.Actor))
498+
cond = cond.And(AccessibleRepositoryCondition(opts.Actor, unit.TypeInvalid))
483499
}
484500

485501
if opts.Archived != util.OptionalBoolNone {
@@ -574,7 +590,7 @@ func searchRepositoryByCondition(ctx context.Context, opts *SearchRepoOptions, c
574590
}
575591

576592
// AccessibleRepositoryCondition takes a user a returns a condition for checking if a repository is accessible
577-
func AccessibleRepositoryCondition(user *user_model.User) builder.Cond {
593+
func AccessibleRepositoryCondition(user *user_model.User, unitType unit.Type) builder.Cond {
578594
cond := builder.NewCond()
579595

580596
if user == nil || !user.IsRestricted || user.ID <= 0 {
@@ -594,13 +610,24 @@ func AccessibleRepositoryCondition(user *user_model.User) builder.Cond {
594610
}
595611

596612
if user != nil {
613+
// 2. Be able to see all repositories that we have unit independent access to
614+
// 3. Be able to see all repositories through team membership(s)
615+
if unitType == unit.TypeInvalid {
616+
// Regardless of UnitType
617+
cond = cond.Or(
618+
UserAccessRepoCond("`repository`.id", user.ID),
619+
UserOrgTeamRepoCond("`repository`.id", user.ID),
620+
)
621+
} else {
622+
// For a specific UnitType
623+
cond = cond.Or(
624+
UserCollaborationRepoCond("`repository`.id", user.ID),
625+
userOrgTeamUnitRepoCond("`repository`.id", user.ID, unitType),
626+
)
627+
}
597628
cond = cond.Or(
598-
// 2. Be able to see all repositories that we have access to
599-
UserCollaborationRepoCond("`repository`.id", user.ID),
600-
// 3. Repositories that we directly own
629+
// 4. Repositories that we directly own
601630
builder.Eq{"`repository`.owner_id": user.ID},
602-
// 4. Be able to see all repositories that we are in a team
603-
userOrgTeamRepoCond("`repository`.id", user.ID),
604631
// 5. Be able to see all public repos in private organizations that we are an org_user of
605632
userOrgPublicRepoCond(user.ID),
606633
)
@@ -645,18 +672,18 @@ func SearchRepositoryIDs(opts *SearchRepoOptions) ([]int64, int64, error) {
645672
// AccessibleRepoIDsQuery queries accessible repository ids. Usable as a subquery wherever repo ids need to be filtered.
646673
func AccessibleRepoIDsQuery(user *user_model.User) *builder.Builder {
647674
// NB: Please note this code needs to still work if user is nil
648-
return builder.Select("id").From("repository").Where(AccessibleRepositoryCondition(user))
675+
return builder.Select("id").From("repository").Where(AccessibleRepositoryCondition(user, unit.TypeInvalid))
649676
}
650677

651-
// FindUserAccessibleRepoIDs find all accessible repositories' ID by user's id
652-
func FindUserAccessibleRepoIDs(user *user_model.User) ([]int64, error) {
678+
// FindUserCodeAccessibleRepoIDs finds all at Code level accessible repositories' ID by the user's id
679+
func FindUserCodeAccessibleRepoIDs(user *user_model.User) ([]int64, error) {
653680
repoIDs := make([]int64, 0, 10)
654681
if err := db.GetEngine(db.DefaultContext).
655682
Table("repository").
656683
Cols("id").
657-
Where(AccessibleRepositoryCondition(user)).
684+
Where(AccessibleRepositoryCondition(user, unit.TypeCode)).
658685
Find(&repoIDs); err != nil {
659-
return nil, fmt.Errorf("FindUserAccesibleRepoIDs: %v", err)
686+
return nil, fmt.Errorf("FindUserCodeAccesibleRepoIDs: %v", err)
660687
}
661688
return repoIDs, nil
662689
}

routers/web/explore/code.go

Lines changed: 57 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ package explore
77
import (
88
"net/http"
99

10-
"code.gitea.io/gitea/models"
1110
repo_model "code.gitea.io/gitea/models/repo"
12-
"code.gitea.io/gitea/models/unit"
1311
"code.gitea.io/gitea/modules/base"
1412
"code.gitea.io/gitea/modules/context"
1513
code_indexer "code.gitea.io/gitea/modules/indexer/code"
@@ -44,106 +42,78 @@ func Code(ctx *context.Context) {
4442
queryType := ctx.FormTrim("t")
4543
isMatch := queryType == "match"
4644

47-
var (
48-
repoIDs []int64
49-
err error
50-
isAdmin bool
51-
)
52-
if ctx.Doer != nil {
53-
isAdmin = ctx.Doer.IsAdmin
54-
}
55-
56-
// guest user or non-admin user
57-
if ctx.Doer == nil || !isAdmin {
58-
repoIDs, err = repo_model.FindUserAccessibleRepoIDs(ctx.Doer)
59-
if err != nil {
60-
ctx.ServerError("SearchResults", err)
61-
return
62-
}
63-
}
64-
65-
var (
66-
total int
67-
searchResults []*code_indexer.Result
68-
searchResultLanguages []*code_indexer.SearchResultLanguages
69-
)
70-
71-
// if non-admin login user, we need check UnitTypeCode at first
72-
if ctx.Doer != nil && len(repoIDs) > 0 {
73-
repoMaps, err := repo_model.GetRepositoriesMapByIDs(repoIDs)
74-
if err != nil {
75-
ctx.ServerError("SearchResults", err)
76-
return
45+
if keyword != "" {
46+
var (
47+
repoIDs []int64
48+
err error
49+
isAdmin bool
50+
)
51+
if ctx.Doer != nil {
52+
isAdmin = ctx.Doer.IsAdmin
7753
}
7854

79-
rightRepoMap := make(map[int64]*repo_model.Repository, len(repoMaps))
80-
repoIDs = make([]int64, 0, len(repoMaps))
81-
for id, repo := range repoMaps {
82-
if models.CheckRepoUnitUser(ctx, repo, ctx.Doer, unit.TypeCode) {
83-
rightRepoMap[id] = repo
84-
repoIDs = append(repoIDs, id)
85-
}
86-
}
87-
88-
ctx.Data["RepoMaps"] = rightRepoMap
89-
90-
total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(ctx, repoIDs, language, keyword, page, setting.UI.RepoSearchPagingNum, isMatch)
91-
if err != nil {
92-
if code_indexer.IsAvailable() {
55+
// guest user or non-admin user
56+
if ctx.Doer == nil || !isAdmin {
57+
repoIDs, err = repo_model.FindUserCodeAccessibleRepoIDs(ctx.Doer)
58+
if err != nil {
9359
ctx.ServerError("SearchResults", err)
9460
return
9561
}
96-
ctx.Data["CodeIndexerUnavailable"] = true
97-
} else {
98-
ctx.Data["CodeIndexerUnavailable"] = !code_indexer.IsAvailable()
9962
}
100-
// if non-login user or isAdmin, no need to check UnitTypeCode
101-
} else if (ctx.Doer == nil && len(repoIDs) > 0) || isAdmin {
102-
total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(ctx, repoIDs, language, keyword, page, setting.UI.RepoSearchPagingNum, isMatch)
103-
if err != nil {
104-
if code_indexer.IsAvailable() {
105-
ctx.ServerError("SearchResults", err)
106-
return
63+
64+
var (
65+
total int
66+
searchResults []*code_indexer.Result
67+
searchResultLanguages []*code_indexer.SearchResultLanguages
68+
)
69+
70+
if (len(repoIDs) > 0) || isAdmin {
71+
total, searchResults, searchResultLanguages, err = code_indexer.PerformSearch(ctx, repoIDs, language, keyword, page, setting.UI.RepoSearchPagingNum, isMatch)
72+
if err != nil {
73+
if code_indexer.IsAvailable() {
74+
ctx.ServerError("SearchResults", err)
75+
return
76+
}
77+
ctx.Data["CodeIndexerUnavailable"] = true
78+
} else {
79+
ctx.Data["CodeIndexerUnavailable"] = !code_indexer.IsAvailable()
10780
}
108-
ctx.Data["CodeIndexerUnavailable"] = true
109-
} else {
110-
ctx.Data["CodeIndexerUnavailable"] = !code_indexer.IsAvailable()
111-
}
11281

113-
loadRepoIDs := make([]int64, 0, len(searchResults))
114-
for _, result := range searchResults {
115-
var find bool
116-
for _, id := range loadRepoIDs {
117-
if id == result.RepoID {
118-
find = true
119-
break
82+
loadRepoIDs := make([]int64, 0, len(searchResults))
83+
for _, result := range searchResults {
84+
var find bool
85+
for _, id := range loadRepoIDs {
86+
if id == result.RepoID {
87+
find = true
88+
break
89+
}
90+
}
91+
if !find {
92+
loadRepoIDs = append(loadRepoIDs, result.RepoID)
12093
}
12194
}
122-
if !find {
123-
loadRepoIDs = append(loadRepoIDs, result.RepoID)
95+
96+
repoMaps, err := repo_model.GetRepositoriesMapByIDs(loadRepoIDs)
97+
if err != nil {
98+
ctx.ServerError("SearchResults", err)
99+
return
124100
}
125-
}
126101

127-
repoMaps, err := repo_model.GetRepositoriesMapByIDs(loadRepoIDs)
128-
if err != nil {
129-
ctx.ServerError("SearchResults", err)
130-
return
102+
ctx.Data["RepoMaps"] = repoMaps
131103
}
132104

133-
ctx.Data["RepoMaps"] = repoMaps
105+
ctx.Data["Keyword"] = keyword
106+
ctx.Data["Language"] = language
107+
ctx.Data["queryType"] = queryType
108+
ctx.Data["SearchResults"] = searchResults
109+
ctx.Data["SearchResultLanguages"] = searchResultLanguages
110+
ctx.Data["PageIsViewCode"] = true
111+
112+
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
113+
pager.SetDefaultParams(ctx)
114+
pager.AddParam(ctx, "l", "Language")
115+
ctx.Data["Page"] = pager
134116
}
135117

136-
ctx.Data["Keyword"] = keyword
137-
ctx.Data["Language"] = language
138-
ctx.Data["queryType"] = queryType
139-
ctx.Data["SearchResults"] = searchResults
140-
ctx.Data["SearchResultLanguages"] = searchResultLanguages
141-
ctx.Data["PageIsViewCode"] = true
142-
143-
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
144-
pager.SetDefaultParams(ctx)
145-
pager.AddParam(ctx, "l", "Language")
146-
ctx.Data["Page"] = pager
147-
148118
ctx.HTML(http.StatusOK, tplExploreCode)
149119
}

0 commit comments

Comments
 (0)