repo_collaboration.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.InsertOne(collaboration); err != nil {
  55. return err
  56. }
  57. if repo.Owner.IsOrganization() {
  58. err = repo.recalculateTeamAccesses(sess, 0)
  59. } else {
  60. err = repo.recalculateAccesses(sess)
  61. }
  62. if err != nil {
  63. return fmt.Errorf("recalculateAccesses 'team=%v': %v", repo.Owner.IsOrganization(), err)
  64. }
  65. return sess.Commit()
  66. }
  67. func (repo *Repository) getCollaborations(e Engine) ([]*Collaboration, error) {
  68. collaborations := make([]*Collaboration, 0)
  69. return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID})
  70. }
  71. // Collaborator represents a user with collaboration details.
  72. type Collaborator struct {
  73. *User
  74. Collaboration *Collaboration
  75. }
  76. func (c *Collaborator) APIFormat() *api.Collaborator {
  77. return &api.Collaborator{
  78. User: c.User.APIFormat(),
  79. Permissions: api.Permission{
  80. Admin: c.Collaboration.Mode >= ACCESS_MODE_ADMIN,
  81. Push: c.Collaboration.Mode >= ACCESS_MODE_WRITE,
  82. Pull: c.Collaboration.Mode >= ACCESS_MODE_READ,
  83. },
  84. }
  85. }
  86. func (repo *Repository) getCollaborators(e Engine) ([]*Collaborator, error) {
  87. collaborations, err := repo.getCollaborations(e)
  88. if err != nil {
  89. return nil, fmt.Errorf("getCollaborations: %v", err)
  90. }
  91. collaborators := make([]*Collaborator, len(collaborations))
  92. for i, c := range collaborations {
  93. user, err := getUserByID(e, c.UserID)
  94. if err != nil {
  95. return nil, err
  96. }
  97. collaborators[i] = &Collaborator{
  98. User: user,
  99. Collaboration: c,
  100. }
  101. }
  102. return collaborators, nil
  103. }
  104. // GetCollaborators returns the collaborators for a repository
  105. func (repo *Repository) GetCollaborators() ([]*Collaborator, error) {
  106. return repo.getCollaborators(x)
  107. }
  108. // ChangeCollaborationAccessMode sets new access mode for the collaboration.
  109. func (repo *Repository) ChangeCollaborationAccessMode(userID int64, mode AccessMode) error {
  110. // Discard invalid input
  111. if mode <= ACCESS_MODE_NONE || mode > ACCESS_MODE_OWNER {
  112. return nil
  113. }
  114. collaboration := &Collaboration{
  115. RepoID: repo.ID,
  116. UserID: userID,
  117. }
  118. has, err := x.Get(collaboration)
  119. if err != nil {
  120. return fmt.Errorf("get collaboration: %v", err)
  121. } else if !has {
  122. return nil
  123. }
  124. if collaboration.Mode == mode {
  125. return nil
  126. }
  127. collaboration.Mode = mode
  128. sess := x.NewSession()
  129. defer sessionRelease(sess)
  130. if err = sess.Begin(); err != nil {
  131. return err
  132. }
  133. if _, err = sess.Id(collaboration.ID).AllCols().Update(collaboration); err != nil {
  134. return fmt.Errorf("update collaboration: %v", err)
  135. }
  136. access := Access{
  137. UserID: userID,
  138. RepoID: repo.ID,
  139. }
  140. has, err = sess.Get(access)
  141. if err != nil {
  142. return fmt.Errorf("get access record: %v", err)
  143. }
  144. if has {
  145. _, err = sess.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, userID, repo.ID)
  146. } else {
  147. access.Mode = mode
  148. _, err = sess.Insert(access)
  149. }
  150. if err != nil {
  151. return fmt.Errorf("update access table: %v", err)
  152. }
  153. return sess.Commit()
  154. }
  155. // DeleteCollaboration removes collaboration relation between the user and repository.
  156. func (repo *Repository) DeleteCollaboration(uid int64) (err error) {
  157. collaboration := &Collaboration{
  158. RepoID: repo.ID,
  159. UserID: uid,
  160. }
  161. sess := x.NewSession()
  162. defer sessionRelease(sess)
  163. if err = sess.Begin(); err != nil {
  164. return err
  165. }
  166. if has, err := sess.Delete(collaboration); err != nil || has == 0 {
  167. return err
  168. } else if err = repo.recalculateAccesses(sess); err != nil {
  169. return err
  170. }
  171. return sess.Commit()
  172. }