org.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, 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. // CreateOrgForUser api for create organization
  15. func CreateOrgForUser(c *context.APIContext, apiForm api.CreateOrgOption, user *models.User) {
  16. if c.Written() {
  17. return
  18. }
  19. org := &models.User{
  20. Name: apiForm.UserName,
  21. FullName: apiForm.FullName,
  22. Description: apiForm.Description,
  23. Website: apiForm.Website,
  24. Location: apiForm.Location,
  25. IsActive: true,
  26. Type: models.UserTypeOrganization,
  27. }
  28. if err := models.CreateOrganization(org, user); err != nil {
  29. if models.IsErrUserAlreadyExist(err) ||
  30. models.IsErrNameReserved(err) ||
  31. models.IsErrNamePatternNotAllowed(err) {
  32. c.Error(422, "", err)
  33. } else {
  34. c.Error(500, "CreateOrganization", err)
  35. }
  36. return
  37. }
  38. c.JSON(201, convert.ToOrganization(org))
  39. }
  40. func listUserOrgs(c *context.APIContext, u *models.User, all bool) {
  41. if err := u.GetOrganizations(all); err != nil {
  42. c.Error(500, "GetOrganizations", err)
  43. return
  44. }
  45. apiOrgs := make([]*api.Organization, len(u.Orgs))
  46. for i := range u.Orgs {
  47. apiOrgs[i] = convert.ToOrganization(u.Orgs[i])
  48. }
  49. c.JSON(200, &apiOrgs)
  50. }
  51. // ListMyOrgs list all my orgs
  52. func ListMyOrgs(c *context.APIContext) {
  53. listUserOrgs(c, c.User, true)
  54. }
  55. // CreateMyOrg list user's orgs
  56. func CreateMyOrg(c *context.APIContext, apiForm api.CreateOrgOption) {
  57. CreateOrgForUser(c, apiForm, c.User)
  58. }
  59. // ListUserOrgs list user's orgs
  60. func ListUserOrgs(c *context.APIContext) {
  61. u := user.GetUserByParams(c)
  62. if c.Written() {
  63. return
  64. }
  65. listUserOrgs(c, u, false)
  66. }
  67. // Get get an organization
  68. func Get(c *context.APIContext) {
  69. c.JSON(200, convert.ToOrganization(c.Org.Organization))
  70. }
  71. // Edit change an organization's information
  72. func Edit(c *context.APIContext, form api.EditOrgOption) {
  73. org := c.Org.Organization
  74. if !org.IsOwnedBy(c.User.ID) {
  75. c.Status(403)
  76. return
  77. }
  78. org.FullName = form.FullName
  79. org.Description = form.Description
  80. org.Website = form.Website
  81. org.Location = form.Location
  82. if err := models.UpdateUser(org); err != nil {
  83. c.Error(500, "UpdateUser", err)
  84. return
  85. }
  86. c.JSON(200, convert.ToOrganization(org))
  87. }