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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "context"
  7. "fmt"
  8. "code.gitea.io/gitea/models/db"
  9. "code.gitea.io/gitea/models/perm"
  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: %v", err)
  36. }
  37. collaborators := make([]*Collaborator, 0, len(collaborations))
  38. for _, c := range collaborations {
  39. user, err := user_model.GetUserByIDCtx(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(repoID int64) (int64, error) {
  57. return db.GetEngine(db.DefaultContext).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 ChangeCollaborationAccessModeCtx(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. e := db.GetEngine(ctx)
  92. collaboration := &Collaboration{
  93. RepoID: repo.ID,
  94. UserID: uid,
  95. }
  96. has, err := e.Get(collaboration)
  97. if err != nil {
  98. return fmt.Errorf("get collaboration: %v", err)
  99. } else if !has {
  100. return nil
  101. }
  102. if collaboration.Mode == mode {
  103. return nil
  104. }
  105. collaboration.Mode = mode
  106. if _, err = e.
  107. ID(collaboration.ID).
  108. Cols("mode").
  109. Update(collaboration); err != nil {
  110. return fmt.Errorf("update collaboration: %v", err)
  111. } else if _, err = e.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, uid, repo.ID); err != nil {
  112. return fmt.Errorf("update access table: %v", err)
  113. }
  114. return nil
  115. }
  116. // ChangeCollaborationAccessMode sets new access mode for the collaboration.
  117. func ChangeCollaborationAccessMode(repo *Repository, uid int64, mode perm.AccessMode) error {
  118. ctx, committer, err := db.TxContext()
  119. if err != nil {
  120. return err
  121. }
  122. defer committer.Close()
  123. if err := ChangeCollaborationAccessModeCtx(ctx, repo, uid, mode); err != nil {
  124. return err
  125. }
  126. return committer.Commit()
  127. }