From cab512160814519bebfecada8a0f1eb154e77e5b Mon Sep 17 00:00:00 2001 From: David Schneiderbauer Date: Sun, 3 Jun 2018 14:16:02 +0200 Subject: [PATCH] limit GetUserRepositoryIDs by accessible unittypes --- models/user.go | 36 ++++++++++++++++++++++++++---------- routers/user/home.go | 6 +++++- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/models/user.go b/models/user.go index 33c0658bc..653e99426 100644 --- a/models/user.go +++ b/models/user.go @@ -546,30 +546,46 @@ func (u *User) GetRepositories(page, pageSize int) (err error) { return err } -// GetRepositoryIDs returns repositories IDs where user owned -func (u *User) GetRepositoryIDs() ([]int64, error) { +// GetRepositoryIDs returns repositories IDs where user owned and has unittypes +func (u *User) GetRepositoryIDs(units ...UnitType) ([]int64, error) { var ids []int64 - return ids, x.Table("repository").Cols("id").Where("owner_id = ?", u.ID).Find(&ids) + + sess := x.Table("repository").Cols("repository.id") + + if len(units) > 0 { + sess = sess.Join("INNER", "repo_unit", "repository.id = repo_unit.repo_id") + sess = sess.In("repo_unit.type", units) + } + + return ids, sess.Where("owner_id = ?", u.ID).Find(&ids) } -// GetOrgRepositoryIDs returns repositories IDs where user's team owned -func (u *User) GetOrgRepositoryIDs() ([]int64, error) { +// GetOrgRepositoryIDs returns repositories IDs where user's team owned and has unittypes +func (u *User) GetOrgRepositoryIDs(units ...UnitType) ([]int64, error) { var ids []int64 - return ids, x.Table("repository"). + + sess := x.Table("repository"). Cols("repository.id"). Join("INNER", "team_user", "repository.owner_id = team_user.org_id"). - Join("INNER", "team_repo", "repository.is_private != ? OR (team_user.team_id = team_repo.team_id AND repository.id = team_repo.repo_id)", true). + Join("INNER", "team_repo", "repository.is_private != ? OR (team_user.team_id = team_repo.team_id AND repository.id = team_repo.repo_id)", true) + + if len(units) > 0 { + sess = sess.Join("INNER", "team_unit", "team_unit.team_id = team_user.team_id") + sess = sess.In("team_unit.type", units) + } + + return ids, sess. Where("team_user.uid = ?", u.ID). GroupBy("repository.id").Find(&ids) } // GetAccessRepoIDs returns all repositories IDs where user's or user is a team member organizations -func (u *User) GetAccessRepoIDs() ([]int64, error) { - ids, err := u.GetRepositoryIDs() +func (u *User) GetAccessRepoIDs(units ...UnitType) ([]int64, error) { + ids, err := u.GetRepositoryIDs(units...) if err != nil { return nil, err } - ids2, err := u.GetOrgRepositoryIDs() + ids2, err := u.GetOrgRepositoryIDs(units...) if err != nil { return nil, err } diff --git a/routers/user/home.go b/routers/user/home.go index 2a193bbde..0c84b2498 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -203,7 +203,11 @@ func Issues(ctx *context.Context) { return } } else { - userRepoIDs, err = ctxUser.GetAccessRepoIDs() + unitType := models.UnitTypeIssues + if isPullList { + unitType = models.UnitTypePullRequests + } + userRepoIDs, err = ctxUser.GetAccessRepoIDs(unitType) if err != nil { ctx.ServerError("ctxUser.GetAccessRepoIDs", err) return