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.

302 lines
8.0 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
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 2017 The Gitea 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 org
  5. import (
  6. "fmt"
  7. "net/http"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/convert"
  11. "code.gitea.io/gitea/modules/setting"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/routers/api/v1/user"
  14. "code.gitea.io/gitea/routers/api/v1/utils"
  15. )
  16. // listMembers list an organization's members
  17. func listMembers(ctx *context.APIContext, publicOnly bool) {
  18. var members []*models.User
  19. members, _, err := models.FindOrgMembers(&models.FindOrgMembersOpts{
  20. OrgID: ctx.Org.Organization.ID,
  21. PublicOnly: publicOnly,
  22. ListOptions: utils.GetListOptions(ctx),
  23. })
  24. if err != nil {
  25. ctx.Error(http.StatusInternalServerError, "GetUsersByIDs", err)
  26. return
  27. }
  28. apiMembers := make([]*api.User, len(members))
  29. for i, member := range members {
  30. apiMembers[i] = convert.ToUser(member, ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin)
  31. }
  32. ctx.JSON(http.StatusOK, apiMembers)
  33. }
  34. // ListMembers list an organization's members
  35. func ListMembers(ctx *context.APIContext) {
  36. // swagger:operation GET /orgs/{org}/members organization orgListMembers
  37. // ---
  38. // summary: List an organization's members
  39. // produces:
  40. // - application/json
  41. // parameters:
  42. // - name: org
  43. // in: path
  44. // description: name of the organization
  45. // type: string
  46. // required: true
  47. // - name: page
  48. // in: query
  49. // description: page number of results to return (1-based)
  50. // type: integer
  51. // - name: limit
  52. // in: query
  53. // description: page size of results
  54. // type: integer
  55. // responses:
  56. // "200":
  57. // "$ref": "#/responses/UserList"
  58. publicOnly := true
  59. if ctx.User != nil {
  60. isMember, err := ctx.Org.Organization.IsOrgMember(ctx.User.ID)
  61. if err != nil {
  62. ctx.Error(http.StatusInternalServerError, "IsOrgMember", err)
  63. return
  64. }
  65. publicOnly = !isMember && !ctx.User.IsAdmin
  66. }
  67. listMembers(ctx, publicOnly)
  68. }
  69. // ListPublicMembers list an organization's public members
  70. func ListPublicMembers(ctx *context.APIContext) {
  71. // swagger:operation GET /orgs/{org}/public_members organization orgListPublicMembers
  72. // ---
  73. // summary: List an organization's public members
  74. // parameters:
  75. // - name: org
  76. // in: path
  77. // description: name of the organization
  78. // type: string
  79. // required: true
  80. // - name: page
  81. // in: query
  82. // description: page number of results to return (1-based)
  83. // type: integer
  84. // - name: limit
  85. // in: query
  86. // description: page size of results
  87. // type: integer
  88. // produces:
  89. // - application/json
  90. // responses:
  91. // "200":
  92. // "$ref": "#/responses/UserList"
  93. listMembers(ctx, true)
  94. }
  95. // IsMember check if a user is a member of an organization
  96. func IsMember(ctx *context.APIContext) {
  97. // swagger:operation GET /orgs/{org}/members/{username} organization orgIsMember
  98. // ---
  99. // summary: Check if a user is a member of an organization
  100. // parameters:
  101. // - name: org
  102. // in: path
  103. // description: name of the organization
  104. // type: string
  105. // required: true
  106. // - name: username
  107. // in: path
  108. // description: username of the user
  109. // type: string
  110. // required: true
  111. // responses:
  112. // "204":
  113. // description: user is a member
  114. // "302":
  115. // description: redirection to /orgs/{org}/public_members/{username}
  116. // "404":
  117. // description: user is not a member
  118. userToCheck := user.GetUserByParams(ctx)
  119. if ctx.Written() {
  120. return
  121. }
  122. if ctx.User != nil {
  123. userIsMember, err := ctx.Org.Organization.IsOrgMember(ctx.User.ID)
  124. if err != nil {
  125. ctx.Error(http.StatusInternalServerError, "IsOrgMember", err)
  126. return
  127. } else if userIsMember || ctx.User.IsAdmin {
  128. userToCheckIsMember, err := ctx.Org.Organization.IsOrgMember(userToCheck.ID)
  129. if err != nil {
  130. ctx.Error(http.StatusInternalServerError, "IsOrgMember", err)
  131. } else if userToCheckIsMember {
  132. ctx.Status(http.StatusNoContent)
  133. } else {
  134. ctx.NotFound()
  135. }
  136. return
  137. } else if ctx.User.ID == userToCheck.ID {
  138. ctx.NotFound()
  139. return
  140. }
  141. }
  142. redirectURL := fmt.Sprintf("%sapi/v1/orgs/%s/public_members/%s",
  143. setting.AppURL, ctx.Org.Organization.Name, userToCheck.Name)
  144. ctx.Redirect(redirectURL, 302)
  145. }
  146. // IsPublicMember check if a user is a public member of an organization
  147. func IsPublicMember(ctx *context.APIContext) {
  148. // swagger:operation GET /orgs/{org}/public_members/{username} organization orgIsPublicMember
  149. // ---
  150. // summary: Check if a user is a public member of an organization
  151. // parameters:
  152. // - name: org
  153. // in: path
  154. // description: name of the organization
  155. // type: string
  156. // required: true
  157. // - name: username
  158. // in: path
  159. // description: username of the user
  160. // type: string
  161. // required: true
  162. // responses:
  163. // "204":
  164. // description: user is a public member
  165. // "404":
  166. // description: user is not a public member
  167. userToCheck := user.GetUserByParams(ctx)
  168. if ctx.Written() {
  169. return
  170. }
  171. if userToCheck.IsPublicMember(ctx.Org.Organization.ID) {
  172. ctx.Status(http.StatusNoContent)
  173. } else {
  174. ctx.NotFound()
  175. }
  176. }
  177. // PublicizeMember make a member's membership public
  178. func PublicizeMember(ctx *context.APIContext) {
  179. // swagger:operation PUT /orgs/{org}/public_members/{username} organization orgPublicizeMember
  180. // ---
  181. // summary: Publicize a user's membership
  182. // produces:
  183. // - application/json
  184. // parameters:
  185. // - name: org
  186. // in: path
  187. // description: name of the organization
  188. // type: string
  189. // required: true
  190. // - name: username
  191. // in: path
  192. // description: username of the user
  193. // type: string
  194. // required: true
  195. // responses:
  196. // "204":
  197. // description: membership publicized
  198. // "403":
  199. // "$ref": "#/responses/forbidden"
  200. userToPublicize := user.GetUserByParams(ctx)
  201. if ctx.Written() {
  202. return
  203. }
  204. if userToPublicize.ID != ctx.User.ID {
  205. ctx.Error(http.StatusForbidden, "", "Cannot publicize another member")
  206. return
  207. }
  208. err := models.ChangeOrgUserStatus(ctx.Org.Organization.ID, userToPublicize.ID, true)
  209. if err != nil {
  210. ctx.Error(http.StatusInternalServerError, "ChangeOrgUserStatus", err)
  211. return
  212. }
  213. ctx.Status(http.StatusNoContent)
  214. }
  215. // ConcealMember make a member's membership not public
  216. func ConcealMember(ctx *context.APIContext) {
  217. // swagger:operation DELETE /orgs/{org}/public_members/{username} organization orgConcealMember
  218. // ---
  219. // summary: Conceal a user's membership
  220. // produces:
  221. // - application/json
  222. // parameters:
  223. // - name: org
  224. // in: path
  225. // description: name of the organization
  226. // type: string
  227. // required: true
  228. // - name: username
  229. // in: path
  230. // description: username of the user
  231. // type: string
  232. // required: true
  233. // responses:
  234. // "204":
  235. // "$ref": "#/responses/empty"
  236. // "403":
  237. // "$ref": "#/responses/forbidden"
  238. userToConceal := user.GetUserByParams(ctx)
  239. if ctx.Written() {
  240. return
  241. }
  242. if userToConceal.ID != ctx.User.ID {
  243. ctx.Error(http.StatusForbidden, "", "Cannot conceal another member")
  244. return
  245. }
  246. err := models.ChangeOrgUserStatus(ctx.Org.Organization.ID, userToConceal.ID, false)
  247. if err != nil {
  248. ctx.Error(http.StatusInternalServerError, "ChangeOrgUserStatus", err)
  249. return
  250. }
  251. ctx.Status(http.StatusNoContent)
  252. }
  253. // DeleteMember remove a member from an organization
  254. func DeleteMember(ctx *context.APIContext) {
  255. // swagger:operation DELETE /orgs/{org}/members/{username} organization orgDeleteMember
  256. // ---
  257. // summary: Remove a member from an organization
  258. // produces:
  259. // - application/json
  260. // parameters:
  261. // - name: org
  262. // in: path
  263. // description: name of the organization
  264. // type: string
  265. // required: true
  266. // - name: username
  267. // in: path
  268. // description: username of the user
  269. // type: string
  270. // required: true
  271. // responses:
  272. // "204":
  273. // description: member removed
  274. member := user.GetUserByParams(ctx)
  275. if ctx.Written() {
  276. return
  277. }
  278. if err := ctx.Org.Organization.RemoveMember(member.ID); err != nil {
  279. ctx.Error(http.StatusInternalServerError, "RemoveMember", err)
  280. }
  281. ctx.Status(http.StatusNoContent)
  282. }