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 7.5KB

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