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.

collaboration.go 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "context"
  6. "fmt"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/models/perm"
  9. "code.gitea.io/gitea/models/unit"
  10. user_model "code.gitea.io/gitea/models/user"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/timeutil"
  13. )
  14. // Collaboration represent the relation between an individual and a repository.
  15. type Collaboration struct {
  16. ID int64 `xorm:"pk autoincr"`
  17. RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
  18. UserID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
  19. Mode perm.AccessMode `xorm:"DEFAULT 2 NOT NULL"`
  20. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  21. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  22. }
  23. func init() {
  24. db.RegisterModel(new(Collaboration))
  25. }
  26. // Collaborator represents a user with collaboration details.
  27. type Collaborator struct {
  28. *user_model.User
  29. Collaboration *Collaboration
  30. }
  31. // GetCollaborators returns the collaborators for a repository
  32. func GetCollaborators(ctx context.Context, repoID int64, listOptions db.ListOptions) ([]*Collaborator, error) {
  33. collaborations, err := getCollaborations(ctx, repoID, listOptions)
  34. if err != nil {
  35. return nil, fmt.Errorf("getCollaborations: %w", err)
  36. }
  37. collaborators := make([]*Collaborator, 0, len(collaborations))
  38. for _, c := range collaborations {
  39. user, err := user_model.GetUserByID(ctx, c.UserID)
  40. if err != nil {
  41. if user_model.IsErrUserNotExist(err) {
  42. log.Warn("Inconsistent DB: User: %d is listed as collaborator of %-v but does not exist", c.UserID, repoID)
  43. user = user_model.NewGhostUser()
  44. } else {
  45. return nil, err
  46. }
  47. }
  48. collaborators = append(collaborators, &Collaborator{
  49. User: user,
  50. Collaboration: c,
  51. })
  52. }
  53. return collaborators, nil
  54. }
  55. // CountCollaborators returns total number of collaborators for a repository
  56. func CountCollaborators(ctx context.Context, repoID int64) (int64, error) {
  57. return db.GetEngine(ctx).Where("repo_id = ? ", repoID).Count(&Collaboration{})
  58. }
  59. // GetCollaboration get collaboration for a repository id with a user id
  60. func GetCollaboration(ctx context.Context, repoID, uid int64) (*Collaboration, error) {
  61. collaboration := &Collaboration{
  62. RepoID: repoID,
  63. UserID: uid,
  64. }
  65. has, err := db.GetEngine(ctx).Get(collaboration)
  66. if !has {
  67. collaboration = nil
  68. }
  69. return collaboration, err
  70. }
  71. // IsCollaborator check if a user is a collaborator of a repository
  72. func IsCollaborator(ctx context.Context, repoID, userID int64) (bool, error) {
  73. return db.GetEngine(ctx).Get(&Collaboration{RepoID: repoID, UserID: userID})
  74. }
  75. func getCollaborations(ctx context.Context, repoID int64, listOptions db.ListOptions) ([]*Collaboration, error) {
  76. if listOptions.Page == 0 {
  77. collaborations := make([]*Collaboration, 0, 8)
  78. return collaborations, db.GetEngine(ctx).Find(&collaborations, &Collaboration{RepoID: repoID})
  79. }
  80. e := db.GetEngine(ctx)
  81. e = db.SetEnginePagination(e, &listOptions)
  82. collaborations := make([]*Collaboration, 0, listOptions.PageSize)
  83. return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repoID})
  84. }
  85. // ChangeCollaborationAccessMode sets new access mode for the collaboration.
  86. func ChangeCollaborationAccessMode(ctx context.Context, repo *Repository, uid int64, mode perm.AccessMode) error {
  87. // Discard invalid input
  88. if mode <= perm.AccessModeNone || mode > perm.AccessModeOwner {
  89. return nil
  90. }
  91. return db.WithTx(ctx, func(ctx context.Context) error {
  92. e := db.GetEngine(ctx)
  93. collaboration := &Collaboration{
  94. RepoID: repo.ID,
  95. UserID: uid,
  96. }
  97. has, err := e.Get(collaboration)
  98. if err != nil {
  99. return fmt.Errorf("get collaboration: %w", err)
  100. } else if !has {
  101. return nil
  102. }
  103. if collaboration.Mode == mode {
  104. return nil
  105. }
  106. collaboration.Mode = mode
  107. if _, err = e.
  108. ID(collaboration.ID).
  109. Cols("mode").
  110. Update(collaboration); err != nil {
  111. return fmt.Errorf("update collaboration: %w", err)
  112. } else if _, err = e.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, uid, repo.ID); err != nil {
  113. return fmt.Errorf("update access table: %w", err)
  114. }
  115. return nil
  116. })
  117. }
  118. // IsOwnerMemberCollaborator checks if a provided user is the owner, a collaborator or a member of a team in a repository
  119. func IsOwnerMemberCollaborator(ctx context.Context, repo *Repository, userID int64) (bool, error) {
  120. if repo.OwnerID == userID {
  121. return true, nil
  122. }
  123. teamMember, err := db.GetEngine(ctx).Join("INNER", "team_repo", "team_repo.team_id = team_user.team_id").
  124. Join("INNER", "team_unit", "team_unit.team_id = team_user.team_id").
  125. Where("team_repo.repo_id = ?", repo.ID).
  126. And("team_unit.`type` = ?", unit.TypeCode).
  127. And("team_user.uid = ?", userID).Table("team_user").Exist()
  128. if err != nil {
  129. return false, err
  130. }
  131. if teamMember {
  132. return true, nil
  133. }
  134. return db.GetEngine(ctx).Get(&Collaboration{RepoID: repo.ID, UserID: userID})
  135. }