fix: Admin can see all private repositories on Explore page. (#1026)

* fix: Admin can see all private repositories on Explore page.

* refactor: fix session
This commit is contained in:
Bo-Yi Wu 2017-02-26 13:59:31 +08:00 committed by Lunny Xiao
parent 831ff41754
commit 95574a3640
4 changed files with 39 additions and 25 deletions

View file

@ -116,7 +116,7 @@ func createIssueIndexer() error {
// populateIssueIndexer populate the issue indexer with issue data // populateIssueIndexer populate the issue indexer with issue data
func populateIssueIndexer() error { func populateIssueIndexer() error {
for page := 1; ; page++ { for page := 1; ; page++ {
repos, err := Repositories(&SearchRepoOptions{ repos, _, err := Repositories(&SearchRepoOptions{
Page: page, Page: page,
PageSize: 10, PageSize: 10,
}) })

View file

@ -1231,7 +1231,7 @@ func CountUserRepositories(userID int64, private bool) int64 {
} }
// Repositories returns all repositories // Repositories returns all repositories
func Repositories(opts *SearchRepoOptions) (_ RepositoryList, err error) { func Repositories(opts *SearchRepoOptions) (_ RepositoryList, count int64, err error) {
if len(opts.OrderBy) == 0 { if len(opts.OrderBy) == 0 {
opts.OrderBy = "id ASC" opts.OrderBy = "id ASC"
} }
@ -1242,14 +1242,16 @@ func Repositories(opts *SearchRepoOptions) (_ RepositoryList, err error) {
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
OrderBy(opts.OrderBy). OrderBy(opts.OrderBy).
Find(&repos); err != nil { Find(&repos); err != nil {
return nil, fmt.Errorf("Repo: %v", err) return nil, 0, fmt.Errorf("Repo: %v", err)
} }
if err = repos.loadAttributes(x); err != nil { if err = repos.loadAttributes(x); err != nil {
return nil, fmt.Errorf("LoadAttributes: %v", err) return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
} }
return repos, nil count = countRepositories(-1, opts.Private)
return repos, count, nil
} }
// RepoPath returns repository path by given user and repository name. // RepoPath returns repository path by given user and repository name.
@ -1757,40 +1759,54 @@ func GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
} }
// GetRecentUpdatedRepositories returns the list of repositories that are recently updated. // GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList, err error) { func GetRecentUpdatedRepositories(opts *SearchRepoOptions) (repos RepositoryList, _ int64, _ error) {
var cond = builder.NewCond()
if len(opts.OrderBy) == 0 { if len(opts.OrderBy) == 0 {
opts.OrderBy = "updated_unix DESC" opts.OrderBy = "updated_unix DESC"
} }
sess := x.Where("is_private=?", false). if !opts.Private {
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize). cond = builder.Eq{
Limit(opts.PageSize) "is_private": false,
}
}
if opts.Searcher != nil { if opts.Searcher != nil && !opts.Searcher.IsAdmin {
sess.Or("owner_id = ?", opts.Searcher.ID) var ownerIds []int64
ownerIds = append(ownerIds, opts.Searcher.ID)
err := opts.Searcher.GetOrganizations(true) err := opts.Searcher.GetOrganizations(true)
if err != nil { if err != nil {
return nil, fmt.Errorf("Organization: %v", err) return nil, 0, fmt.Errorf("Organization: %v", err)
} }
for _, org := range opts.Searcher.Orgs { for _, org := range opts.Searcher.Orgs {
sess.Or("owner_id = ?", org.ID) ownerIds = append(ownerIds, org.ID)
}
} }
if err = sess. cond = cond.Or(builder.In("owner_id", ownerIds))
}
count, err := x.Where(cond).Count(new(Repository))
if err != nil {
return nil, 0, fmt.Errorf("Count: %v", err)
}
if err = x.Where(cond).
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
Limit(opts.PageSize).
OrderBy(opts.OrderBy). OrderBy(opts.OrderBy).
Find(&repos); err != nil { Find(&repos); err != nil {
return nil, fmt.Errorf("Repo: %v", err) return nil, 0, fmt.Errorf("Repo: %v", err)
} }
if err = repos.loadAttributes(x); err != nil { if err = repos.loadAttributes(x); err != nil {
return nil, fmt.Errorf("LoadAttributes: %v", err) return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
} }
return repos, nil return repos, count, nil
} }
func getRepositoryCount(e Engine, u *User) (int64, error) { func getRepositoryCount(e Engine, u *User) (int64, error) {

View file

@ -24,7 +24,6 @@ func Repos(ctx *context.Context) {
ctx.Data["PageIsAdminRepositories"] = true ctx.Data["PageIsAdminRepositories"] = true
routers.RenderRepoSearch(ctx, &routers.RepoSearchOptions{ routers.RenderRepoSearch(ctx, &routers.RepoSearchOptions{
Counter: models.CountRepositories,
Ranger: models.Repositories, Ranger: models.Repositories,
Private: true, Private: true,
PageSize: setting.UI.Admin.RepoPagingNum, PageSize: setting.UI.Admin.RepoPagingNum,

View file

@ -53,8 +53,7 @@ func Home(ctx *context.Context) {
// RepoSearchOptions when calling search repositories // RepoSearchOptions when calling search repositories
type RepoSearchOptions struct { type RepoSearchOptions struct {
Counter func(bool) int64 Ranger func(*models.SearchRepoOptions) (models.RepositoryList, int64, error)
Ranger func(*models.SearchRepoOptions) (models.RepositoryList, error)
Searcher *models.User Searcher *models.User
Private bool Private bool
PageSize int PageSize int
@ -101,17 +100,17 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
keyword := strings.Trim(ctx.Query("q"), " ") keyword := strings.Trim(ctx.Query("q"), " ")
if len(keyword) == 0 { if len(keyword) == 0 {
repos, err = opts.Ranger(&models.SearchRepoOptions{ repos, count, err = opts.Ranger(&models.SearchRepoOptions{
Page: page, Page: page,
PageSize: opts.PageSize, PageSize: opts.PageSize,
Searcher: ctx.User, Searcher: ctx.User,
OrderBy: orderBy, OrderBy: orderBy,
Private: opts.Private,
}) })
if err != nil { if err != nil {
ctx.Handle(500, "opts.Ranger", err) ctx.Handle(500, "opts.Ranger", err)
return return
} }
count = opts.Counter(opts.Private)
} else { } else {
if isKeywordValid(keyword) { if isKeywordValid(keyword) {
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{ repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
@ -143,10 +142,10 @@ func ExploreRepos(ctx *context.Context) {
ctx.Data["PageIsExploreRepositories"] = true ctx.Data["PageIsExploreRepositories"] = true
RenderRepoSearch(ctx, &RepoSearchOptions{ RenderRepoSearch(ctx, &RepoSearchOptions{
Counter: models.CountRepositories,
Ranger: models.GetRecentUpdatedRepositories, Ranger: models.GetRecentUpdatedRepositories,
PageSize: setting.UI.ExplorePagingNum, PageSize: setting.UI.ExplorePagingNum,
Searcher: ctx.User, Searcher: ctx.User,
Private: ctx.User != nil && ctx.User.IsAdmin,
TplName: tplExploreRepos, TplName: tplExploreRepos,
}) })
} }
@ -175,7 +174,6 @@ func RenderUserSearch(ctx *context.Context, opts *UserSearchOptions) {
) )
ctx.Data["SortType"] = ctx.Query("sort") ctx.Data["SortType"] = ctx.Query("sort")
//OrderBy: "id ASC",
switch ctx.Query("sort") { switch ctx.Query("sort") {
case "oldest": case "oldest":
orderBy = "id ASC" orderBy = "id ASC"
@ -193,7 +191,8 @@ func RenderUserSearch(ctx *context.Context, opts *UserSearchOptions) {
keyword := strings.Trim(ctx.Query("q"), " ") keyword := strings.Trim(ctx.Query("q"), " ")
if len(keyword) == 0 { if len(keyword) == 0 {
users, err = opts.Ranger(&models.SearchUserOptions{OrderBy: orderBy, users, err = opts.Ranger(&models.SearchUserOptions{
OrderBy: orderBy,
Page: page, Page: page,
PageSize: opts.PageSize, PageSize: opts.PageSize,
}) })