diff --git a/models/repo.go b/models/repo.go index 7fc24c29e..5cc0471c8 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1570,6 +1570,7 @@ func GetRepositoryCount(u *User) (int64, error) { type SearchRepoOptions struct { Keyword string OwnerID int64 + UserID int64 // if set results will contain all public/private repositories user has access to OrderBy string Private bool // Include private repositories in results Page int @@ -1589,14 +1590,22 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int } repos = make([]*Repository, 0, opts.PageSize) - - // Append conditions - sess := x.Where("LOWER(lower_name) LIKE ?", "%"+opts.Keyword+"%") - if opts.OwnerID > 0 { - sess.And("owner_id = ?", opts.OwnerID) + sess := x.Alias("repo") + // Attempt to find repositories that opts.UserId has access to + // This does not include other people's private repositories even if opts.UserId is an admin + if !opts.Private && opts.UserID > 0 { + sess.Join("LEFT", "access", "access.repo_id = repo.id") + sess.Where("repo.lower_name LIKE ? AND (repo.owner_id=? OR access.user_id=? OR repo.is_private=?)", + "%"+opts.Keyword+"%", opts.UserID, opts.UserID, false) + } else { + sess.Where("repo.lower_name LIKE ?", "%"+opts.Keyword+"%") + // only return public repositories if opts.Private is not set + if !opts.Private { + sess.And("repo.is_private=?", false) + } } - if !opts.Private { - sess.And("is_private=?", false) + if opts.OwnerID > 0 { + sess.And("repo.owner_id=?", opts.OwnerID) } var countSess xorm.Session @@ -1607,7 +1616,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int } if len(opts.OrderBy) > 0 { - sess.OrderBy(opts.OrderBy) + sess.OrderBy("repo." + opts.OrderBy) } return repos, count, sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).Find(&repos) } diff --git a/routers/home.go b/routers/home.go index 22a131a62..63a564903 100644 --- a/routers/home.go +++ b/routers/home.go @@ -77,6 +77,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { } else { repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{ Keyword: keyword, + UserID: ctx.User.ID, OrderBy: opts.OrderBy, Private: opts.Private, Page: page,