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.4 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. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/convert"
  10. api "code.gitea.io/gitea/modules/structs"
  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. RepoAdminChangeTeamAccess: form.RepoAdminChangeTeamAccess,
  97. }
  98. if err := models.CreateOrganization(org, ctx.User); err != nil {
  99. if models.IsErrUserAlreadyExist(err) ||
  100. models.IsErrNameReserved(err) ||
  101. models.IsErrNamePatternNotAllowed(err) {
  102. ctx.Error(422, "", err)
  103. } else {
  104. ctx.Error(500, "CreateOrganization", err)
  105. }
  106. return
  107. }
  108. ctx.JSON(201, convert.ToOrganization(org))
  109. }
  110. // Get get an organization
  111. func Get(ctx *context.APIContext) {
  112. // swagger:operation GET /orgs/{org} organization orgGet
  113. // ---
  114. // summary: Get an organization
  115. // produces:
  116. // - application/json
  117. // parameters:
  118. // - name: org
  119. // in: path
  120. // description: name of the organization to get
  121. // type: string
  122. // required: true
  123. // responses:
  124. // "200":
  125. // "$ref": "#/responses/Organization"
  126. if !models.HasOrgVisible(ctx.Org.Organization, ctx.User) {
  127. ctx.NotFound("HasOrgVisible", nil)
  128. return
  129. }
  130. ctx.JSON(200, convert.ToOrganization(ctx.Org.Organization))
  131. }
  132. // Edit change an organization's information
  133. func Edit(ctx *context.APIContext, form api.EditOrgOption) {
  134. // swagger:operation PATCH /orgs/{org} organization orgEdit
  135. // ---
  136. // summary: Edit an organization
  137. // consumes:
  138. // - application/json
  139. // produces:
  140. // - application/json
  141. // parameters:
  142. // - name: org
  143. // in: path
  144. // description: name of the organization to edit
  145. // type: string
  146. // required: true
  147. // - name: body
  148. // in: body
  149. // required: true
  150. // schema:
  151. // "$ref": "#/definitions/EditOrgOption"
  152. // responses:
  153. // "200":
  154. // "$ref": "#/responses/Organization"
  155. org := ctx.Org.Organization
  156. org.FullName = form.FullName
  157. org.Description = form.Description
  158. org.Website = form.Website
  159. org.Location = form.Location
  160. if form.Visibility != "" {
  161. org.Visibility = api.VisibilityModes[form.Visibility]
  162. }
  163. if err := models.UpdateUserCols(org, "full_name", "description", "website", "location", "visibility"); err != nil {
  164. ctx.Error(500, "EditOrganization", err)
  165. return
  166. }
  167. ctx.JSON(200, convert.ToOrganization(org))
  168. }
  169. //Delete an organization
  170. func Delete(ctx *context.APIContext) {
  171. // swagger:operation DELETE /orgs/{org} organization orgDelete
  172. // ---
  173. // summary: Delete an organization
  174. // produces:
  175. // - application/json
  176. // parameters:
  177. // - name: org
  178. // in: path
  179. // description: organization that is to be deleted
  180. // type: string
  181. // required: true
  182. // responses:
  183. // "204":
  184. // "$ref": "#/responses/empty"
  185. if err := models.DeleteOrganization(ctx.Org.Organization); err != nil {
  186. ctx.Error(500, "DeleteOrganization", err)
  187. return
  188. }
  189. ctx.Status(204)
  190. }