A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
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.

273 lines
7.4 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>
3 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>
3 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>
3 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>
3 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. "code.gitea.io/gitea/modules/timeutil"
  9. "xorm.io/builder"
  10. )
  11. // Collaboration represent the relation between an individual and a repository.
  12. type Collaboration struct {
  13. ID int64 `xorm:"pk autoincr"`
  14. RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
  15. UserID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
  16. Mode AccessMode `xorm:"DEFAULT 2 NOT NULL"`
  17. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  18. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  19. }
  20. func (repo *Repository) addCollaborator(e Engine, u *User) error {
  21. collaboration := &Collaboration{
  22. RepoID: repo.ID,
  23. UserID: u.ID,
  24. }
  25. has, err := e.Get(collaboration)
  26. if err != nil {
  27. return err
  28. } else if has {
  29. return nil
  30. }
  31. collaboration.Mode = AccessModeWrite
  32. if _, err = e.InsertOne(collaboration); err != nil {
  33. return err
  34. }
  35. return repo.recalculateUserAccess(e, u.ID)
  36. }
  37. // AddCollaborator adds new collaboration to a repository with default access mode.
  38. func (repo *Repository) AddCollaborator(u *User) error {
  39. sess := x.NewSession()
  40. defer sess.Close()
  41. if err := sess.Begin(); err != nil {
  42. return err
  43. }
  44. if err := repo.addCollaborator(sess, u); err != nil {
  45. return err
  46. }
  47. return sess.Commit()
  48. }
  49. func (repo *Repository) getCollaborations(e Engine, listOptions ListOptions) ([]*Collaboration, error) {
  50. if listOptions.Page == 0 {
  51. collaborations := make([]*Collaboration, 0, 8)
  52. return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID})
  53. }
  54. e = listOptions.setEnginePagination(e)
  55. collaborations := make([]*Collaboration, 0, listOptions.PageSize)
  56. return collaborations, e.Find(&collaborations, &Collaboration{RepoID: repo.ID})
  57. }
  58. // Collaborator represents a user with collaboration details.
  59. type Collaborator struct {
  60. *User
  61. Collaboration *Collaboration
  62. }
  63. func (repo *Repository) getCollaborators(e Engine, listOptions ListOptions) ([]*Collaborator, error) {
  64. collaborations, err := repo.getCollaborations(e, listOptions)
  65. if err != nil {
  66. return nil, fmt.Errorf("getCollaborations: %v", err)
  67. }
  68. collaborators := make([]*Collaborator, len(collaborations))
  69. for i, c := range collaborations {
  70. user, err := getUserByID(e, c.UserID)
  71. if err != nil {
  72. return nil, err
  73. }
  74. collaborators[i] = &Collaborator{
  75. User: user,
  76. Collaboration: c,
  77. }
  78. }
  79. return collaborators, nil
  80. }
  81. // GetCollaborators returns the collaborators for a repository
  82. func (repo *Repository) GetCollaborators(listOptions ListOptions) ([]*Collaborator, error) {
  83. return repo.getCollaborators(x, listOptions)
  84. }
  85. func (repo *Repository) getCollaboration(e Engine, uid int64) (*Collaboration, error) {
  86. collaboration := &Collaboration{
  87. RepoID: repo.ID,
  88. UserID: uid,
  89. }
  90. has, err := e.Get(collaboration)
  91. if !has {
  92. collaboration = nil
  93. }
  94. return collaboration, err
  95. }
  96. func (repo *Repository) isCollaborator(e Engine, userID int64) (bool, error) {
  97. return e.Get(&Collaboration{RepoID: repo.ID, UserID: userID})
  98. }
  99. // IsCollaborator check if a user is a collaborator of a repository
  100. func (repo *Repository) IsCollaborator(userID int64) (bool, error) {
  101. return repo.isCollaborator(x, userID)
  102. }
  103. func (repo *Repository) changeCollaborationAccessMode(e Engine, uid int64, mode AccessMode) error {
  104. // Discard invalid input
  105. if mode <= AccessModeNone || mode > AccessModeOwner {
  106. return nil
  107. }
  108. collaboration := &Collaboration{
  109. RepoID: repo.ID,
  110. UserID: uid,
  111. }
  112. has, err := e.Get(collaboration)
  113. if err != nil {
  114. return fmt.Errorf("get collaboration: %v", err)
  115. } else if !has {
  116. return nil
  117. }
  118. if collaboration.Mode == mode {
  119. return nil
  120. }
  121. collaboration.Mode = mode
  122. if _, err = e.
  123. ID(collaboration.ID).
  124. Cols("mode").
  125. Update(collaboration); err != nil {
  126. return fmt.Errorf("update collaboration: %v", err)
  127. } else if _, err = e.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, uid, repo.ID); err != nil {
  128. return fmt.Errorf("update access table: %v", err)
  129. }
  130. return nil
  131. }
  132. // ChangeCollaborationAccessMode sets new access mode for the collaboration.
  133. func (repo *Repository) ChangeCollaborationAccessMode(uid int64, mode AccessMode) error {
  134. sess := x.NewSession()
  135. defer sess.Close()
  136. if err := sess.Begin(); err != nil {
  137. return err
  138. }
  139. if err := repo.changeCollaborationAccessMode(sess, uid, mode); err != nil {
  140. return err
  141. }
  142. return sess.Commit()
  143. }
  144. // DeleteCollaboration removes collaboration relation between the user and repository.
  145. func (repo *Repository) DeleteCollaboration(uid int64) (err error) {
  146. collaboration := &Collaboration{
  147. RepoID: repo.ID,
  148. UserID: uid,
  149. }
  150. sess := x.NewSession()
  151. defer sess.Close()
  152. if err = sess.Begin(); err != nil {
  153. return err
  154. }
  155. if has, err := sess.Delete(collaboration); err != nil || has == 0 {
  156. return err
  157. } else if err = repo.recalculateAccesses(sess); err != nil {
  158. return err
  159. }
  160. if err = watchRepo(sess, uid, repo.ID, false); err != nil {
  161. return err
  162. }
  163. if err = repo.reconsiderWatches(sess, uid); err != nil {
  164. return err
  165. }
  166. // Unassign a user from any issue (s)he has been assigned to in the repository
  167. if err := repo.reconsiderIssueAssignees(sess, uid); err != nil {
  168. return err
  169. }
  170. return sess.Commit()
  171. }
  172. func (repo *Repository) reconsiderIssueAssignees(e Engine, uid int64) error {
  173. user, err := getUserByID(e, uid)
  174. if err != nil {
  175. return err
  176. }
  177. if canAssigned, err := canBeAssigned(e, user, repo, true); err != nil || canAssigned {
  178. return err
  179. }
  180. if _, err := e.Where(builder.Eq{"assignee_id": uid}).
  181. In("issue_id", builder.Select("id").From("issue").Where(builder.Eq{"repo_id": repo.ID})).
  182. Delete(&IssueAssignees{}); err != nil {
  183. return fmt.Errorf("Could not delete assignee[%d] %v", uid, err)
  184. }
  185. return nil
  186. }
  187. func (repo *Repository) reconsiderWatches(e Engine, uid int64) error {
  188. if has, err := hasAccess(e, uid, repo); err != nil || has {
  189. return err
  190. }
  191. if err := watchRepo(e, uid, repo.ID, false); err != nil {
  192. return err
  193. }
  194. // Remove all IssueWatches a user has subscribed to in the repository
  195. return removeIssueWatchersByRepoID(e, uid, repo.ID)
  196. }
  197. func (repo *Repository) getRepoTeams(e Engine) (teams []*Team, err error) {
  198. return teams, e.
  199. Join("INNER", "team_repo", "team_repo.team_id = team.id").
  200. Where("team.org_id = ?", repo.OwnerID).
  201. And("team_repo.repo_id=?", repo.ID).
  202. OrderBy("CASE WHEN name LIKE '" + ownerTeamName + "' THEN '' ELSE name END").
  203. Find(&teams)
  204. }
  205. // GetRepoTeams gets the list of teams that has access to the repository
  206. func (repo *Repository) GetRepoTeams() ([]*Team, error) {
  207. return repo.getRepoTeams(x)
  208. }
  209. // IsOwnerMemberCollaborator checks if a provided user is the owner, a collaborator or a member of a team in a repository
  210. func (repo *Repository) IsOwnerMemberCollaborator(userID int64) (bool, error) {
  211. if repo.OwnerID == userID {
  212. return true, nil
  213. }
  214. teamMember, err := x.Join("INNER", "team_repo", "team_repo.team_id = team_user.team_id").
  215. Join("INNER", "team_unit", "team_unit.team_id = team_user.team_id").
  216. Where("team_repo.repo_id = ?", repo.ID).
  217. And("team_unit.`type` = ?", UnitTypeCode).
  218. And("team_user.uid = ?", userID).Table("team_user").Exist(&TeamUser{})
  219. if err != nil {
  220. return false, err
  221. }
  222. if teamMember {
  223. return true, nil
  224. }
  225. return x.Get(&Collaboration{RepoID: repo.ID, UserID: userID})
  226. }