org.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package org
  5. import (
  6. "net/http"
  7. api "github.com/gogs/go-gogs-client"
  8. "github.com/gogs/gogs/models"
  9. "github.com/gogs/gogs/pkg/context"
  10. "github.com/gogs/gogs/routes/api/v1/convert"
  11. "github.com/gogs/gogs/routes/api/v1/user"
  12. )
  13. func CreateOrgForUser(c *context.APIContext, apiForm api.CreateOrgOption, user *models.User) {
  14. if c.Written() {
  15. return
  16. }
  17. org := &models.User{
  18. Name: apiForm.UserName,
  19. FullName: apiForm.FullName,
  20. Description: apiForm.Description,
  21. Website: apiForm.Website,
  22. Location: apiForm.Location,
  23. IsActive: true,
  24. Type: models.USER_TYPE_ORGANIZATION,
  25. }
  26. if err := models.CreateOrganization(org, user); err != nil {
  27. if models.IsErrUserAlreadyExist(err) ||
  28. models.IsErrNameReserved(err) ||
  29. models.IsErrNamePatternNotAllowed(err) {
  30. c.Error(http.StatusUnprocessableEntity, "", err)
  31. } else {
  32. c.ServerError("CreateOrganization", err)
  33. }
  34. return
  35. }
  36. c.JSON(201, convert.ToOrganization(org))
  37. }
  38. func listUserOrgs(c *context.APIContext, u *models.User, all bool) {
  39. if err := u.GetOrganizations(all); err != nil {
  40. c.ServerError("GetOrganizations", err)
  41. return
  42. }
  43. apiOrgs := make([]*api.Organization, len(u.Orgs))
  44. for i := range u.Orgs {
  45. apiOrgs[i] = convert.ToOrganization(u.Orgs[i])
  46. }
  47. c.JSONSuccess(&apiOrgs)
  48. }
  49. func ListMyOrgs(c *context.APIContext) {
  50. listUserOrgs(c, c.User, true)
  51. }
  52. func CreateMyOrg(c *context.APIContext, apiForm api.CreateOrgOption) {
  53. CreateOrgForUser(c, apiForm, c.User)
  54. }
  55. func ListUserOrgs(c *context.APIContext) {
  56. u := user.GetUserByParams(c)
  57. if c.Written() {
  58. return
  59. }
  60. listUserOrgs(c, u, false)
  61. }
  62. func Get(c *context.APIContext) {
  63. c.JSONSuccess(convert.ToOrganization(c.Org.Organization))
  64. }
  65. func Edit(c *context.APIContext, form api.EditOrgOption) {
  66. org := c.Org.Organization
  67. if !org.IsOwnedBy(c.User.ID) {
  68. c.Status(http.StatusForbidden)
  69. return
  70. }
  71. org.FullName = form.FullName
  72. org.Description = form.Description
  73. org.Website = form.Website
  74. org.Location = form.Location
  75. if err := models.UpdateUser(org); err != nil {
  76. c.ServerError("UpdateUser", err)
  77. return
  78. }
  79. c.JSONSuccess(convert.ToOrganization(org))
  80. }