本站源代码
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.

193 lines
4.9KB

  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. // AddCollaborator adds new collaboration to a repository with default access mode.
  16. func (repo *Repository) AddCollaborator(u *User) error {
  17. collaboration := &Collaboration{
  18. RepoID: repo.ID,
  19. UserID: u.ID,
  20. }
  21. has, err := x.Get(collaboration)
  22. if err != nil {
  23. return err
  24. } else if has {
  25. return nil
  26. }
  27. collaboration.Mode = AccessModeWrite
  28. sess := x.NewSession()
  29. defer sess.Close()
  30. if err = sess.Begin(); err != nil {
  31. return err
  32. }
  33. if _, err = sess.InsertOne(collaboration); err != nil {
  34. return err
  35. }
  36. if err = repo.recalculateUserAccess(sess, u.ID); err != nil {
  37. return fmt.Errorf("recalculateAccesses 'team=%v': %v", repo.Owner.IsOrganization(), err)
  38. }
  39. return sess.Commit()
  40. }
  41. func (repo *Repository) getCollaborations(e Engine) ([]*Collaboration, error) {
  42. var collaborations []*Collaboration
  43. return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID})
  44. }
  45. // Collaborator represents a user with collaboration details.
  46. type Collaborator struct {
  47. *User
  48. Collaboration *Collaboration
  49. }
  50. func (repo *Repository) getCollaborators(e Engine) ([]*Collaborator, error) {
  51. collaborations, err := repo.getCollaborations(e)
  52. if err != nil {
  53. return nil, fmt.Errorf("getCollaborations: %v", err)
  54. }
  55. collaborators := make([]*Collaborator, len(collaborations))
  56. for i, c := range collaborations {
  57. user, err := getUserByID(e, c.UserID)
  58. if err != nil {
  59. return nil, err
  60. }
  61. collaborators[i] = &Collaborator{
  62. User: user,
  63. Collaboration: c,
  64. }
  65. }
  66. return collaborators, nil
  67. }
  68. // GetCollaborators returns the collaborators for a repository
  69. func (repo *Repository) GetCollaborators() ([]*Collaborator, error) {
  70. return repo.getCollaborators(x)
  71. }
  72. func (repo *Repository) getCollaboration(e Engine, uid int64) (*Collaboration, error) {
  73. collaboration := &Collaboration{
  74. RepoID: repo.ID,
  75. UserID: uid,
  76. }
  77. has, err := e.Get(collaboration)
  78. if !has {
  79. collaboration = nil
  80. }
  81. return collaboration, err
  82. }
  83. func (repo *Repository) isCollaborator(e Engine, userID int64) (bool, error) {
  84. return e.Get(&Collaboration{RepoID: repo.ID, UserID: userID})
  85. }
  86. // IsCollaborator check if a user is a collaborator of a repository
  87. func (repo *Repository) IsCollaborator(userID int64) (bool, error) {
  88. return repo.isCollaborator(x, userID)
  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 <= AccessModeNone || mode > AccessModeOwner {
  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. if collaboration.Mode == mode {
  107. return nil
  108. }
  109. collaboration.Mode = mode
  110. sess := x.NewSession()
  111. defer sess.Close()
  112. if err = sess.Begin(); err != nil {
  113. return err
  114. }
  115. if _, err = sess.
  116. ID(collaboration.ID).
  117. Cols("mode").
  118. Update(collaboration); err != nil {
  119. return fmt.Errorf("update collaboration: %v", err)
  120. } else if _, err = sess.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, uid, repo.ID); err != nil {
  121. return fmt.Errorf("update access table: %v", err)
  122. }
  123. return sess.Commit()
  124. }
  125. // DeleteCollaboration removes collaboration relation between the user and repository.
  126. func (repo *Repository) DeleteCollaboration(uid int64) (err error) {
  127. collaboration := &Collaboration{
  128. RepoID: repo.ID,
  129. UserID: uid,
  130. }
  131. sess := x.NewSession()
  132. defer sess.Close()
  133. if err = sess.Begin(); err != nil {
  134. return err
  135. }
  136. if has, err := sess.Delete(collaboration); err != nil || has == 0 {
  137. return err
  138. } else if err = repo.recalculateAccesses(sess); err != nil {
  139. return err
  140. }
  141. if err = watchRepo(sess, uid, repo.ID, false); err != nil {
  142. return err
  143. }
  144. // Remove all IssueWatches a user has subscribed to in the repository
  145. if err := removeIssueWatchersByRepoID(sess, uid, repo.ID); err != nil {
  146. return err
  147. }
  148. return sess.Commit()
  149. }
  150. func (repo *Repository) getRepoTeams(e Engine) (teams []*Team, err error) {
  151. return teams, e.
  152. Join("INNER", "team_repo", "team_repo.team_id = team.id").
  153. Where("team.org_id = ?", repo.OwnerID).
  154. And("team_repo.repo_id=?", repo.ID).
  155. OrderBy("CASE WHEN name LIKE '" + ownerTeamName + "' THEN '' ELSE name END").
  156. Find(&teams)
  157. }
  158. // GetRepoTeams gets the list of teams that has access to the repository
  159. func (repo *Repository) GetRepoTeams() ([]*Team, error) {
  160. return repo.getRepoTeams(x)
  161. }
上海开阖软件有限公司 沪ICP备12045867号-1