You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

team_list.go 3.3KB

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