org.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package db
  5. import (
  6. "fmt"
  7. "xorm.io/builder"
  8. )
  9. // getOrgUsersByOrgID returns all organization-user relations by organization ID.
  10. func getOrgUsersByOrgID(e Engine, orgID int64, limit int) ([]*OrgUser, error) {
  11. orgUsers := make([]*OrgUser, 0, 10)
  12. sess := e.Where("org_id=?", orgID)
  13. if limit > 0 {
  14. sess = sess.Limit(limit)
  15. }
  16. return orgUsers, sess.Find(&orgUsers)
  17. }
  18. // GetUserMirrorRepositories returns mirror repositories of the organization which the user has access to.
  19. func (u *User) GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
  20. teamIDs, err := u.GetUserTeamIDs(userID)
  21. if err != nil {
  22. return nil, fmt.Errorf("GetUserTeamIDs: %v", err)
  23. }
  24. if len(teamIDs) == 0 {
  25. teamIDs = []int64{-1}
  26. }
  27. var teamRepoIDs []int64
  28. err = x.Table("team_repo").In("team_id", teamIDs).Distinct("repo_id").Find(&teamRepoIDs)
  29. if err != nil {
  30. return nil, fmt.Errorf("get team repository ids: %v", err)
  31. }
  32. if len(teamRepoIDs) == 0 {
  33. // team has no repo but "IN ()" is invalid SQL
  34. teamRepoIDs = []int64{-1} // there is no repo with id=-1
  35. }
  36. repos := make([]*Repository, 0, 10)
  37. if err = x.Where("owner_id = ?", u.ID).
  38. And("is_private = ?", false).
  39. Or(builder.In("id", teamRepoIDs)).
  40. And("is_mirror = ?", true). // Don't move up because it's an independent condition
  41. Desc("updated_unix").
  42. Find(&repos); err != nil {
  43. return nil, fmt.Errorf("get user repositories: %v", err)
  44. }
  45. return repos, nil
  46. }