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 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. // Create api for create organization
  60. func Create(ctx *context.APIContext, form api.CreateOrgOption) {
  61. // swagger:operation POST /orgs organization orgCreate
  62. // ---
  63. // summary: Create an organization
  64. // consumes:
  65. // - application/json
  66. // produces:
  67. // - application/json
  68. // parameters:
  69. // - name: organization
  70. // in: body
  71. // required: true
  72. // schema: { "$ref": "#/definitions/CreateOrgOption" }
  73. // responses:
  74. // "201":
  75. // "$ref": "#/responses/Organization"
  76. // "403":
  77. // "$ref": "#/responses/forbidden"
  78. // "422":
  79. // "$ref": "#/responses/validationError"
  80. if !ctx.User.CanCreateOrganization() {
  81. ctx.Error(http.StatusForbidden, "Create organization not allowed", nil)
  82. return
  83. }
  84. visibility := api.VisibleTypePublic
  85. if form.Visibility != "" {
  86. visibility = api.VisibilityModes[form.Visibility]
  87. }
  88. org := &models.User{
  89. Name: form.UserName,
  90. FullName: form.FullName,
  91. Description: form.Description,
  92. Website: form.Website,
  93. Location: form.Location,
  94. IsActive: true,
  95. Type: models.UserTypeOrganization,
  96. Visibility: visibility,
  97. RepoAdminChangeTeamAccess: form.RepoAdminChangeTeamAccess,
  98. }
  99. if err := models.CreateOrganization(org, ctx.User); err != nil {
  100. if models.IsErrUserAlreadyExist(err) ||
  101. models.IsErrNameReserved(err) ||
  102. models.IsErrNamePatternNotAllowed(err) {
  103. ctx.Error(http.StatusUnprocessableEntity, "", err)
  104. } else {
  105. ctx.Error(http.StatusInternalServerError, "CreateOrganization", err)
  106. }
  107. return
  108. }
  109. ctx.JSON(http.StatusCreated, convert.ToOrganization(org))
  110. }
  111. // Get get an organization
  112. func Get(ctx *context.APIContext) {
  113. // swagger:operation GET /orgs/{org} organization orgGet
  114. // ---
  115. // summary: Get an organization
  116. // produces:
  117. // - application/json
  118. // parameters:
  119. // - name: org
  120. // in: path
  121. // description: name of the organization to get
  122. // type: string
  123. // required: true
  124. // responses:
  125. // "200":
  126. // "$ref": "#/responses/Organization"
  127. if !models.HasOrgVisible(ctx.Org.Organization, ctx.User) {
  128. ctx.NotFound("HasOrgVisible", nil)
  129. return
  130. }
  131. ctx.JSON(http.StatusOK, convert.ToOrganization(ctx.Org.Organization))
  132. }
  133. // Edit change an organization's information
  134. func Edit(ctx *context.APIContext, form api.EditOrgOption) {
  135. // swagger:operation PATCH /orgs/{org} organization orgEdit
  136. // ---
  137. // summary: Edit an organization
  138. // consumes:
  139. // - application/json
  140. // produces:
  141. // - application/json
  142. // parameters:
  143. // - name: org
  144. // in: path
  145. // description: name of the organization to edit
  146. // type: string
  147. // required: true
  148. // - name: body
  149. // in: body
  150. // required: true
  151. // schema:
  152. // "$ref": "#/definitions/EditOrgOption"
  153. // responses:
  154. // "200":
  155. // "$ref": "#/responses/Organization"
  156. org := ctx.Org.Organization
  157. org.FullName = form.FullName
  158. org.Description = form.Description
  159. org.Website = form.Website
  160. org.Location = form.Location
  161. if form.Visibility != "" {
  162. org.Visibility = api.VisibilityModes[form.Visibility]
  163. }
  164. if err := models.UpdateUserCols(org, "full_name", "description", "website", "location", "visibility"); err != nil {
  165. ctx.Error(http.StatusInternalServerError, "EditOrganization", err)
  166. return
  167. }
  168. ctx.JSON(http.StatusOK, convert.ToOrganization(org))
  169. }
  170. //Delete an organization
  171. func Delete(ctx *context.APIContext) {
  172. // swagger:operation DELETE /orgs/{org} organization orgDelete
  173. // ---
  174. // summary: Delete an organization
  175. // produces:
  176. // - application/json
  177. // parameters:
  178. // - name: org
  179. // in: path
  180. // description: organization that is to be deleted
  181. // type: string
  182. // required: true
  183. // responses:
  184. // "204":
  185. // "$ref": "#/responses/empty"
  186. if err := models.DeleteOrganization(ctx.Org.Organization); err != nil {
  187. ctx.Error(http.StatusInternalServerError, "DeleteOrganization", err)
  188. return
  189. }
  190. ctx.Status(http.StatusNoContent)
  191. }