gitea源码

collaborators.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package repo
  5. import (
  6. "errors"
  7. "net/http"
  8. "strings"
  9. "code.gitea.io/gitea/models/perm"
  10. access_model "code.gitea.io/gitea/models/perm/access"
  11. repo_model "code.gitea.io/gitea/models/repo"
  12. user_model "code.gitea.io/gitea/models/user"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "code.gitea.io/gitea/modules/web"
  15. "code.gitea.io/gitea/routers/api/v1/utils"
  16. "code.gitea.io/gitea/services/context"
  17. "code.gitea.io/gitea/services/convert"
  18. issue_service "code.gitea.io/gitea/services/issue"
  19. pull_service "code.gitea.io/gitea/services/pull"
  20. repo_service "code.gitea.io/gitea/services/repository"
  21. )
  22. // ListCollaborators list a repository's collaborators
  23. func ListCollaborators(ctx *context.APIContext) {
  24. // swagger:operation GET /repos/{owner}/{repo}/collaborators repository repoListCollaborators
  25. // ---
  26. // summary: List a repository's collaborators
  27. // produces:
  28. // - application/json
  29. // parameters:
  30. // - name: owner
  31. // in: path
  32. // description: owner of the repo
  33. // type: string
  34. // required: true
  35. // - name: repo
  36. // in: path
  37. // description: name of the repo
  38. // type: string
  39. // required: true
  40. // - name: page
  41. // in: query
  42. // description: page number of results to return (1-based)
  43. // type: integer
  44. // - name: limit
  45. // in: query
  46. // description: page size of results
  47. // type: integer
  48. // responses:
  49. // "200":
  50. // "$ref": "#/responses/UserList"
  51. // "404":
  52. // "$ref": "#/responses/notFound"
  53. collaborators, total, err := repo_model.GetCollaborators(ctx, &repo_model.FindCollaborationOptions{
  54. ListOptions: utils.GetListOptions(ctx),
  55. RepoID: ctx.Repo.Repository.ID,
  56. })
  57. if err != nil {
  58. ctx.APIErrorInternal(err)
  59. return
  60. }
  61. users := make([]*api.User, len(collaborators))
  62. for i, collaborator := range collaborators {
  63. users[i] = convert.ToUser(ctx, collaborator.User, ctx.Doer)
  64. }
  65. ctx.SetTotalCountHeader(total)
  66. ctx.JSON(http.StatusOK, users)
  67. }
  68. // IsCollaborator check if a user is a collaborator of a repository
  69. func IsCollaborator(ctx *context.APIContext) {
  70. // swagger:operation GET /repos/{owner}/{repo}/collaborators/{collaborator} repository repoCheckCollaborator
  71. // ---
  72. // summary: Check if a user is a collaborator of a repository
  73. // produces:
  74. // - application/json
  75. // parameters:
  76. // - name: owner
  77. // in: path
  78. // description: owner of the repo
  79. // type: string
  80. // required: true
  81. // - name: repo
  82. // in: path
  83. // description: name of the repo
  84. // type: string
  85. // required: true
  86. // - name: collaborator
  87. // in: path
  88. // description: username of the user to check for being a collaborator
  89. // type: string
  90. // required: true
  91. // responses:
  92. // "204":
  93. // "$ref": "#/responses/empty"
  94. // "404":
  95. // "$ref": "#/responses/notFound"
  96. // "422":
  97. // "$ref": "#/responses/validationError"
  98. user, err := user_model.GetUserByName(ctx, ctx.PathParam("collaborator"))
  99. if err != nil {
  100. if user_model.IsErrUserNotExist(err) {
  101. ctx.APIError(http.StatusUnprocessableEntity, err)
  102. } else {
  103. ctx.APIErrorInternal(err)
  104. }
  105. return
  106. }
  107. isColab, err := repo_model.IsCollaborator(ctx, ctx.Repo.Repository.ID, user.ID)
  108. if err != nil {
  109. ctx.APIErrorInternal(err)
  110. return
  111. }
  112. if isColab {
  113. ctx.Status(http.StatusNoContent)
  114. } else {
  115. ctx.APIErrorNotFound()
  116. }
  117. }
  118. // AddOrUpdateCollaborator add or update a collaborator to a repository
  119. func AddOrUpdateCollaborator(ctx *context.APIContext) {
  120. // swagger:operation PUT /repos/{owner}/{repo}/collaborators/{collaborator} repository repoAddCollaborator
  121. // ---
  122. // summary: Add or Update a collaborator to a repository
  123. // produces:
  124. // - application/json
  125. // parameters:
  126. // - name: owner
  127. // in: path
  128. // description: owner of the repo
  129. // type: string
  130. // required: true
  131. // - name: repo
  132. // in: path
  133. // description: name of the repo
  134. // type: string
  135. // required: true
  136. // - name: collaborator
  137. // in: path
  138. // description: username of the user to add or update as a collaborator
  139. // type: string
  140. // required: true
  141. // - name: body
  142. // in: body
  143. // schema:
  144. // "$ref": "#/definitions/AddCollaboratorOption"
  145. // responses:
  146. // "204":
  147. // "$ref": "#/responses/empty"
  148. // "403":
  149. // "$ref": "#/responses/forbidden"
  150. // "404":
  151. // "$ref": "#/responses/notFound"
  152. // "422":
  153. // "$ref": "#/responses/validationError"
  154. form := web.GetForm(ctx).(*api.AddCollaboratorOption)
  155. collaborator, err := user_model.GetUserByName(ctx, ctx.PathParam("collaborator"))
  156. if err != nil {
  157. if user_model.IsErrUserNotExist(err) {
  158. ctx.APIError(http.StatusUnprocessableEntity, err)
  159. } else {
  160. ctx.APIErrorInternal(err)
  161. }
  162. return
  163. }
  164. if !collaborator.IsActive {
  165. ctx.APIErrorInternal(errors.New("collaborator's account is inactive"))
  166. return
  167. }
  168. p := perm.AccessModeWrite
  169. if form.Permission != nil {
  170. p = perm.ParseAccessMode(*form.Permission, perm.AccessModeRead, perm.AccessModeWrite, perm.AccessModeAdmin)
  171. }
  172. if err := repo_service.AddOrUpdateCollaborator(ctx, ctx.Repo.Repository, collaborator, p); err != nil {
  173. if errors.Is(err, user_model.ErrBlockedUser) {
  174. ctx.APIError(http.StatusForbidden, err)
  175. } else {
  176. ctx.APIErrorInternal(err)
  177. }
  178. return
  179. }
  180. ctx.Status(http.StatusNoContent)
  181. }
  182. // DeleteCollaborator delete a collaborator from a repository
  183. func DeleteCollaborator(ctx *context.APIContext) {
  184. // swagger:operation DELETE /repos/{owner}/{repo}/collaborators/{collaborator} repository repoDeleteCollaborator
  185. // ---
  186. // summary: Delete a collaborator from a repository
  187. // produces:
  188. // - application/json
  189. // parameters:
  190. // - name: owner
  191. // in: path
  192. // description: owner of the repo
  193. // type: string
  194. // required: true
  195. // - name: repo
  196. // in: path
  197. // description: name of the repo
  198. // type: string
  199. // required: true
  200. // - name: collaborator
  201. // in: path
  202. // description: username of the collaborator to delete
  203. // type: string
  204. // required: true
  205. // responses:
  206. // "204":
  207. // "$ref": "#/responses/empty"
  208. // "404":
  209. // "$ref": "#/responses/notFound"
  210. // "422":
  211. // "$ref": "#/responses/validationError"
  212. collaborator, err := user_model.GetUserByName(ctx, ctx.PathParam("collaborator"))
  213. if err != nil {
  214. if user_model.IsErrUserNotExist(err) {
  215. ctx.APIError(http.StatusUnprocessableEntity, err)
  216. } else {
  217. ctx.APIErrorInternal(err)
  218. }
  219. return
  220. }
  221. if err := repo_service.DeleteCollaboration(ctx, ctx.Repo.Repository, collaborator); err != nil {
  222. ctx.APIErrorInternal(err)
  223. return
  224. }
  225. ctx.Status(http.StatusNoContent)
  226. }
  227. // GetRepoPermissions gets repository permissions for a user
  228. func GetRepoPermissions(ctx *context.APIContext) {
  229. // swagger:operation GET /repos/{owner}/{repo}/collaborators/{collaborator}/permission repository repoGetRepoPermissions
  230. // ---
  231. // summary: Get repository permissions for a user
  232. // produces:
  233. // - application/json
  234. // parameters:
  235. // - name: owner
  236. // in: path
  237. // description: owner of the repo
  238. // type: string
  239. // required: true
  240. // - name: repo
  241. // in: path
  242. // description: name of the repo
  243. // type: string
  244. // required: true
  245. // - name: collaborator
  246. // in: path
  247. // description: username of the collaborator whose permissions are to be obtained
  248. // type: string
  249. // required: true
  250. // responses:
  251. // "200":
  252. // "$ref": "#/responses/RepoCollaboratorPermission"
  253. // "404":
  254. // "$ref": "#/responses/notFound"
  255. // "403":
  256. // "$ref": "#/responses/forbidden"
  257. collaboratorUsername := ctx.PathParam("collaborator")
  258. if !ctx.Doer.IsAdmin && !strings.EqualFold(ctx.Doer.LowerName, collaboratorUsername) && !ctx.IsUserRepoAdmin() {
  259. ctx.APIError(http.StatusForbidden, "Only admins can query all permissions, repo admins can query all repo permissions, collaborators can query only their own")
  260. return
  261. }
  262. collaborator, err := user_model.GetUserByName(ctx, collaboratorUsername)
  263. if err != nil {
  264. if user_model.IsErrUserNotExist(err) {
  265. ctx.APIError(http.StatusNotFound, err)
  266. } else {
  267. ctx.APIErrorInternal(err)
  268. }
  269. return
  270. }
  271. permission, err := access_model.GetUserRepoPermission(ctx, ctx.Repo.Repository, collaborator)
  272. if err != nil {
  273. ctx.APIErrorInternal(err)
  274. return
  275. }
  276. ctx.JSON(http.StatusOK, convert.ToUserAndPermission(ctx, collaborator, ctx.ContextUser, permission.AccessMode))
  277. }
  278. // GetReviewers return all users that can be requested to review in this repo
  279. func GetReviewers(ctx *context.APIContext) {
  280. // swagger:operation GET /repos/{owner}/{repo}/reviewers repository repoGetReviewers
  281. // ---
  282. // summary: Return all users that can be requested to review in this repo
  283. // produces:
  284. // - application/json
  285. // parameters:
  286. // - name: owner
  287. // in: path
  288. // description: owner of the repo
  289. // type: string
  290. // required: true
  291. // - name: repo
  292. // in: path
  293. // description: name of the repo
  294. // type: string
  295. // required: true
  296. // responses:
  297. // "200":
  298. // "$ref": "#/responses/UserList"
  299. // "404":
  300. // "$ref": "#/responses/notFound"
  301. canChooseReviewer := issue_service.CanDoerChangeReviewRequests(ctx, ctx.Doer, ctx.Repo.Repository, 0)
  302. if !canChooseReviewer {
  303. ctx.APIError(http.StatusForbidden, errors.New("doer has no permission to get reviewers"))
  304. return
  305. }
  306. reviewers, err := pull_service.GetReviewers(ctx, ctx.Repo.Repository, ctx.Doer.ID, 0)
  307. if err != nil {
  308. ctx.APIErrorInternal(err)
  309. return
  310. }
  311. ctx.JSON(http.StatusOK, convert.ToUsers(ctx, ctx.Doer, reviewers))
  312. }
  313. // GetAssignees return all users that have write access and can be assigned to issues
  314. func GetAssignees(ctx *context.APIContext) {
  315. // swagger:operation GET /repos/{owner}/{repo}/assignees repository repoGetAssignees
  316. // ---
  317. // summary: Return all users that have write access and can be assigned to issues
  318. // produces:
  319. // - application/json
  320. // parameters:
  321. // - name: owner
  322. // in: path
  323. // description: owner of the repo
  324. // type: string
  325. // required: true
  326. // - name: repo
  327. // in: path
  328. // description: name of the repo
  329. // type: string
  330. // required: true
  331. // responses:
  332. // "200":
  333. // "$ref": "#/responses/UserList"
  334. // "404":
  335. // "$ref": "#/responses/notFound"
  336. assignees, err := repo_model.GetRepoAssignees(ctx, ctx.Repo.Repository)
  337. if err != nil {
  338. ctx.APIErrorInternal(err)
  339. return
  340. }
  341. ctx.JSON(http.StatusOK, convert.ToUsers(ctx, ctx.Doer, assignees))
  342. }