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.

org.go 7.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright 2015 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 org
  6. import (
  7. "net/http"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/convert"
  11. api "code.gitea.io/gitea/modules/structs"
  12. "code.gitea.io/gitea/routers/api/v1/user"
  13. )
  14. func listUserOrgs(ctx *context.APIContext, u *models.User, all bool) {
  15. if err := u.GetOrganizations(all); err != nil {
  16. ctx.Error(http.StatusInternalServerError, "GetOrganizations", err)
  17. return
  18. }
  19. apiOrgs := make([]*api.Organization, len(u.Orgs))
  20. for i := range u.Orgs {
  21. apiOrgs[i] = convert.ToOrganization(u.Orgs[i])
  22. }
  23. ctx.JSON(http.StatusOK, &apiOrgs)
  24. }
  25. // ListMyOrgs list all my orgs
  26. func ListMyOrgs(ctx *context.APIContext) {
  27. // swagger:operation GET /user/orgs organization orgListCurrentUserOrgs
  28. // ---
  29. // summary: List the current user's organizations
  30. // produces:
  31. // - application/json
  32. // responses:
  33. // "200":
  34. // "$ref": "#/responses/OrganizationList"
  35. listUserOrgs(ctx, ctx.User, true)
  36. }
  37. // ListUserOrgs list user's orgs
  38. func ListUserOrgs(ctx *context.APIContext) {
  39. // swagger:operation GET /users/{username}/orgs organization orgListUserOrgs
  40. // ---
  41. // summary: List a user's organizations
  42. // produces:
  43. // - application/json
  44. // parameters:
  45. // - name: username
  46. // in: path
  47. // description: username of user
  48. // type: string
  49. // required: true
  50. // responses:
  51. // "200":
  52. // "$ref": "#/responses/OrganizationList"
  53. u := user.GetUserByParams(ctx)
  54. if ctx.Written() {
  55. return
  56. }
  57. listUserOrgs(ctx, u, ctx.User.IsAdmin)
  58. }
  59. // GetAll return list of all public organizations
  60. func GetAll(ctx *context.APIContext) {
  61. // swagger:operation Get /orgs organization orgGetAll
  62. // ---
  63. // summary: Get list of organizations
  64. // produces:
  65. // - application/json
  66. // parameters:
  67. // - name: page
  68. // in: query
  69. // description: page number of results to return (1-based)
  70. // type: integer
  71. // - name: limit
  72. // in: query
  73. // description: page size of results, maximum page size is 50
  74. // type: integer
  75. // responses:
  76. // "200":
  77. // "$ref": "#/responses/OrganizationList"
  78. vMode := []api.VisibleType{api.VisibleTypePublic}
  79. if ctx.IsSigned {
  80. vMode = append(vMode, api.VisibleTypeLimited)
  81. if ctx.User.IsAdmin {
  82. vMode = append(vMode, api.VisibleTypePrivate)
  83. }
  84. }
  85. publicOrgs, _, err := models.SearchUsers(&models.SearchUserOptions{
  86. Type: models.UserTypeOrganization,
  87. OrderBy: models.SearchOrderByAlphabetically,
  88. Page: ctx.QueryInt("page"),
  89. PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")),
  90. Visible: vMode,
  91. })
  92. if err != nil {
  93. ctx.Error(http.StatusInternalServerError, "SearchOrganizations", err)
  94. return
  95. }
  96. orgs := make([]*api.Organization, len(publicOrgs))
  97. for i := range publicOrgs {
  98. orgs[i] = convert.ToOrganization(publicOrgs[i])
  99. }
  100. ctx.JSON(http.StatusOK, &orgs)
  101. }
  102. // Create api for create organization
  103. func Create(ctx *context.APIContext, form api.CreateOrgOption) {
  104. // swagger:operation POST /orgs organization orgCreate
  105. // ---
  106. // summary: Create an organization
  107. // consumes:
  108. // - application/json
  109. // produces:
  110. // - application/json
  111. // parameters:
  112. // - name: organization
  113. // in: body
  114. // required: true
  115. // schema: { "$ref": "#/definitions/CreateOrgOption" }
  116. // responses:
  117. // "201":
  118. // "$ref": "#/responses/Organization"
  119. // "403":
  120. // "$ref": "#/responses/forbidden"
  121. // "422":
  122. // "$ref": "#/responses/validationError"
  123. if !ctx.User.CanCreateOrganization() {
  124. ctx.Error(http.StatusForbidden, "Create organization not allowed", nil)
  125. return
  126. }
  127. visibility := api.VisibleTypePublic
  128. if form.Visibility != "" {
  129. visibility = api.VisibilityModes[form.Visibility]
  130. }
  131. org := &models.User{
  132. Name: form.UserName,
  133. FullName: form.FullName,
  134. Description: form.Description,
  135. Website: form.Website,
  136. Location: form.Location,
  137. IsActive: true,
  138. Type: models.UserTypeOrganization,
  139. Visibility: visibility,
  140. RepoAdminChangeTeamAccess: form.RepoAdminChangeTeamAccess,
  141. }
  142. if err := models.CreateOrganization(org, ctx.User); err != nil {
  143. if models.IsErrUserAlreadyExist(err) ||
  144. models.IsErrNameReserved(err) ||
  145. models.IsErrNamePatternNotAllowed(err) {
  146. ctx.Error(http.StatusUnprocessableEntity, "", err)
  147. } else {
  148. ctx.Error(http.StatusInternalServerError, "CreateOrganization", err)
  149. }
  150. return
  151. }
  152. ctx.JSON(http.StatusCreated, convert.ToOrganization(org))
  153. }
  154. // Get get an organization
  155. func Get(ctx *context.APIContext) {
  156. // swagger:operation GET /orgs/{org} organization orgGet
  157. // ---
  158. // summary: Get an organization
  159. // produces:
  160. // - application/json
  161. // parameters:
  162. // - name: org
  163. // in: path
  164. // description: name of the organization to get
  165. // type: string
  166. // required: true
  167. // responses:
  168. // "200":
  169. // "$ref": "#/responses/Organization"
  170. if !models.HasOrgVisible(ctx.Org.Organization, ctx.User) {
  171. ctx.NotFound("HasOrgVisible", nil)
  172. return
  173. }
  174. ctx.JSON(http.StatusOK, convert.ToOrganization(ctx.Org.Organization))
  175. }
  176. // Edit change an organization's information
  177. func Edit(ctx *context.APIContext, form api.EditOrgOption) {
  178. // swagger:operation PATCH /orgs/{org} organization orgEdit
  179. // ---
  180. // summary: Edit an organization
  181. // consumes:
  182. // - application/json
  183. // produces:
  184. // - application/json
  185. // parameters:
  186. // - name: org
  187. // in: path
  188. // description: name of the organization to edit
  189. // type: string
  190. // required: true
  191. // - name: body
  192. // in: body
  193. // required: true
  194. // schema:
  195. // "$ref": "#/definitions/EditOrgOption"
  196. // responses:
  197. // "200":
  198. // "$ref": "#/responses/Organization"
  199. org := ctx.Org.Organization
  200. org.FullName = form.FullName
  201. org.Description = form.Description
  202. org.Website = form.Website
  203. org.Location = form.Location
  204. if form.Visibility != "" {
  205. org.Visibility = api.VisibilityModes[form.Visibility]
  206. }
  207. if err := models.UpdateUserCols(org, "full_name", "description", "website", "location", "visibility"); err != nil {
  208. ctx.Error(http.StatusInternalServerError, "EditOrganization", err)
  209. return
  210. }
  211. ctx.JSON(http.StatusOK, convert.ToOrganization(org))
  212. }
  213. //Delete an organization
  214. func Delete(ctx *context.APIContext) {
  215. // swagger:operation DELETE /orgs/{org} organization orgDelete
  216. // ---
  217. // summary: Delete an organization
  218. // produces:
  219. // - application/json
  220. // parameters:
  221. // - name: org
  222. // in: path
  223. // description: organization that is to be deleted
  224. // type: string
  225. // required: true
  226. // responses:
  227. // "204":
  228. // "$ref": "#/responses/empty"
  229. if err := models.DeleteOrganization(ctx.Org.Organization); err != nil {
  230. ctx.Error(http.StatusInternalServerError, "DeleteOrganization", err)
  231. return
  232. }
  233. ctx.Status(http.StatusNoContent)
  234. }