org.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. "xorm.io/xorm"
  9. )
  10. func getOrgsByUserID(sess *xorm.Session, userID int64, showAll bool) ([]*User, error) {
  11. orgs := make([]*User, 0, 10)
  12. if !showAll {
  13. sess.And("`org_user`.is_public=?", true)
  14. }
  15. return orgs, sess.And("`org_user`.uid=?", userID).
  16. Join("INNER", "`org_user`", "`org_user`.org_id=`user`.id").Find(&orgs)
  17. }
  18. // GetOrgsByUserID returns a list of organizations that the given user ID
  19. // has joined.
  20. func GetOrgsByUserID(userID int64, showAll bool) ([]*User, error) {
  21. return getOrgsByUserID(x.NewSession(), userID, showAll)
  22. }
  23. // getOwnedOrgsByUserID returns a list of organizations are owned by given user ID.
  24. func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) {
  25. orgs := make([]*User, 0, 10)
  26. return orgs, sess.Where("`org_user`.uid=?", userID).And("`org_user`.is_owner=?", true).
  27. Join("INNER", "`org_user`", "`org_user`.org_id=`user`.id").Find(&orgs)
  28. }
  29. // GetOwnedOrganizationsByUserIDDesc returns a list of organizations are owned by
  30. // given user ID, ordered descending by the given condition.
  31. func GetOwnedOrgsByUserIDDesc(userID int64, desc string) ([]*User, error) {
  32. sess := x.NewSession()
  33. return getOwnedOrgsByUserID(sess.Desc(desc), userID)
  34. }
  35. // getOrgUsersByOrgID returns all organization-user relations by organization ID.
  36. func getOrgUsersByOrgID(e Engine, orgID int64, limit int) ([]*OrgUser, error) {
  37. orgUsers := make([]*OrgUser, 0, 10)
  38. sess := e.Where("org_id=?", orgID)
  39. if limit > 0 {
  40. sess = sess.Limit(limit)
  41. }
  42. return orgUsers, sess.Find(&orgUsers)
  43. }
  44. // GetUserMirrorRepositories returns mirror repositories of the organization which the user has access to.
  45. func (u *User) GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
  46. teamIDs, err := u.GetUserTeamIDs(userID)
  47. if err != nil {
  48. return nil, fmt.Errorf("GetUserTeamIDs: %v", err)
  49. }
  50. if len(teamIDs) == 0 {
  51. teamIDs = []int64{-1}
  52. }
  53. var teamRepoIDs []int64
  54. err = x.Table("team_repo").In("team_id", teamIDs).Distinct("repo_id").Find(&teamRepoIDs)
  55. if err != nil {
  56. return nil, fmt.Errorf("get team repository ids: %v", err)
  57. }
  58. if len(teamRepoIDs) == 0 {
  59. // team has no repo but "IN ()" is invalid SQL
  60. teamRepoIDs = []int64{-1} // there is no repo with id=-1
  61. }
  62. repos := make([]*Repository, 0, 10)
  63. if err = x.Where("owner_id = ?", u.ID).
  64. And("is_private = ?", false).
  65. Or(builder.In("id", teamRepoIDs)).
  66. And("is_mirror = ?", true). // Don't move up because it's an independent condition
  67. Desc("updated_unix").
  68. Find(&repos); err != nil {
  69. return nil, fmt.Errorf("get user repositories: %v", err)
  70. }
  71. return repos, nil
  72. }