Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

repo_collaboration.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2016 The Gogs 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 models
  5. import (
  6. "fmt"
  7. )
  8. // Collaboration represent the relation between an individual and a repository.
  9. type Collaboration struct {
  10. ID int64 `xorm:"pk autoincr"`
  11. RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
  12. UserID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
  13. Mode AccessMode `xorm:"DEFAULT 2 NOT NULL"`
  14. }
  15. // ModeI18nKey returns the collaboration mode I18n Key
  16. func (c *Collaboration) ModeI18nKey() string {
  17. switch c.Mode {
  18. case AccessModeRead:
  19. return "repo.settings.collaboration.read"
  20. case AccessModeWrite:
  21. return "repo.settings.collaboration.write"
  22. case AccessModeAdmin:
  23. return "repo.settings.collaboration.admin"
  24. default:
  25. return "repo.settings.collaboration.undefined"
  26. }
  27. }
  28. // AddCollaborator adds new collaboration to a repository with default access mode.
  29. func (repo *Repository) AddCollaborator(u *User) error {
  30. collaboration := &Collaboration{
  31. RepoID: repo.ID,
  32. UserID: u.ID,
  33. }
  34. has, err := x.Get(collaboration)
  35. if err != nil {
  36. return err
  37. } else if has {
  38. return nil
  39. }
  40. collaboration.Mode = AccessModeWrite
  41. sess := x.NewSession()
  42. defer sess.Close()
  43. if err = sess.Begin(); err != nil {
  44. return err
  45. }
  46. if _, err = sess.InsertOne(collaboration); err != nil {
  47. return err
  48. }
  49. if repo.Owner.IsOrganization() {
  50. err = repo.recalculateTeamAccesses(sess, 0)
  51. } else {
  52. err = repo.recalculateAccesses(sess)
  53. }
  54. if err != nil {
  55. return fmt.Errorf("recalculateAccesses 'team=%v': %v", repo.Owner.IsOrganization(), err)
  56. }
  57. return sess.Commit()
  58. }
  59. func (repo *Repository) getCollaborations(e Engine) ([]*Collaboration, error) {
  60. var collaborations []*Collaboration
  61. return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID})
  62. }
  63. // Collaborator represents a user with collaboration details.
  64. type Collaborator struct {
  65. *User
  66. Collaboration *Collaboration
  67. }
  68. func (repo *Repository) getCollaborators(e Engine) ([]*Collaborator, error) {
  69. collaborations, err := repo.getCollaborations(e)
  70. if err != nil {
  71. return nil, fmt.Errorf("getCollaborations: %v", err)
  72. }
  73. collaborators := make([]*Collaborator, len(collaborations))
  74. for i, c := range collaborations {
  75. user, err := getUserByID(e, c.UserID)
  76. if err != nil {
  77. return nil, err
  78. }
  79. collaborators[i] = &Collaborator{
  80. User: user,
  81. Collaboration: c,
  82. }
  83. }
  84. return collaborators, nil
  85. }
  86. // GetCollaborators returns the collaborators for a repository
  87. func (repo *Repository) GetCollaborators() ([]*Collaborator, error) {
  88. return repo.getCollaborators(x)
  89. }
  90. func (repo *Repository) isCollaborator(e Engine, userID int64) (bool, error) {
  91. return e.Get(&Collaboration{RepoID: repo.ID, UserID: userID})
  92. }
  93. // IsCollaborator check if a user is a collaborator of a repository
  94. func (repo *Repository) IsCollaborator(userID int64) (bool, error) {
  95. return repo.isCollaborator(x, userID)
  96. }
  97. // ChangeCollaborationAccessMode sets new access mode for the collaboration.
  98. func (repo *Repository) ChangeCollaborationAccessMode(uid int64, mode AccessMode) error {
  99. // Discard invalid input
  100. if mode <= AccessModeNone || mode > AccessModeOwner {
  101. return nil
  102. }
  103. collaboration := &Collaboration{
  104. RepoID: repo.ID,
  105. UserID: uid,
  106. }
  107. has, err := x.Get(collaboration)
  108. if err != nil {
  109. return fmt.Errorf("get collaboration: %v", err)
  110. } else if !has {
  111. return nil
  112. }
  113. if collaboration.Mode == mode {
  114. return nil
  115. }
  116. collaboration.Mode = mode
  117. sess := x.NewSession()
  118. defer sess.Close()
  119. if err = sess.Begin(); err != nil {
  120. return err
  121. }
  122. if _, err = sess.
  123. Id(collaboration.ID).
  124. Cols("mode").
  125. Update(collaboration); err != nil {
  126. return fmt.Errorf("update collaboration: %v", err)
  127. } else if _, err = sess.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, uid, repo.ID); err != nil {
  128. return fmt.Errorf("update access table: %v", err)
  129. }
  130. return sess.Commit()
  131. }
  132. // DeleteCollaboration removes collaboration relation between the user and repository.
  133. func (repo *Repository) DeleteCollaboration(uid int64) (err error) {
  134. collaboration := &Collaboration{
  135. RepoID: repo.ID,
  136. UserID: uid,
  137. }
  138. sess := x.NewSession()
  139. defer sess.Close()
  140. if err = sess.Begin(); err != nil {
  141. return err
  142. }
  143. if has, err := sess.Delete(collaboration); err != nil || has == 0 {
  144. return err
  145. } else if err = repo.recalculateAccesses(sess); err != nil {
  146. return err
  147. }
  148. return sess.Commit()
  149. }