repo_collaboration.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. func (c *Collaboration) ModeName() string {
  16. switch c.Mode {
  17. case ACCESS_MODE_READ:
  18. return "Read"
  19. case ACCESS_MODE_WRITE:
  20. return "Write"
  21. case ACCESS_MODE_ADMIN:
  22. return "Admin"
  23. }
  24. return "Undefined"
  25. }
  26. // AddCollaborator adds new collaboration relation between an individual and a repository.
  27. func (repo *Repository) AddCollaborator(u *User) error {
  28. collaboration := &Collaboration{
  29. RepoID: repo.ID,
  30. UserID: u.Id,
  31. }
  32. has, err := x.Get(collaboration)
  33. if err != nil {
  34. return err
  35. } else if has {
  36. return nil
  37. }
  38. collaboration.Mode = ACCESS_MODE_WRITE
  39. sess := x.NewSession()
  40. defer sessionRelease(sess)
  41. if err = sess.Begin(); err != nil {
  42. return err
  43. }
  44. if _, err = sess.InsertOne(collaboration); err != nil {
  45. return err
  46. }
  47. if repo.Owner.IsOrganization() {
  48. err = repo.recalculateTeamAccesses(sess, 0)
  49. } else {
  50. err = repo.recalculateAccesses(sess)
  51. }
  52. if err != nil {
  53. return fmt.Errorf("recalculateAccesses 'team=%v': %v", repo.Owner.IsOrganization(), err)
  54. }
  55. return sess.Commit()
  56. }
  57. func (repo *Repository) getCollaborations(e Engine) ([]*Collaboration, error) {
  58. collaborations := make([]*Collaboration, 0)
  59. return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID})
  60. }
  61. // Collaborator represents a user with collaboration details.
  62. type Collaborator struct {
  63. *User
  64. Collaboration *Collaboration
  65. }
  66. func (repo *Repository) getCollaborators(e Engine) ([]*Collaborator, error) {
  67. collaborations, err := repo.getCollaborations(e)
  68. if err != nil {
  69. return nil, fmt.Errorf("getCollaborations: %v", err)
  70. }
  71. collaborators := make([]*Collaborator, len(collaborations))
  72. for i, c := range collaborations {
  73. user, err := getUserByID(e, c.UserID)
  74. if err != nil {
  75. return nil, err
  76. }
  77. collaborators[i] = &Collaborator{
  78. User: user,
  79. Collaboration: c,
  80. }
  81. }
  82. return collaborators, nil
  83. }
  84. // GetCollaborators returns the collaborators for a repository
  85. func (repo *Repository) GetCollaborators() ([]*Collaborator, error) {
  86. return repo.getCollaborators(x)
  87. }
  88. // ChangeCollaborationAccessMode sets new access mode for the collaboration.
  89. func (repo *Repository) ChangeCollaborationAccessMode(uid int64, mode AccessMode) error {
  90. // Discard invalid input
  91. if mode <= ACCESS_MODE_NONE || mode > ACCESS_MODE_OWNER {
  92. return nil
  93. }
  94. collaboration := &Collaboration{
  95. RepoID: repo.ID,
  96. UserID: uid,
  97. }
  98. has, err := x.Get(collaboration)
  99. if err != nil {
  100. return fmt.Errorf("get collaboration: %v", err)
  101. } else if !has {
  102. return nil
  103. }
  104. collaboration.Mode = mode
  105. sess := x.NewSession()
  106. defer sessionRelease(sess)
  107. if err = sess.Begin(); err != nil {
  108. return err
  109. }
  110. if _, err = sess.Id(collaboration.ID).AllCols().Update(collaboration); err != nil {
  111. return fmt.Errorf("update collaboration: %v", err)
  112. } else if _, err = sess.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, uid, repo.ID); err != nil {
  113. return fmt.Errorf("update access table: %v", err)
  114. }
  115. return sess.Commit()
  116. }
  117. // DeleteCollaboration removes collaboration relation between the user and repository.
  118. func (repo *Repository) DeleteCollaboration(uid int64) (err error) {
  119. collaboration := &Collaboration{
  120. RepoID: repo.ID,
  121. UserID: uid,
  122. }
  123. sess := x.NewSession()
  124. defer sessionRelease(sess)
  125. if err = sess.Begin(); err != nil {
  126. return err
  127. }
  128. if has, err := sess.Delete(collaboration); err != nil || has == 0 {
  129. return err
  130. } else if err = repo.recalculateAccesses(sess); err != nil {
  131. return err
  132. }
  133. return sess.Commit()
  134. }