repo_collaboration.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. "time"
  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. Created time.Time `xorm:"CREATED"`
  16. }
  17. func (c *Collaboration) ModeName() string {
  18. switch c.Mode {
  19. case ACCESS_MODE_READ:
  20. return "Read"
  21. case ACCESS_MODE_WRITE:
  22. return "Write"
  23. case ACCESS_MODE_ADMIN:
  24. return "Admin"
  25. }
  26. return "Undefined"
  27. }
  28. // AddCollaborator adds new collaboration relation between an individual and a repository.
  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 = ACCESS_MODE_WRITE
  41. sess := x.NewSession()
  42. defer sessionRelease(sess)
  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. collaborations := make([]*Collaboration, 0)
  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. // ChangeCollaborationAccessMode sets new access mode for the collaboration.
  91. func (repo *Repository) ChangeCollaborationAccessMode(uid int64, mode AccessMode) error {
  92. // Discard invalid input
  93. if mode <= ACCESS_MODE_NONE || mode > ACCESS_MODE_OWNER {
  94. return nil
  95. }
  96. collaboration := &Collaboration{
  97. RepoID: repo.ID,
  98. UserID: uid,
  99. }
  100. has, err := x.Get(collaboration)
  101. if err != nil {
  102. return fmt.Errorf("get collaboration: %v", err)
  103. } else if !has {
  104. return nil
  105. }
  106. collaboration.Mode = mode
  107. sess := x.NewSession()
  108. defer sessionRelease(sess)
  109. if err = sess.Begin(); err != nil {
  110. return err
  111. }
  112. if _, err = sess.Id(collaboration.ID).AllCols().Update(collaboration); err != nil {
  113. return fmt.Errorf("update collaboration: %v", err)
  114. } else if _, err = sess.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, uid, repo.ID); err != nil {
  115. return fmt.Errorf("update access table: %v", err)
  116. }
  117. return sess.Commit()
  118. }
  119. // DeleteCollaboration removes collaboration relation between the user and repository.
  120. func (repo *Repository) DeleteCollaboration(uid int64) (err error) {
  121. collaboration := &Collaboration{
  122. RepoID: repo.ID,
  123. UserID: uid,
  124. }
  125. sess := x.NewSession()
  126. defer sessionRelease(sess)
  127. if err = sess.Begin(); err != nil {
  128. return err
  129. }
  130. if has, err := sess.Delete(collaboration); err != nil || has == 0 {
  131. return err
  132. } else if err = repo.recalculateAccesses(sess); err != nil {
  133. return err
  134. }
  135. return sess.Commit()
  136. }