You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.1 KiB

  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 AccessModeRead:
  18. return "repo.settings.collaboration.read"
  19. case AccessModeWrite:
  20. return "repo.settings.collaboration.write"
  21. case AccessModeAdmin:
  22. return "repo.settings.collaboration.admin"
  23. default:
  24. return "repo.settings.collaboration.undefined"
  25. }
  26. }
  27. // AddCollaborator adds new collaboration to a repository with default access mode.
  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 = AccessModeWrite
  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 <= AccessModeNone || mode > AccessModeOwner {
  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. if collaboration.Mode == mode {
  106. return nil
  107. }
  108. collaboration.Mode = mode
  109. sess := x.NewSession()
  110. defer sessionRelease(sess)
  111. if err = sess.Begin(); err != nil {
  112. return err
  113. }
  114. if _, err = sess.
  115. Id(collaboration.ID).
  116. AllCols().
  117. Update(collaboration); err != nil {
  118. return fmt.Errorf("update collaboration: %v", err)
  119. } else if _, err = sess.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, uid, repo.ID); err != nil {
  120. return fmt.Errorf("update access table: %v", err)
  121. }
  122. return sess.Commit()
  123. }
  124. // DeleteCollaboration removes collaboration relation between the user and repository.
  125. func (repo *Repository) DeleteCollaboration(uid int64) (err error) {
  126. collaboration := &Collaboration{
  127. RepoID: repo.ID,
  128. UserID: uid,
  129. }
  130. sess := x.NewSession()
  131. defer sessionRelease(sess)
  132. if err = sess.Begin(); err != nil {
  133. return err
  134. }
  135. if has, err := sess.Delete(collaboration); err != nil || has == 0 {
  136. return err
  137. } else if err = repo.recalculateAccesses(sess); err != nil {
  138. return err
  139. }
  140. return sess.Commit()
  141. }