gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package organization
  4. import (
  5. "context"
  6. "strings"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/models/perm"
  9. "code.gitea.io/gitea/models/unit"
  10. "xorm.io/builder"
  11. )
  12. type TeamList []*Team
  13. func (t TeamList) LoadUnits(ctx context.Context) error {
  14. for _, team := range t {
  15. if err := team.LoadUnits(ctx); err != nil {
  16. return err
  17. }
  18. }
  19. return nil
  20. }
  21. func (t TeamList) UnitMaxAccess(tp unit.Type) perm.AccessMode {
  22. maxAccess := perm.AccessModeNone
  23. for _, team := range t {
  24. if team.IsOwnerTeam() {
  25. return perm.AccessModeOwner
  26. }
  27. for _, teamUnit := range team.Units {
  28. if teamUnit.Type != tp {
  29. continue
  30. }
  31. if teamUnit.AccessMode > maxAccess {
  32. maxAccess = teamUnit.AccessMode
  33. }
  34. }
  35. }
  36. return maxAccess
  37. }
  38. // SearchTeamOptions holds the search options
  39. type SearchTeamOptions struct {
  40. db.ListOptions
  41. UserID int64
  42. Keyword string
  43. OrgID int64
  44. IncludeDesc bool
  45. }
  46. func (opts *SearchTeamOptions) toCond() builder.Cond {
  47. cond := builder.NewCond()
  48. if len(opts.Keyword) > 0 {
  49. lowerKeyword := strings.ToLower(opts.Keyword)
  50. var keywordCond builder.Cond = builder.Like{"lower_name", lowerKeyword}
  51. if opts.IncludeDesc {
  52. keywordCond = keywordCond.Or(builder.Like{"LOWER(description)", lowerKeyword})
  53. }
  54. cond = cond.And(keywordCond)
  55. }
  56. if opts.OrgID > 0 {
  57. cond = cond.And(builder.Eq{"`team`.org_id": opts.OrgID})
  58. }
  59. if opts.UserID > 0 {
  60. cond = cond.And(builder.Eq{"team_user.uid": opts.UserID})
  61. }
  62. return cond
  63. }
  64. // SearchTeam search for teams. Caller is responsible to check permissions.
  65. func SearchTeam(ctx context.Context, opts *SearchTeamOptions) (TeamList, int64, error) {
  66. sess := db.GetEngine(ctx)
  67. opts.SetDefaultValues()
  68. cond := opts.toCond()
  69. if opts.UserID > 0 {
  70. sess = sess.Join("INNER", "team_user", "team_user.team_id = team.id")
  71. }
  72. sess = db.SetSessionPagination(sess, opts)
  73. teams := make([]*Team, 0, opts.PageSize)
  74. count, err := sess.Where(cond).OrderBy("lower_name").FindAndCount(&teams)
  75. if err != nil {
  76. return nil, 0, err
  77. }
  78. return teams, count, nil
  79. }
  80. // GetRepoTeams gets the list of teams that has access to the repository
  81. func GetRepoTeams(ctx context.Context, orgID, repoID int64) (teams TeamList, err error) {
  82. return teams, db.GetEngine(ctx).
  83. Join("INNER", "team_repo", "team_repo.team_id = team.id").
  84. Where("team.org_id = ?", orgID).
  85. And("team_repo.repo_id=?", repoID).
  86. OrderBy("CASE WHEN name LIKE '" + OwnerTeamName + "' THEN '' ELSE name END").
  87. Find(&teams)
  88. }
  89. // GetUserOrgTeams returns all teams that user belongs to in given organization.
  90. func GetUserOrgTeams(ctx context.Context, orgID, userID int64) (teams TeamList, err error) {
  91. return teams, db.GetEngine(ctx).
  92. Join("INNER", "team_user", "team_user.team_id = team.id").
  93. Where("team.org_id = ?", orgID).
  94. And("team_user.uid=?", userID).
  95. Find(&teams)
  96. }
  97. // GetUserRepoTeams returns user repo's teams
  98. func GetUserRepoTeams(ctx context.Context, orgID, userID, repoID int64) (teams TeamList, err error) {
  99. return teams, db.GetEngine(ctx).
  100. Join("INNER", "team_user", "team_user.team_id = team.id").
  101. Join("INNER", "team_repo", "team_repo.team_id = team.id").
  102. Where("team.org_id = ?", orgID).
  103. And("team_user.uid=?", userID).
  104. And("team_repo.repo_id=?", repoID).
  105. Find(&teams)
  106. }
  107. func GetTeamsByOrgIDs(ctx context.Context, orgIDs []int64) (TeamList, error) {
  108. teams := make([]*Team, 0, 10)
  109. return teams, db.GetEngine(ctx).Where(builder.In("org_id", orgIDs)).Find(&teams)
  110. }
  111. func GetTeamsByIDs(ctx context.Context, teamIDs []int64) (map[int64]*Team, error) {
  112. teams := make(map[int64]*Team, len(teamIDs))
  113. if len(teamIDs) == 0 {
  114. return teams, nil
  115. }
  116. return teams, db.GetEngine(ctx).Where(builder.In("`id`", teamIDs)).Find(&teams)
  117. }