org.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. api "github.com/gogits/go-gogs-client"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/pkg/context"
  9. "github.com/gogits/gogs/routes/api/v1/convert"
  10. "github.com/gogits/gogs/routes/api/v1/user"
  11. )
  12. func CreateOrgForUser(c *context.APIContext, apiForm api.CreateOrgOption, user *models.User) {
  13. if c.Written() {
  14. return
  15. }
  16. org := &models.User{
  17. Name: apiForm.UserName,
  18. FullName: apiForm.FullName,
  19. Description: apiForm.Description,
  20. Website: apiForm.Website,
  21. Location: apiForm.Location,
  22. IsActive: true,
  23. Type: models.USER_TYPE_ORGANIZATION,
  24. }
  25. if err := models.CreateOrganization(org, user); err != nil {
  26. if models.IsErrUserAlreadyExist(err) ||
  27. models.IsErrNameReserved(err) ||
  28. models.IsErrNamePatternNotAllowed(err) {
  29. c.Error(422, "", err)
  30. } else {
  31. c.Error(500, "CreateOrganization", err)
  32. }
  33. return
  34. }
  35. c.JSON(201, convert.ToOrganization(org))
  36. }
  37. func listUserOrgs(c *context.APIContext, u *models.User, all bool) {
  38. if err := u.GetOrganizations(all); err != nil {
  39. c.Error(500, "GetOrganizations", err)
  40. return
  41. }
  42. apiOrgs := make([]*api.Organization, len(u.Orgs))
  43. for i := range u.Orgs {
  44. apiOrgs[i] = convert.ToOrganization(u.Orgs[i])
  45. }
  46. c.JSON(200, &apiOrgs)
  47. }
  48. // https://github.com/gogits/go-gogs-client/wiki/Organizations#list-your-organizations
  49. func ListMyOrgs(c *context.APIContext) {
  50. listUserOrgs(c, c.User, true)
  51. }
  52. // https://github.com/gogits/go-gogs-client/wiki/Organizations#create-your-organization
  53. func CreateMyOrg(c *context.APIContext, apiForm api.CreateOrgOption) {
  54. CreateOrgForUser(c, apiForm, c.User)
  55. }
  56. // https://github.com/gogits/go-gogs-client/wiki/Organizations#list-user-organizations
  57. func ListUserOrgs(c *context.APIContext) {
  58. u := user.GetUserByParams(c)
  59. if c.Written() {
  60. return
  61. }
  62. listUserOrgs(c, u, false)
  63. }
  64. // https://github.com/gogits/go-gogs-client/wiki/Organizations#get-an-organization
  65. func Get(c *context.APIContext) {
  66. c.JSON(200, convert.ToOrganization(c.Org.Organization))
  67. }
  68. // https://github.com/gogits/go-gogs-client/wiki/Organizations#edit-an-organization
  69. func Edit(c *context.APIContext, form api.EditOrgOption) {
  70. org := c.Org.Organization
  71. if !org.IsOwnedBy(c.User.ID) {
  72. c.Status(403)
  73. return
  74. }
  75. org.FullName = form.FullName
  76. org.Description = form.Description
  77. org.Website = form.Website
  78. org.Location = form.Location
  79. if err := models.UpdateUser(org); err != nil {
  80. c.Error(500, "UpdateUser", err)
  81. return
  82. }
  83. c.JSON(200, convert.ToOrganization(org))
  84. }