gitea源码

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package organization
  4. import (
  5. "context"
  6. "code.gitea.io/gitea/models/db"
  7. "code.gitea.io/gitea/models/perm"
  8. "code.gitea.io/gitea/models/unit"
  9. "xorm.io/builder"
  10. )
  11. // TeamRepo represents an team-repository relation.
  12. type TeamRepo struct {
  13. ID int64 `xorm:"pk autoincr"`
  14. OrgID int64 `xorm:"INDEX"`
  15. TeamID int64 `xorm:"UNIQUE(s)"`
  16. RepoID int64 `xorm:"UNIQUE(s)"`
  17. }
  18. // HasTeamRepo returns true if given repository belongs to team.
  19. func HasTeamRepo(ctx context.Context, orgID, teamID, repoID int64) bool {
  20. has, _ := db.GetEngine(ctx).
  21. Where("org_id=?", orgID).
  22. And("team_id=?", teamID).
  23. And("repo_id=?", repoID).
  24. Get(new(TeamRepo))
  25. return has
  26. }
  27. // AddTeamRepo adds a repo for an organization's team
  28. func AddTeamRepo(ctx context.Context, orgID, teamID, repoID int64) error {
  29. _, err := db.GetEngine(ctx).Insert(&TeamRepo{
  30. OrgID: orgID,
  31. TeamID: teamID,
  32. RepoID: repoID,
  33. })
  34. return err
  35. }
  36. // RemoveTeamRepo remove repository from team
  37. func RemoveTeamRepo(ctx context.Context, teamID, repoID int64) error {
  38. _, err := db.DeleteByBean(ctx, &TeamRepo{
  39. TeamID: teamID,
  40. RepoID: repoID,
  41. })
  42. return err
  43. }
  44. // GetTeamsWithAccessToAnyRepoUnit returns all teams in an organization that have given access level to the repository special unit.
  45. // This function is only used for finding some teams that can be used as branch protection allowlist or reviewers, it isn't really used for access control.
  46. // FIXME: TEAM-UNIT-PERMISSION this logic is not complete, search the fixme keyword to see more details
  47. func GetTeamsWithAccessToAnyRepoUnit(ctx context.Context, orgID, repoID int64, mode perm.AccessMode, unitType unit.Type, unitTypesMore ...unit.Type) ([]*Team, error) {
  48. teams := make([]*Team, 0, 5)
  49. sub := builder.Select("team_id").From("team_unit").
  50. Where(builder.Expr("team_unit.team_id = team.id")).
  51. And(builder.In("team_unit.type", append([]unit.Type{unitType}, unitTypesMore...))).
  52. And(builder.Expr("team_unit.access_mode >= ?", mode))
  53. err := db.GetEngine(ctx).
  54. Join("INNER", "team_repo", "team_repo.team_id = team.id").
  55. And("team_repo.org_id = ?", orgID).
  56. And("team_repo.repo_id = ?", repoID).
  57. And(builder.Or(
  58. builder.Expr("team.authorize >= ?", mode),
  59. builder.In("team.id", sub),
  60. )).
  61. OrderBy("name").
  62. Find(&teams)
  63. return teams, err
  64. }