org.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. Email: form.Email,
  51. IsActive: true,
  52. Type: models.ORGANIZATION,
  53. }
  54. var err error
  55. if err = models.CreateOrganization(org, ctx.User); err != nil {
  56. switch {
  57. case models.IsErrUserAlreadyExist(err):
  58. ctx.Data["Err_OrgName"] = true
  59. ctx.RenderWithErr(ctx.Tr("form.org_name_been_taken"), CREATE, &form)
  60. case models.IsErrEmailAlreadyUsed(err):
  61. ctx.Data["Err_Email"] = true
  62. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), CREATE, &form)
  63. case models.IsErrNameReserved(err):
  64. ctx.Data["Err_OrgName"] = true
  65. ctx.RenderWithErr(ctx.Tr("org.form.name_reserved", err.(models.ErrNameReserved).Name), CREATE, &form)
  66. case models.IsErrNamePatternNotAllowed(err):
  67. ctx.Data["Err_OrgName"] = true
  68. ctx.RenderWithErr(ctx.Tr("org.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), CREATE, &form)
  69. default:
  70. ctx.Handle(500, "CreateOrganization", err)
  71. }
  72. return
  73. }
  74. log.Trace("Organization created: %s", org.Name)
  75. ctx.Redirect(setting.AppSubUrl + "/org/" + form.OrgName + "/dashboard")
  76. }