org.go 2.2 KB

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