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