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.

org.go 4.9KB

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