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.

205 lines
5.2 KiB

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