repo_collaboration.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. api "github.com/gogits/go-gogs-client"
  8. )
  9. // Collaboration represent the relation between an individual and a repository.
  10. type Collaboration struct {
  11. ID int64 `xorm:"pk autoincr"`
  12. RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
  13. UserID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
  14. Mode AccessMode `xorm:"DEFAULT 2 NOT NULL"`
  15. }
  16. func (c *Collaboration) ModeI18nKey() string {
  17. switch c.Mode {
  18. case ACCESS_MODE_READ:
  19. return "repo.settings.collaboration.read"
  20. case ACCESS_MODE_WRITE:
  21. return "repo.settings.collaboration.write"
  22. case ACCESS_MODE_ADMIN:
  23. return "repo.settings.collaboration.admin"
  24. default:
  25. return "repo.settings.collaboration.undefined"
  26. }
  27. }
  28. //IsCollaborator returns true if the user is a collaborator
  29. func (repo *Repository) IsCollaborator(uid int64) (bool, error) {
  30. collaboration := &Collaboration{
  31. RepoID: repo.ID,
  32. UserID: uid,
  33. }
  34. return x.Get(collaboration)
  35. }
  36. // AddCollaborator adds new collaboration to a repository with default access mode.
  37. func (repo *Repository) AddCollaborator(u *User) error {
  38. collaboration := &Collaboration{
  39. RepoID: repo.ID,
  40. UserID: u.ID,
  41. }
  42. has, err := x.Get(collaboration)
  43. if err != nil {
  44. return err
  45. } else if has {
  46. return nil
  47. }
  48. collaboration.Mode = ACCESS_MODE_WRITE
  49. sess := x.NewSession()
  50. defer sessionRelease(sess)
  51. if err = sess.Begin(); err != nil {
  52. return err
  53. }
  54. if _, err = sess.Insert(collaboration); err != nil {
  55. return err
  56. } else if err = repo.recalculateAccesses(sess); err != nil {
  57. return fmt.Errorf("recalculateAccesses [repo_id: %v]: %v", repo.ID, err)
  58. }
  59. return sess.Commit()
  60. }
  61. func (repo *Repository) getCollaborations(e Engine) ([]*Collaboration, error) {
  62. collaborations := make([]*Collaboration, 0)
  63. return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID})
  64. }
  65. // Collaborator represents a user with collaboration details.
  66. type Collaborator struct {
  67. *User
  68. Collaboration *Collaboration
  69. }
  70. func (c *Collaborator) APIFormat() *api.Collaborator {
  71. return &api.Collaborator{
  72. User: c.User.APIFormat(),
  73. Permissions: api.Permission{
  74. Admin: c.Collaboration.Mode >= ACCESS_MODE_ADMIN,
  75. Push: c.Collaboration.Mode >= ACCESS_MODE_WRITE,
  76. Pull: c.Collaboration.Mode >= ACCESS_MODE_READ,
  77. },
  78. }
  79. }
  80. func (repo *Repository) getCollaborators(e Engine) ([]*Collaborator, error) {
  81. collaborations, err := repo.getCollaborations(e)
  82. if err != nil {
  83. return nil, fmt.Errorf("getCollaborations: %v", err)
  84. }
  85. collaborators := make([]*Collaborator, len(collaborations))
  86. for i, c := range collaborations {
  87. user, err := getUserByID(e, c.UserID)
  88. if err != nil {
  89. return nil, err
  90. }
  91. collaborators[i] = &Collaborator{
  92. User: user,
  93. Collaboration: c,
  94. }
  95. }
  96. return collaborators, nil
  97. }
  98. // GetCollaborators returns the collaborators for a repository
  99. func (repo *Repository) GetCollaborators() ([]*Collaborator, error) {
  100. return repo.getCollaborators(x)
  101. }
  102. // ChangeCollaborationAccessMode sets new access mode for the collaboration.
  103. func (repo *Repository) ChangeCollaborationAccessMode(userID int64, mode AccessMode) error {
  104. // Discard invalid input
  105. if mode <= ACCESS_MODE_NONE || mode > ACCESS_MODE_OWNER {
  106. return nil
  107. }
  108. collaboration := &Collaboration{
  109. RepoID: repo.ID,
  110. UserID: userID,
  111. }
  112. has, err := x.Get(collaboration)
  113. if err != nil {
  114. return fmt.Errorf("get collaboration: %v", err)
  115. } else if !has {
  116. return nil
  117. }
  118. if collaboration.Mode == mode {
  119. return nil
  120. }
  121. collaboration.Mode = mode
  122. // If it's an organizational repository, merge with team access level for highest permission
  123. if repo.Owner.IsOrganization() {
  124. teams, err := GetUserTeams(repo.OwnerID, userID)
  125. if err != nil {
  126. return fmt.Errorf("GetUserTeams: [org_id: %d, user_id: %d]: %v", repo.OwnerID, userID, err)
  127. }
  128. for i := range teams {
  129. if mode < teams[i].Authorize {
  130. mode = teams[i].Authorize
  131. }
  132. }
  133. }
  134. sess := x.NewSession()
  135. defer sessionRelease(sess)
  136. if err = sess.Begin(); err != nil {
  137. return err
  138. }
  139. if _, err = sess.Id(collaboration.ID).AllCols().Update(collaboration); err != nil {
  140. return fmt.Errorf("update collaboration: %v", err)
  141. }
  142. access := Access{
  143. UserID: userID,
  144. RepoID: repo.ID,
  145. }
  146. has, err = sess.Get(access)
  147. if err != nil {
  148. return fmt.Errorf("get access record: %v", err)
  149. }
  150. if has {
  151. _, err = sess.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, userID, repo.ID)
  152. } else {
  153. access.Mode = mode
  154. _, err = sess.Insert(access)
  155. }
  156. if err != nil {
  157. return fmt.Errorf("update/insert access table: %v", err)
  158. }
  159. return sess.Commit()
  160. }
  161. // DeleteCollaboration removes collaboration relation between the user and repository.
  162. func (repo *Repository) DeleteCollaboration(uid int64) (err error) {
  163. collaboration := &Collaboration{
  164. RepoID: repo.ID,
  165. UserID: uid,
  166. }
  167. sess := x.NewSession()
  168. defer sessionRelease(sess)
  169. if err = sess.Begin(); err != nil {
  170. return err
  171. }
  172. if has, err := sess.Delete(collaboration); err != nil || has == 0 {
  173. return err
  174. } else if err = repo.recalculateAccesses(sess); err != nil {
  175. return err
  176. }
  177. return sess.Commit()
  178. }