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.

269 lines
7.2 KiB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2020 The Gitea Authors.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "fmt"
  8. "xorm.io/builder"
  9. )
  10. // Collaboration represent the relation between an individual and a repository.
  11. type Collaboration struct {
  12. ID int64 `xorm:"pk autoincr"`
  13. RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
  14. UserID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
  15. Mode AccessMode `xorm:"DEFAULT 2 NOT NULL"`
  16. }
  17. func (repo *Repository) addCollaborator(e Engine, u *User) error {
  18. collaboration := &Collaboration{
  19. RepoID: repo.ID,
  20. UserID: u.ID,
  21. }
  22. has, err := e.Get(collaboration)
  23. if err != nil {
  24. return err
  25. } else if has {
  26. return nil
  27. }
  28. collaboration.Mode = AccessModeWrite
  29. if _, err = e.InsertOne(collaboration); err != nil {
  30. return err
  31. }
  32. return repo.recalculateUserAccess(e, u.ID)
  33. }
  34. // AddCollaborator adds new collaboration to a repository with default access mode.
  35. func (repo *Repository) AddCollaborator(u *User) error {
  36. sess := x.NewSession()
  37. defer sess.Close()
  38. if err := sess.Begin(); err != nil {
  39. return err
  40. }
  41. if err := repo.addCollaborator(sess, u); err != nil {
  42. return err
  43. }
  44. return sess.Commit()
  45. }
  46. func (repo *Repository) getCollaborations(e Engine, listOptions ListOptions) ([]*Collaboration, error) {
  47. if listOptions.Page == 0 {
  48. collaborations := make([]*Collaboration, 0, 8)
  49. return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID})
  50. }
  51. e = listOptions.setEnginePagination(e)
  52. collaborations := make([]*Collaboration, 0, listOptions.PageSize)
  53. return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID})
  54. }
  55. // Collaborator represents a user with collaboration details.
  56. type Collaborator struct {
  57. *User
  58. Collaboration *Collaboration
  59. }
  60. func (repo *Repository) getCollaborators(e Engine, listOptions ListOptions) ([]*Collaborator, error) {
  61. collaborations, err := repo.getCollaborations(e, listOptions)
  62. if err != nil {
  63. return nil, fmt.Errorf("getCollaborations: %v", err)
  64. }
  65. collaborators := make([]*Collaborator, len(collaborations))
  66. for i, c := range collaborations {
  67. user, err := getUserByID(e, c.UserID)
  68. if err != nil {
  69. return nil, err
  70. }
  71. collaborators[i] = &Collaborator{
  72. User: user,
  73. Collaboration: c,
  74. }
  75. }
  76. return collaborators, nil
  77. }
  78. // GetCollaborators returns the collaborators for a repository
  79. func (repo *Repository) GetCollaborators(listOptions ListOptions) ([]*Collaborator, error) {
  80. return repo.getCollaborators(x, listOptions)
  81. }
  82. func (repo *Repository) getCollaboration(e Engine, uid int64) (*Collaboration, error) {
  83. collaboration := &Collaboration{
  84. RepoID: repo.ID,
  85. UserID: uid,
  86. }
  87. has, err := e.Get(collaboration)
  88. if !has {
  89. collaboration = nil
  90. }
  91. return collaboration, err
  92. }
  93. func (repo *Repository) isCollaborator(e Engine, userID int64) (bool, error) {
  94. return e.Get(&Collaboration{RepoID: repo.ID, UserID: userID})
  95. }
  96. // IsCollaborator check if a user is a collaborator of a repository
  97. func (repo *Repository) IsCollaborator(userID int64) (bool, error) {
  98. return repo.isCollaborator(x, userID)
  99. }
  100. func (repo *Repository) changeCollaborationAccessMode(e Engine, uid int64, mode AccessMode) error {
  101. // Discard invalid input
  102. if mode <= AccessModeNone || mode > AccessModeOwner {
  103. return nil
  104. }
  105. collaboration := &Collaboration{
  106. RepoID: repo.ID,
  107. UserID: uid,
  108. }
  109. has, err := e.Get(collaboration)
  110. if err != nil {
  111. return fmt.Errorf("get collaboration: %v", err)
  112. } else if !has {
  113. return nil
  114. }
  115. if collaboration.Mode == mode {
  116. return nil
  117. }
  118. collaboration.Mode = mode
  119. if _, err = e.
  120. ID(collaboration.ID).
  121. Cols("mode").
  122. Update(collaboration); err != nil {
  123. return fmt.Errorf("update collaboration: %v", err)
  124. } else if _, err = e.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, uid, repo.ID); err != nil {
  125. return fmt.Errorf("update access table: %v", err)
  126. }
  127. return nil
  128. }
  129. // ChangeCollaborationAccessMode sets new access mode for the collaboration.
  130. func (repo *Repository) ChangeCollaborationAccessMode(uid int64, mode AccessMode) error {
  131. sess := x.NewSession()
  132. defer sess.Close()
  133. if err := sess.Begin(); err != nil {
  134. return err
  135. }
  136. if err := repo.changeCollaborationAccessMode(sess, uid, mode); err != nil {
  137. return err
  138. }
  139. return sess.Commit()
  140. }
  141. // DeleteCollaboration removes collaboration relation between the user and repository.
  142. func (repo *Repository) DeleteCollaboration(uid int64) (err error) {
  143. collaboration := &Collaboration{
  144. RepoID: repo.ID,
  145. UserID: uid,
  146. }
  147. sess := x.NewSession()
  148. defer sess.Close()
  149. if err = sess.Begin(); err != nil {
  150. return err
  151. }
  152. if has, err := sess.Delete(collaboration); err != nil || has == 0 {
  153. return err
  154. } else if err = repo.recalculateAccesses(sess); err != nil {
  155. return err
  156. }
  157. if err = watchRepo(sess, uid, repo.ID, false); err != nil {
  158. return err
  159. }
  160. if err = repo.reconsiderWatches(sess, uid); err != nil {
  161. return err
  162. }
  163. // Unassign a user from any issue (s)he has been assigned to in the repository
  164. if err := repo.reconsiderIssueAssignees(sess, uid); err != nil {
  165. return err
  166. }
  167. return sess.Commit()
  168. }
  169. func (repo *Repository) reconsiderIssueAssignees(e Engine, uid int64) error {
  170. user, err := getUserByID(e, uid)
  171. if err != nil {
  172. return err
  173. }
  174. if canAssigned, err := canBeAssigned(e, user, repo, true); err != nil || canAssigned {
  175. return err
  176. }
  177. if _, err := e.Where(builder.Eq{"assignee_id": uid}).
  178. In("issue_id", builder.Select("id").From("issue").Where(builder.Eq{"repo_id": repo.ID})).
  179. Delete(&IssueAssignees{}); err != nil {
  180. return fmt.Errorf("Could not delete assignee[%d] %v", uid, err)
  181. }
  182. return nil
  183. }
  184. func (repo *Repository) reconsiderWatches(e Engine, uid int64) error {
  185. if has, err := hasAccess(e, uid, repo); err != nil || has {
  186. return err
  187. }
  188. if err := watchRepo(e, uid, repo.ID, false); err != nil {
  189. return err
  190. }
  191. // Remove all IssueWatches a user has subscribed to in the repository
  192. return removeIssueWatchersByRepoID(e, uid, repo.ID)
  193. }
  194. func (repo *Repository) getRepoTeams(e Engine) (teams []*Team, err error) {
  195. return teams, e.
  196. Join("INNER", "team_repo", "team_repo.team_id = team.id").
  197. Where("team.org_id = ?", repo.OwnerID).
  198. And("team_repo.repo_id=?", repo.ID).
  199. OrderBy("CASE WHEN name LIKE '" + ownerTeamName + "' THEN '' ELSE name END").
  200. Find(&teams)
  201. }
  202. // GetRepoTeams gets the list of teams that has access to the repository
  203. func (repo *Repository) GetRepoTeams() ([]*Team, error) {
  204. return repo.getRepoTeams(x)
  205. }
  206. // IsOwnerMemberCollaborator checks if a provided user is the owner, a collaborator or a member of a team in a repository
  207. func (repo *Repository) IsOwnerMemberCollaborator(userID int64) (bool, error) {
  208. if repo.OwnerID == userID {
  209. return true, nil
  210. }
  211. teamMember, err := x.Join("INNER", "team_repo", "team_repo.team_id = team_user.team_id").
  212. Join("INNER", "team_unit", "team_unit.team_id = team_user.team_id").
  213. Where("team_repo.repo_id = ?", repo.ID).
  214. And("team_unit.`type` = ?", UnitTypeCode).
  215. And("team_user.uid = ?", userID).Table("team_user").Exist(&TeamUser{})
  216. if err != nil {
  217. return false, err
  218. }
  219. if teamMember {
  220. return true, nil
  221. }
  222. return x.Get(&Collaboration{RepoID: repo.ID, UserID: userID})
  223. }