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.

collaborators.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. "code.gitea.io/gitea/models/perm"
  9. access_model "code.gitea.io/gitea/models/perm/access"
  10. repo_model "code.gitea.io/gitea/models/repo"
  11. user_model "code.gitea.io/gitea/models/user"
  12. repo_module "code.gitea.io/gitea/modules/repository"
  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. repo_service "code.gitea.io/gitea/services/repository"
  19. )
  20. // ListCollaborators list a repository's collaborators
  21. func ListCollaborators(ctx *context.APIContext) {
  22. // swagger:operation GET /repos/{owner}/{repo}/collaborators repository repoListCollaborators
  23. // ---
  24. // summary: List a repository's collaborators
  25. // produces:
  26. // - application/json
  27. // parameters:
  28. // - name: owner
  29. // in: path
  30. // description: owner of the repo
  31. // type: string
  32. // required: true
  33. // - name: repo
  34. // in: path
  35. // description: name of the repo
  36. // type: string
  37. // required: true
  38. // - name: page
  39. // in: query
  40. // description: page number of results to return (1-based)
  41. // type: integer
  42. // - name: limit
  43. // in: query
  44. // description: page size of results
  45. // type: integer
  46. // responses:
  47. // "200":
  48. // "$ref": "#/responses/UserList"
  49. // "404":
  50. // "$ref": "#/responses/notFound"
  51. collaborators, total, err := repo_model.GetCollaborators(ctx, &repo_model.FindCollaborationOptions{
  52. ListOptions: utils.GetListOptions(ctx),
  53. RepoID: ctx.Repo.Repository.ID,
  54. })
  55. if err != nil {
  56. ctx.Error(http.StatusInternalServerError, "ListCollaborators", err)
  57. return
  58. }
  59. users := make([]*api.User, len(collaborators))
  60. for i, collaborator := range collaborators {
  61. users[i] = convert.ToUser(ctx, collaborator.User, ctx.Doer)
  62. }
  63. ctx.SetTotalCountHeader(total)
  64. ctx.JSON(http.StatusOK, users)
  65. }
  66. // IsCollaborator check if a user is a collaborator of a repository
  67. func IsCollaborator(ctx *context.APIContext) {
  68. // swagger:operation GET /repos/{owner}/{repo}/collaborators/{collaborator} repository repoCheckCollaborator
  69. // ---
  70. // summary: Check if a user is a collaborator of a repository
  71. // produces:
  72. // - application/json
  73. // parameters:
  74. // - name: owner
  75. // in: path
  76. // description: owner of the repo
  77. // type: string
  78. // required: true
  79. // - name: repo
  80. // in: path
  81. // description: name of the repo
  82. // type: string
  83. // required: true
  84. // - name: collaborator
  85. // in: path
  86. // description: username of the collaborator
  87. // type: string
  88. // required: true
  89. // responses:
  90. // "204":
  91. // "$ref": "#/responses/empty"
  92. // "404":
  93. // "$ref": "#/responses/notFound"
  94. // "422":
  95. // "$ref": "#/responses/validationError"
  96. user, err := user_model.GetUserByName(ctx, ctx.Params(":collaborator"))
  97. if err != nil {
  98. if user_model.IsErrUserNotExist(err) {
  99. ctx.Error(http.StatusUnprocessableEntity, "", err)
  100. } else {
  101. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  102. }
  103. return
  104. }
  105. isColab, err := repo_model.IsCollaborator(ctx, ctx.Repo.Repository.ID, user.ID)
  106. if err != nil {
  107. ctx.Error(http.StatusInternalServerError, "IsCollaborator", err)
  108. return
  109. }
  110. if isColab {
  111. ctx.Status(http.StatusNoContent)
  112. } else {
  113. ctx.NotFound()
  114. }
  115. }
  116. // AddCollaborator add a collaborator to a repository
  117. func AddCollaborator(ctx *context.APIContext) {
  118. // swagger:operation PUT /repos/{owner}/{repo}/collaborators/{collaborator} repository repoAddCollaborator
  119. // ---
  120. // summary: Add a collaborator to a repository
  121. // produces:
  122. // - application/json
  123. // parameters:
  124. // - name: owner
  125. // in: path
  126. // description: owner of the repo
  127. // type: string
  128. // required: true
  129. // - name: repo
  130. // in: path
  131. // description: name of the repo
  132. // type: string
  133. // required: true
  134. // - name: collaborator
  135. // in: path
  136. // description: username of the collaborator to add
  137. // type: string
  138. // required: true
  139. // - name: body
  140. // in: body
  141. // schema:
  142. // "$ref": "#/definitions/AddCollaboratorOption"
  143. // responses:
  144. // "204":
  145. // "$ref": "#/responses/empty"
  146. // "403":
  147. // "$ref": "#/responses/forbidden"
  148. // "404":
  149. // "$ref": "#/responses/notFound"
  150. // "422":
  151. // "$ref": "#/responses/validationError"
  152. form := web.GetForm(ctx).(*api.AddCollaboratorOption)
  153. collaborator, err := user_model.GetUserByName(ctx, ctx.Params(":collaborator"))
  154. if err != nil {
  155. if user_model.IsErrUserNotExist(err) {
  156. ctx.Error(http.StatusUnprocessableEntity, "", err)
  157. } else {
  158. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  159. }
  160. return
  161. }
  162. if !collaborator.IsActive {
  163. ctx.Error(http.StatusInternalServerError, "InactiveCollaborator", errors.New("collaborator's account is inactive"))
  164. return
  165. }
  166. if err := repo_module.AddCollaborator(ctx, ctx.Repo.Repository, collaborator); err != nil {
  167. if errors.Is(err, user_model.ErrBlockedUser) {
  168. ctx.Error(http.StatusForbidden, "AddCollaborator", err)
  169. } else {
  170. ctx.Error(http.StatusInternalServerError, "AddCollaborator", err)
  171. }
  172. return
  173. }
  174. if form.Permission != nil {
  175. if err := repo_model.ChangeCollaborationAccessMode(ctx, ctx.Repo.Repository, collaborator.ID, perm.ParseAccessMode(*form.Permission)); err != nil {
  176. ctx.Error(http.StatusInternalServerError, "ChangeCollaborationAccessMode", err)
  177. return
  178. }
  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.Params(":collaborator"))
  213. if err != nil {
  214. if user_model.IsErrUserNotExist(err) {
  215. ctx.Error(http.StatusUnprocessableEntity, "", err)
  216. } else {
  217. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  218. }
  219. return
  220. }
  221. if err := repo_service.DeleteCollaboration(ctx, ctx.Repo.Repository, collaborator); err != nil {
  222. ctx.Error(http.StatusInternalServerError, "DeleteCollaboration", 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
  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. if !ctx.Doer.IsAdmin && ctx.Doer.LoginName != ctx.Params(":collaborator") && !ctx.IsUserRepoAdmin() {
  258. ctx.Error(http.StatusForbidden, "User", "Only admins can query all permissions, repo admins can query all repo permissions, collaborators can query only their own")
  259. return
  260. }
  261. collaborator, err := user_model.GetUserByName(ctx, ctx.Params(":collaborator"))
  262. if err != nil {
  263. if user_model.IsErrUserNotExist(err) {
  264. ctx.Error(http.StatusNotFound, "GetUserByName", err)
  265. } else {
  266. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  267. }
  268. return
  269. }
  270. permission, err := access_model.GetUserRepoPermission(ctx, ctx.Repo.Repository, collaborator)
  271. if err != nil {
  272. ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err)
  273. return
  274. }
  275. ctx.JSON(http.StatusOK, convert.ToUserAndPermission(ctx, collaborator, ctx.ContextUser, permission.AccessMode))
  276. }
  277. // GetReviewers return all users that can be requested to review in this repo
  278. func GetReviewers(ctx *context.APIContext) {
  279. // swagger:operation GET /repos/{owner}/{repo}/reviewers repository repoGetReviewers
  280. // ---
  281. // summary: Return all users that can be requested to review in this repo
  282. // produces:
  283. // - application/json
  284. // parameters:
  285. // - name: owner
  286. // in: path
  287. // description: owner of the repo
  288. // type: string
  289. // required: true
  290. // - name: repo
  291. // in: path
  292. // description: name of the repo
  293. // type: string
  294. // required: true
  295. // responses:
  296. // "200":
  297. // "$ref": "#/responses/UserList"
  298. // "404":
  299. // "$ref": "#/responses/notFound"
  300. reviewers, err := repo_model.GetReviewers(ctx, ctx.Repo.Repository, ctx.Doer.ID, 0)
  301. if err != nil {
  302. ctx.Error(http.StatusInternalServerError, "ListCollaborators", err)
  303. return
  304. }
  305. ctx.JSON(http.StatusOK, convert.ToUsers(ctx, ctx.Doer, reviewers))
  306. }
  307. // GetAssignees return all users that have write access and can be assigned to issues
  308. func GetAssignees(ctx *context.APIContext) {
  309. // swagger:operation GET /repos/{owner}/{repo}/assignees repository repoGetAssignees
  310. // ---
  311. // summary: Return all users that have write access and can be assigned to issues
  312. // produces:
  313. // - application/json
  314. // parameters:
  315. // - name: owner
  316. // in: path
  317. // description: owner of the repo
  318. // type: string
  319. // required: true
  320. // - name: repo
  321. // in: path
  322. // description: name of the repo
  323. // type: string
  324. // required: true
  325. // responses:
  326. // "200":
  327. // "$ref": "#/responses/UserList"
  328. // "404":
  329. // "$ref": "#/responses/notFound"
  330. assignees, err := repo_model.GetRepoAssignees(ctx, ctx.Repo.Repository)
  331. if err != nil {
  332. ctx.Error(http.StatusInternalServerError, "ListCollaborators", err)
  333. return
  334. }
  335. ctx.JSON(http.StatusOK, convert.ToUsers(ctx, ctx.Doer, assignees))
  336. }