org.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 org, err = models.CreateOrganization(org, ctx.User); err != nil {
  56. switch err {
  57. case models.ErrUserAlreadyExist:
  58. ctx.Data["Err_OrgName"] = true
  59. ctx.RenderWithErr(ctx.Tr("form.org_name_been_taken"), CREATE, &form)
  60. case models.ErrEmailAlreadyUsed:
  61. ctx.Data["Err_Email"] = true
  62. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), CREATE, &form)
  63. case models.ErrUserNameIllegal:
  64. ctx.Data["Err_OrgName"] = true
  65. ctx.RenderWithErr(ctx.Tr("form.illegal_org_name"), CREATE, &form)
  66. default:
  67. ctx.Handle(500, "CreateUser", err)
  68. }
  69. return
  70. }
  71. log.Trace("Organization created: %s", org.Name)
  72. ctx.Redirect(setting.AppSubUrl + "/org/" + form.OrgName + "/dashboard")
  73. }