org.go 2.2 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. "gogs.io/gogs/internal/context"
  9. "gogs.io/gogs/internal/db"
  10. "gogs.io/gogs/internal/route/api/v1/convert"
  11. "gogs.io/gogs/internal/route/api/v1/user"
  12. )
  13. func CreateOrgForUser(c *context.APIContext, apiForm api.CreateOrgOption, user *db.User) {
  14. if c.Written() {
  15. return
  16. }
  17. org := &db.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: db.USER_TYPE_ORGANIZATION,
  25. }
  26. if err := db.CreateOrganization(org, user); err != nil {
  27. if db.IsErrUserAlreadyExist(err) ||
  28. db.IsErrNameReserved(err) ||
  29. db.IsErrNamePatternNotAllowed(err) {
  30. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  31. } else {
  32. c.Error(err, "create organization")
  33. }
  34. return
  35. }
  36. c.JSON(201, convert.ToOrganization(org))
  37. }
  38. func listUserOrgs(c *context.APIContext, u *db.User, all bool) {
  39. if err := u.GetOrganizations(all); err != nil {
  40. c.Error(err, "get organization")
  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 := db.UpdateUser(org); err != nil {
  76. c.Error(err, "update user")
  77. return
  78. }
  79. c.JSONSuccess(convert.ToOrganization(org))
  80. }