repo_collaboration.go 5.5 KB

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