setting.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. "strings"
  7. "github.com/Unknwon/com"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/auth"
  10. "github.com/gogits/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/log"
  12. "github.com/gogits/gogs/modules/middleware"
  13. "github.com/gogits/gogs/modules/setting"
  14. "github.com/gogits/gogs/routers/user"
  15. )
  16. const (
  17. SETTINGS_OPTIONS base.TplName = "org/settings/options"
  18. SETTINGS_DELETE base.TplName = "org/settings/delete"
  19. SETTINGS_HOOKS base.TplName = "org/settings/hooks"
  20. )
  21. func Settings(ctx *middleware.Context) {
  22. ctx.Data["Title"] = ctx.Tr("org.settings")
  23. ctx.Data["PageIsSettingsOptions"] = true
  24. ctx.HTML(200, SETTINGS_OPTIONS)
  25. }
  26. func SettingsPost(ctx *middleware.Context, form auth.UpdateOrgSettingForm) {
  27. ctx.Data["Title"] = ctx.Tr("org.settings")
  28. ctx.Data["PageIsSettingsOptions"] = true
  29. if ctx.HasError() {
  30. ctx.HTML(200, SETTINGS_OPTIONS)
  31. return
  32. }
  33. org := ctx.Org.Organization
  34. // Check if organization name has been changed.
  35. if org.LowerName != strings.ToLower(form.Name) {
  36. isExist, err := models.IsUserExist(org.Id, form.Name)
  37. if err != nil {
  38. ctx.Handle(500, "IsUserExist", err)
  39. return
  40. } else if isExist {
  41. ctx.Data["OrgName"] = true
  42. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_OPTIONS, &form)
  43. return
  44. } else if err = models.ChangeUserName(org, form.Name); err != nil {
  45. if err == models.ErrUserNameIllegal {
  46. ctx.Data["OrgName"] = true
  47. ctx.RenderWithErr(ctx.Tr("form.illegal_username"), SETTINGS_OPTIONS, &form)
  48. } else {
  49. ctx.Handle(500, "ChangeUserName", err)
  50. }
  51. return
  52. }
  53. log.Trace("Organization name changed: %s -> %s", org.Name, form.Name)
  54. }
  55. // In case it's just a case change.
  56. org.Name = form.Name
  57. org.LowerName = strings.ToLower(form.Name)
  58. org.FullName = form.FullName
  59. org.Description = form.Description
  60. org.Website = form.Website
  61. org.Location = form.Location
  62. if err := models.UpdateUser(org); err != nil {
  63. ctx.Handle(500, "UpdateUser", err)
  64. return
  65. }
  66. log.Trace("Organization setting updated: %s", org.Name)
  67. ctx.Flash.Success(ctx.Tr("org.settings.update_setting_success"))
  68. ctx.Redirect(ctx.Org.OrgLink + "/settings")
  69. }
  70. func SettingsAvatar(ctx *middleware.Context, form auth.UploadAvatarForm) {
  71. form.Enable = true
  72. if err := user.UpdateAvatarSetting(ctx, form, ctx.Org.Organization); err != nil {
  73. ctx.Flash.Error(err.Error())
  74. } else {
  75. ctx.Flash.Success(ctx.Tr("org.settings.update_avatar_success"))
  76. }
  77. ctx.Redirect(ctx.Org.OrgLink + "/settings")
  78. }
  79. func SettingsDelete(ctx *middleware.Context) {
  80. ctx.Data["Title"] = ctx.Tr("org.settings")
  81. ctx.Data["PageIsSettingsDelete"] = true
  82. org := ctx.Org.Organization
  83. if ctx.Req.Method == "POST" {
  84. if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  85. if models.IsErrUserNotExist(err) {
  86. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_DELETE, nil)
  87. } else {
  88. ctx.Handle(500, "UserSignIn", err)
  89. }
  90. return
  91. }
  92. if err := models.DeleteOrganization(org); err != nil {
  93. if models.IsErrUserOwnRepos(err) {
  94. ctx.Flash.Error(ctx.Tr("form.org_still_own_repo"))
  95. ctx.Redirect(ctx.Org.OrgLink + "/settings/delete")
  96. } else {
  97. ctx.Handle(500, "DeleteOrganization", err)
  98. }
  99. } else {
  100. log.Trace("Organization deleted: %s", org.Name)
  101. ctx.Redirect(setting.AppSubUrl + "/")
  102. }
  103. return
  104. }
  105. ctx.HTML(200, SETTINGS_DELETE)
  106. }
  107. func Webhooks(ctx *middleware.Context) {
  108. ctx.Data["Title"] = ctx.Tr("org.settings")
  109. ctx.Data["PageIsSettingsHooks"] = true
  110. ctx.Data["BaseLink"] = ctx.Org.OrgLink
  111. ctx.Data["Description"] = ctx.Tr("org.settings.hooks_desc")
  112. // Delete web hook.
  113. remove := com.StrTo(ctx.Query("remove")).MustInt64()
  114. if remove > 0 {
  115. if err := models.DeleteWebhook(remove); err != nil {
  116. ctx.Handle(500, "DeleteWebhook", err)
  117. return
  118. }
  119. ctx.Flash.Success(ctx.Tr("repo.settings.remove_hook_success"))
  120. ctx.Redirect(ctx.Org.OrgLink + "/settings/hooks")
  121. return
  122. }
  123. ws, err := models.GetWebhooksByOrgId(ctx.Org.Organization.Id)
  124. if err != nil {
  125. ctx.Handle(500, "GetWebhooksByOrgId", err)
  126. return
  127. }
  128. ctx.Data["Webhooks"] = ws
  129. ctx.HTML(200, SETTINGS_HOOKS)
  130. }
  131. func DeleteWebhook(ctx *middleware.Context) {
  132. if err := models.DeleteWebhook(ctx.QueryInt64("id")); err != nil {
  133. ctx.Flash.Error("DeleteWebhook: " + err.Error())
  134. } else {
  135. ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
  136. }
  137. ctx.JSON(200, map[string]interface{}{
  138. "redirect": ctx.Org.OrgLink + "/settings/hooks",
  139. })
  140. }