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