mirror of
https://github.com/gogs/gogs.git
synced 2025-05-31 11:42:13 +00:00
models/repo: allow SearchRepos to return private but accessible repositories (#4273)
* models/repo.go SearchRepositoryByName() Updated function to return public and private repositories that the logged in user has been given rights to view issue #3088 * models/repo.go SearchRepositoryName changed repository table alias to 'repo' removed debug line * models/repo.go SearchRepositoryByName modified UserID search query to use the "access" table instead of team_repo, team_user etc * models/repo.go SearchRepositoryByName 1) uppercased SQL keywords 2) removed alias for ACCESS table
This commit is contained in:
parent
44a6b63316
commit
aaadc61ee8
@ -1570,6 +1570,7 @@ func GetRepositoryCount(u *User) (int64, error) {
|
|||||||
type SearchRepoOptions struct {
|
type SearchRepoOptions struct {
|
||||||
Keyword string
|
Keyword string
|
||||||
OwnerID int64
|
OwnerID int64
|
||||||
|
UserID int64 // if set results will contain all public/private repositories user has access to
|
||||||
OrderBy string
|
OrderBy string
|
||||||
Private bool // Include private repositories in results
|
Private bool // Include private repositories in results
|
||||||
Page int
|
Page int
|
||||||
@ -1589,14 +1590,22 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int
|
|||||||
}
|
}
|
||||||
|
|
||||||
repos = make([]*Repository, 0, opts.PageSize)
|
repos = make([]*Repository, 0, opts.PageSize)
|
||||||
|
sess := x.Alias("repo")
|
||||||
// Append conditions
|
// Attempt to find repositories that opts.UserId has access to
|
||||||
sess := x.Where("LOWER(lower_name) LIKE ?", "%"+opts.Keyword+"%")
|
// This does not include other people's private repositories even if opts.UserId is an admin
|
||||||
if opts.OwnerID > 0 {
|
if !opts.Private && opts.UserID > 0 {
|
||||||
sess.And("owner_id = ?", opts.OwnerID)
|
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 {
|
if opts.OwnerID > 0 {
|
||||||
sess.And("is_private=?", false)
|
sess.And("repo.owner_id=?", opts.OwnerID)
|
||||||
}
|
}
|
||||||
|
|
||||||
var countSess xorm.Session
|
var countSess xorm.Session
|
||||||
@ -1607,7 +1616,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos []*Repository, _ int
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(opts.OrderBy) > 0 {
|
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)
|
return repos, count, sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).Find(&repos)
|
||||||
}
|
}
|
||||||
|
@ -77,6 +77,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
|
|||||||
} else {
|
} else {
|
||||||
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
|
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
|
||||||
Keyword: keyword,
|
Keyword: keyword,
|
||||||
|
UserID: ctx.User.ID,
|
||||||
OrderBy: opts.OrderBy,
|
OrderBy: opts.OrderBy,
|
||||||
Private: opts.Private,
|
Private: opts.Private,
|
||||||
Page: page,
|
Page: page,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user