org.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2014 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. "github.com/gogits/gogs/models"
  7. "github.com/gogits/gogs/modules/auth"
  8. "github.com/gogits/gogs/modules/base"
  9. "github.com/gogits/gogs/modules/log"
  10. "github.com/gogits/gogs/modules/middleware"
  11. "github.com/gogits/gogs/modules/setting"
  12. )
  13. const (
  14. HOME base.TplName = "org/home"
  15. CREATE base.TplName = "org/create"
  16. )
  17. func Home(ctx *middleware.Context) {
  18. org := ctx.Org.Organization
  19. ctx.Data["Title"] = org.FullName
  20. repos, err := models.GetRepositories(org.Id, ctx.IsSigned && org.IsOrgMember(ctx.User.Id))
  21. if err != nil {
  22. ctx.Handle(500, "GetRepositories", err)
  23. return
  24. }
  25. ctx.Data["Repos"] = repos
  26. if err = org.GetMembers(); err != nil {
  27. ctx.Handle(500, "GetMembers", err)
  28. return
  29. }
  30. ctx.Data["Members"] = org.Members
  31. if err = org.GetTeams(); err != nil {
  32. ctx.Handle(500, "GetTeams", err)
  33. return
  34. }
  35. ctx.Data["Teams"] = org.Teams
  36. ctx.HTML(200, HOME)
  37. }
  38. func Create(ctx *middleware.Context) {
  39. ctx.Data["Title"] = ctx.Tr("new_org")
  40. ctx.HTML(200, CREATE)
  41. }
  42. func CreatePost(ctx *middleware.Context, form auth.CreateOrgForm) {
  43. ctx.Data["Title"] = ctx.Tr("new_org")
  44. if ctx.HasError() {
  45. ctx.HTML(200, CREATE)
  46. return
  47. }
  48. org := &models.User{
  49. Name: form.OrgName,
  50. IsActive: true,
  51. Type: models.ORGANIZATION,
  52. }
  53. if err := models.CreateOrganization(org, ctx.User); err != nil {
  54. ctx.Data["Err_OrgName"] = true
  55. switch {
  56. case models.IsErrUserAlreadyExist(err):
  57. ctx.RenderWithErr(ctx.Tr("form.org_name_been_taken"), CREATE, &form)
  58. case models.IsErrNameReserved(err):
  59. ctx.RenderWithErr(ctx.Tr("org.form.name_reserved", err.(models.ErrNameReserved).Name), CREATE, &form)
  60. case models.IsErrNamePatternNotAllowed(err):
  61. ctx.RenderWithErr(ctx.Tr("org.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), CREATE, &form)
  62. default:
  63. ctx.Handle(500, "CreateOrganization", err)
  64. }
  65. return
  66. }
  67. log.Trace("Organization created: %s", org.Name)
  68. ctx.Redirect(setting.AppSubUrl + "/org/" + form.OrgName + "/dashboard")
  69. }