setting.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/Unknwon/com"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/modules/auth"
  9. "github.com/gogits/gogs/modules/base"
  10. "github.com/gogits/gogs/modules/log"
  11. "github.com/gogits/gogs/modules/middleware"
  12. "github.com/gogits/gogs/modules/setting"
  13. )
  14. const (
  15. SETTINGS_OPTIONS base.TplName = "org/settings/options"
  16. SETTINGS_DELETE base.TplName = "org/settings/delete"
  17. SETTINGS_HOOKS base.TplName = "org/settings/hooks"
  18. )
  19. func Settings(ctx *middleware.Context) {
  20. ctx.Data["Title"] = ctx.Tr("org.settings")
  21. ctx.Data["PageIsSettingsOptions"] = true
  22. ctx.HTML(200, SETTINGS_OPTIONS)
  23. }
  24. func SettingsPost(ctx *middleware.Context, form auth.UpdateOrgSettingForm) {
  25. ctx.Data["Title"] = ctx.Tr("org.settings")
  26. ctx.Data["PageIsSettingsOptions"] = true
  27. if ctx.HasError() {
  28. ctx.HTML(200, SETTINGS_OPTIONS)
  29. return
  30. }
  31. org := ctx.Org.Organization
  32. // Check if organization name has been changed.
  33. if org.Name != form.OrgUserName {
  34. isExist, err := models.IsUserExist(form.OrgUserName)
  35. if err != nil {
  36. ctx.Handle(500, "IsUserExist", err)
  37. return
  38. } else if isExist {
  39. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_OPTIONS, &form)
  40. return
  41. } else if err = models.ChangeUserName(org, form.OrgUserName); err != nil {
  42. if err == models.ErrUserNameIllegal {
  43. ctx.Flash.Error(ctx.Tr("form.illegal_username"))
  44. ctx.Redirect(setting.AppSubUrl + "/org/" + org.LowerName + "/settings")
  45. return
  46. } else {
  47. ctx.Handle(500, "ChangeUserName", err)
  48. }
  49. return
  50. }
  51. log.Trace("Organization name changed: %s -> %s", org.Name, form.OrgUserName)
  52. org.Name = form.OrgUserName
  53. }
  54. org.FullName = form.OrgFullName
  55. org.Email = form.Email
  56. org.Description = form.Description
  57. org.Website = form.Website
  58. org.Location = form.Location
  59. org.Avatar = base.EncodeMd5(form.Avatar)
  60. org.AvatarEmail = form.Avatar
  61. if err := models.UpdateUser(org); err != nil {
  62. ctx.Handle(500, "UpdateUser", err)
  63. return
  64. }
  65. log.Trace("Organization setting updated: %s", org.Name)
  66. ctx.Flash.Success(ctx.Tr("org.settings.update_setting_success"))
  67. ctx.Redirect(setting.AppSubUrl + "/org/" + org.Name + "/settings")
  68. }
  69. func SettingsDelete(ctx *middleware.Context) {
  70. ctx.Data["Title"] = ctx.Tr("org.settings")
  71. ctx.Data["PageIsSettingsDelete"] = true
  72. org := ctx.Org.Organization
  73. if ctx.Req.Method == "POST" {
  74. // TODO: validate password.
  75. if err := models.DeleteOrganization(org); err != nil {
  76. switch err {
  77. case models.ErrUserOwnRepos:
  78. ctx.Flash.Error(ctx.Tr("form.org_still_own_repo"))
  79. ctx.Redirect(setting.AppSubUrl + "/org/" + org.LowerName + "/settings/delete")
  80. default:
  81. ctx.Handle(500, "DeleteOrganization", err)
  82. }
  83. } else {
  84. log.Trace("Organization deleted: %s", org.Name)
  85. ctx.Redirect(setting.AppSubUrl + "/")
  86. }
  87. return
  88. }
  89. ctx.HTML(200, SETTINGS_DELETE)
  90. }
  91. func SettingsHooks(ctx *middleware.Context) {
  92. ctx.Data["Title"] = ctx.Tr("org.settings")
  93. ctx.Data["PageIsSettingsHooks"] = true
  94. // Delete web hook.
  95. remove := com.StrTo(ctx.Query("remove")).MustInt64()
  96. if remove > 0 {
  97. if err := models.DeleteWebhook(remove); err != nil {
  98. ctx.Handle(500, "DeleteWebhook", err)
  99. return
  100. }
  101. ctx.Flash.Success(ctx.Tr("repo.settings.remove_hook_success"))
  102. ctx.Redirect(ctx.Org.OrgLink + "/settings/hooks")
  103. return
  104. }
  105. ws, err := models.GetWebhooksByOrgId(ctx.Org.Organization.Id)
  106. if err != nil {
  107. ctx.Handle(500, "GetWebhooksByOrgId", err)
  108. return
  109. }
  110. ctx.Data["Webhooks"] = ws
  111. ctx.HTML(200, SETTINGS_HOOKS)
  112. }