setting.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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(org.Id, form.OrgUserName)
  35. if err != nil {
  36. ctx.Handle(500, "IsUserExist", err)
  37. return
  38. } else if isExist {
  39. ctx.Data["Err_UserName"] = true
  40. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_OPTIONS, &form)
  41. return
  42. } else if err = models.ChangeUserName(org, form.OrgUserName); err != nil {
  43. if err == models.ErrUserNameIllegal {
  44. ctx.Data["Err_UserName"] = true
  45. ctx.RenderWithErr(ctx.Tr("form.illegal_username"), SETTINGS_OPTIONS, &form)
  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. if models.IsErrEmailAlreadyUsed(err) {
  63. ctx.Data["Err_Email"] = true
  64. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SETTINGS_OPTIONS, &form)
  65. } else {
  66. ctx.Handle(500, "UpdateUser", err)
  67. }
  68. return
  69. }
  70. log.Trace("Organization setting updated: %s", org.Name)
  71. ctx.Flash.Success(ctx.Tr("org.settings.update_setting_success"))
  72. ctx.Redirect(setting.AppSubUrl + "/org/" + org.Name + "/settings")
  73. }
  74. func SettingsDelete(ctx *middleware.Context) {
  75. ctx.Data["Title"] = ctx.Tr("org.settings")
  76. ctx.Data["PageIsSettingsDelete"] = true
  77. org := ctx.Org.Organization
  78. if ctx.Req.Method == "POST" {
  79. // FIXME: validate password.
  80. if err := models.DeleteOrganization(org); err != nil {
  81. if models.IsErrUserOwnRepos(err) {
  82. ctx.Flash.Error(ctx.Tr("form.org_still_own_repo"))
  83. ctx.Redirect(setting.AppSubUrl + "/org/" + org.LowerName + "/settings/delete")
  84. } else {
  85. ctx.Handle(500, "DeleteOrganization", err)
  86. }
  87. } else {
  88. log.Trace("Organization deleted: %s", org.Name)
  89. ctx.Redirect(setting.AppSubUrl + "/")
  90. }
  91. return
  92. }
  93. ctx.HTML(200, SETTINGS_DELETE)
  94. }
  95. func SettingsHooks(ctx *middleware.Context) {
  96. ctx.Data["Title"] = ctx.Tr("org.settings")
  97. ctx.Data["PageIsSettingsHooks"] = true
  98. // Delete web hook.
  99. remove := com.StrTo(ctx.Query("remove")).MustInt64()
  100. if remove > 0 {
  101. if err := models.DeleteWebhook(remove); err != nil {
  102. ctx.Handle(500, "DeleteWebhook", err)
  103. return
  104. }
  105. ctx.Flash.Success(ctx.Tr("repo.settings.remove_hook_success"))
  106. ctx.Redirect(ctx.Org.OrgLink + "/settings/hooks")
  107. return
  108. }
  109. ws, err := models.GetWebhooksByOrgId(ctx.Org.Organization.Id)
  110. if err != nil {
  111. ctx.Handle(500, "GetWebhooksByOrgId", err)
  112. return
  113. }
  114. ctx.Data["Webhooks"] = ws
  115. ctx.HTML(200, SETTINGS_HOOKS)
  116. }