org.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.UserOrganization,
  25. }
  26. if err := db.CreateOrganization(org, user); err != nil {
  27. if db.IsErrUserAlreadyExist(err) ||
  28. db.IsErrNameNotAllowed(err) {
  29. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  30. } else {
  31. c.Error(err, "create organization")
  32. }
  33. return
  34. }
  35. c.JSON(201, convert.ToOrganization(org))
  36. }
  37. func listUserOrgs(c *context.APIContext, u *db.User, all bool) {
  38. if err := u.GetOrganizations(all); err != nil {
  39. c.Error(err, "get organization")
  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.JSONSuccess(&apiOrgs)
  47. }
  48. func ListMyOrgs(c *context.APIContext) {
  49. listUserOrgs(c, c.User, true)
  50. }
  51. func CreateMyOrg(c *context.APIContext, apiForm api.CreateOrgOption) {
  52. CreateOrgForUser(c, apiForm, c.User)
  53. }
  54. func ListUserOrgs(c *context.APIContext) {
  55. u := user.GetUserByParams(c)
  56. if c.Written() {
  57. return
  58. }
  59. listUserOrgs(c, u, false)
  60. }
  61. func Get(c *context.APIContext) {
  62. c.JSONSuccess(convert.ToOrganization(c.Org.Organization))
  63. }
  64. func Edit(c *context.APIContext, form api.EditOrgOption) {
  65. org := c.Org.Organization
  66. if !org.IsOwnedBy(c.User.ID) {
  67. c.Status(http.StatusForbidden)
  68. return
  69. }
  70. org.FullName = form.FullName
  71. org.Description = form.Description
  72. org.Website = form.Website
  73. org.Location = form.Location
  74. if err := db.UpdateUser(org); err != nil {
  75. c.Error(err, "update user")
  76. return
  77. }
  78. c.JSONSuccess(convert.ToOrganization(org))
  79. }