summaryrefslogtreecommitdiffstats
path: root/models/user.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/user.go')
-rw-r--r--models/user.go61
1 files changed, 23 insertions, 38 deletions
diff --git a/models/user.go b/models/user.go
index 030e23c383..8c4befb139 100644
--- a/models/user.go
+++ b/models/user.go
@@ -615,50 +615,35 @@ func (u *User) GetRepositories(page, pageSize int) (err error) {
return err
}
-// GetRepositoryIDs returns repositories IDs where user owned and has unittypes
-func (u *User) GetRepositoryIDs(units ...UnitType) ([]int64, error) {
- var ids []int64
-
- sess := x.Table("repository").Cols("repository.id")
+// UnitRepositoriesSubQuery returns repositories query builder according units
+func (u *User) UnitRepositoriesSubQuery(units ...UnitType) *builder.Builder {
+ b := builder.Select("repository.id").From("repository")
if len(units) > 0 {
- sess = sess.Join("INNER", "repo_unit", "repository.id = repo_unit.repo_id")
- sess = sess.In("repo_unit.type", units)
+ b.Join("INNER", "repo_unit", builder.Expr("repository.id = repo_unit.repo_id").
+ And(builder.In("repo_unit.type", units)),
+ )
}
-
- return ids, sess.Where("owner_id = ?", u.ID).Find(&ids)
+ return b.Where(builder.Eq{"repository.owner_id": u.ID})
}
-// GetOrgRepositoryIDs returns repositories IDs where user's team owned and has unittypes
-func (u *User) GetOrgRepositoryIDs(units ...UnitType) ([]int64, error) {
- var ids []int64
-
- 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)
-
+// OrgUnitRepositoriesSubQuery returns repositories query builder according orgnizations and units
+func (u *User) OrgUnitRepositoriesSubQuery(userID int64, units ...UnitType) *builder.Builder {
+ b := builder.
+ Select("team_repo.repo_id").
+ From("team_repo").
+ Join("INNER", "team_user", builder.Eq{"team_user.uid": userID}.And(
+ builder.Expr("team_user.team_id = team_repo.team_id"),
+ ))
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(units ...UnitType) ([]int64, error) {
- ids, err := u.GetRepositoryIDs(units...)
- if err != nil {
- return nil, err
- }
- ids2, err := u.GetOrgRepositoryIDs(units...)
- if err != nil {
- return nil, err
- }
- return append(ids, ids2...), nil
+ b.Join("INNER", "team_unit", builder.Eq{"team_unit.org_id": u.ID}.And(
+ builder.Expr("team_unit.team_id = team_repo.team_id").And(
+ builder.In("`type`", units),
+ ),
+ ))
+ }
+ return b.Where(builder.Eq{"team_repo.org_id": u.ID}).
+ GroupBy("team_repo.repo_id")
}
// GetMirrorRepositories returns mirror repositories that user owns, including private repositories.