You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

collaborators.go 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "errors"
  8. "net/http"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/convert"
  12. api "code.gitea.io/gitea/modules/structs"
  13. )
  14. // ListCollaborators list a repository's collaborators
  15. func ListCollaborators(ctx *context.APIContext) {
  16. // swagger:operation GET /repos/{owner}/{repo}/collaborators repository repoListCollaborators
  17. // ---
  18. // summary: List a repository's collaborators
  19. // produces:
  20. // - application/json
  21. // parameters:
  22. // - name: owner
  23. // in: path
  24. // description: owner of the repo
  25. // type: string
  26. // required: true
  27. // - name: repo
  28. // in: path
  29. // description: name of the repo
  30. // type: string
  31. // required: true
  32. // responses:
  33. // "200":
  34. // "$ref": "#/responses/UserList"
  35. collaborators, err := ctx.Repo.Repository.GetCollaborators()
  36. if err != nil {
  37. ctx.Error(http.StatusInternalServerError, "ListCollaborators", err)
  38. return
  39. }
  40. users := make([]*api.User, len(collaborators))
  41. for i, collaborator := range collaborators {
  42. users[i] = convert.ToUser(collaborator.User, ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin)
  43. }
  44. ctx.JSON(http.StatusOK, users)
  45. }
  46. // IsCollaborator check if a user is a collaborator of a repository
  47. func IsCollaborator(ctx *context.APIContext) {
  48. // swagger:operation GET /repos/{owner}/{repo}/collaborators/{collaborator} repository repoCheckCollaborator
  49. // ---
  50. // summary: Check if a user is a collaborator of a repository
  51. // produces:
  52. // - application/json
  53. // parameters:
  54. // - name: owner
  55. // in: path
  56. // description: owner of the repo
  57. // type: string
  58. // required: true
  59. // - name: repo
  60. // in: path
  61. // description: name of the repo
  62. // type: string
  63. // required: true
  64. // - name: collaborator
  65. // in: path
  66. // description: username of the collaborator
  67. // type: string
  68. // required: true
  69. // responses:
  70. // "204":
  71. // "$ref": "#/responses/empty"
  72. // "404":
  73. // "$ref": "#/responses/notFound"
  74. // "422":
  75. // "$ref": "#/responses/validationError"
  76. user, err := models.GetUserByName(ctx.Params(":collaborator"))
  77. if err != nil {
  78. if models.IsErrUserNotExist(err) {
  79. ctx.Error(http.StatusUnprocessableEntity, "", err)
  80. } else {
  81. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  82. }
  83. return
  84. }
  85. isColab, err := ctx.Repo.Repository.IsCollaborator(user.ID)
  86. if err != nil {
  87. ctx.Error(http.StatusInternalServerError, "IsCollaborator", err)
  88. return
  89. }
  90. if isColab {
  91. ctx.Status(http.StatusNoContent)
  92. } else {
  93. ctx.NotFound()
  94. }
  95. }
  96. // AddCollaborator add a collaborator to a repository
  97. func AddCollaborator(ctx *context.APIContext, form api.AddCollaboratorOption) {
  98. // swagger:operation PUT /repos/{owner}/{repo}/collaborators/{collaborator} repository repoAddCollaborator
  99. // ---
  100. // summary: Add a collaborator to a repository
  101. // produces:
  102. // - application/json
  103. // parameters:
  104. // - name: owner
  105. // in: path
  106. // description: owner of the repo
  107. // type: string
  108. // required: true
  109. // - name: repo
  110. // in: path
  111. // description: name of the repo
  112. // type: string
  113. // required: true
  114. // - name: collaborator
  115. // in: path
  116. // description: username of the collaborator to add
  117. // type: string
  118. // required: true
  119. // - name: body
  120. // in: body
  121. // schema:
  122. // "$ref": "#/definitions/AddCollaboratorOption"
  123. // responses:
  124. // "204":
  125. // "$ref": "#/responses/empty"
  126. // "422":
  127. // "$ref": "#/responses/validationError"
  128. collaborator, err := models.GetUserByName(ctx.Params(":collaborator"))
  129. if err != nil {
  130. if models.IsErrUserNotExist(err) {
  131. ctx.Error(http.StatusUnprocessableEntity, "", err)
  132. } else {
  133. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  134. }
  135. return
  136. }
  137. if !collaborator.IsActive {
  138. ctx.Error(http.StatusInternalServerError, "InactiveCollaborator", errors.New("collaborator's account is inactive"))
  139. return
  140. }
  141. if err := ctx.Repo.Repository.AddCollaborator(collaborator); err != nil {
  142. ctx.Error(http.StatusInternalServerError, "AddCollaborator", err)
  143. return
  144. }
  145. if form.Permission != nil {
  146. if err := ctx.Repo.Repository.ChangeCollaborationAccessMode(collaborator.ID, models.ParseAccessMode(*form.Permission)); err != nil {
  147. ctx.Error(http.StatusInternalServerError, "ChangeCollaborationAccessMode", err)
  148. return
  149. }
  150. }
  151. ctx.Status(http.StatusNoContent)
  152. }
  153. // DeleteCollaborator delete a collaborator from a repository
  154. func DeleteCollaborator(ctx *context.APIContext) {
  155. // swagger:operation DELETE /repos/{owner}/{repo}/collaborators/{collaborator} repository repoDeleteCollaborator
  156. // ---
  157. // summary: Delete a collaborator from a repository
  158. // produces:
  159. // - application/json
  160. // parameters:
  161. // - name: owner
  162. // in: path
  163. // description: owner of the repo
  164. // type: string
  165. // required: true
  166. // - name: repo
  167. // in: path
  168. // description: name of the repo
  169. // type: string
  170. // required: true
  171. // - name: collaborator
  172. // in: path
  173. // description: username of the collaborator to delete
  174. // type: string
  175. // required: true
  176. // responses:
  177. // "204":
  178. // "$ref": "#/responses/empty"
  179. // "422":
  180. // "$ref": "#/responses/validationError"
  181. collaborator, err := models.GetUserByName(ctx.Params(":collaborator"))
  182. if err != nil {
  183. if models.IsErrUserNotExist(err) {
  184. ctx.Error(http.StatusUnprocessableEntity, "", err)
  185. } else {
  186. ctx.Error(http.StatusInternalServerError, "GetUserByName", err)
  187. }
  188. return
  189. }
  190. if err := ctx.Repo.Repository.DeleteCollaboration(collaborator.ID); err != nil {
  191. ctx.Error(http.StatusInternalServerError, "DeleteCollaboration", err)
  192. return
  193. }
  194. ctx.Status(http.StatusNoContent)
  195. }