setting.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. if ctx.User.IsAdmin {
  59. org.MaxRepoCreation = form.MaxRepoCreation
  60. }
  61. org.FullName = form.FullName
  62. org.Description = form.Description
  63. org.Website = form.Website
  64. org.Location = form.Location
  65. if err := models.UpdateUser(org); err != nil {
  66. ctx.Handle(500, "UpdateUser", err)
  67. return
  68. }
  69. log.Trace("Organization setting updated: %s", org.Name)
  70. ctx.Flash.Success(ctx.Tr("org.settings.update_setting_success"))
  71. ctx.Redirect(ctx.Org.OrgLink + "/settings")
  72. }
  73. func SettingsAvatar(ctx *middleware.Context, form auth.UploadAvatarForm) {
  74. form.Enable = true
  75. if err := user.UpdateAvatarSetting(ctx, form, ctx.Org.Organization); err != nil {
  76. ctx.Flash.Error(err.Error())
  77. } else {
  78. ctx.Flash.Success(ctx.Tr("org.settings.update_avatar_success"))
  79. }
  80. ctx.Redirect(ctx.Org.OrgLink + "/settings")
  81. }
  82. func SettingsDelete(ctx *middleware.Context) {
  83. ctx.Data["Title"] = ctx.Tr("org.settings")
  84. ctx.Data["PageIsSettingsDelete"] = true
  85. org := ctx.Org.Organization
  86. if ctx.Req.Method == "POST" {
  87. if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  88. if models.IsErrUserNotExist(err) {
  89. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_DELETE, nil)
  90. } else {
  91. ctx.Handle(500, "UserSignIn", err)
  92. }
  93. return
  94. }
  95. if err := models.DeleteOrganization(org); err != nil {
  96. if models.IsErrUserOwnRepos(err) {
  97. ctx.Flash.Error(ctx.Tr("form.org_still_own_repo"))
  98. ctx.Redirect(ctx.Org.OrgLink + "/settings/delete")
  99. } else {
  100. ctx.Handle(500, "DeleteOrganization", err)
  101. }
  102. } else {
  103. log.Trace("Organization deleted: %s", org.Name)
  104. ctx.Redirect(setting.AppSubUrl + "/")
  105. }
  106. return
  107. }
  108. ctx.HTML(200, SETTINGS_DELETE)
  109. }
  110. func Webhooks(ctx *middleware.Context) {
  111. ctx.Data["Title"] = ctx.Tr("org.settings")
  112. ctx.Data["PageIsSettingsHooks"] = true
  113. ctx.Data["BaseLink"] = ctx.Org.OrgLink
  114. ctx.Data["Description"] = ctx.Tr("org.settings.hooks_desc")
  115. // Delete web hook.
  116. remove := com.StrTo(ctx.Query("remove")).MustInt64()
  117. if remove > 0 {
  118. if err := models.DeleteWebhook(remove); err != nil {
  119. ctx.Handle(500, "DeleteWebhook", err)
  120. return
  121. }
  122. ctx.Flash.Success(ctx.Tr("repo.settings.remove_hook_success"))
  123. ctx.Redirect(ctx.Org.OrgLink + "/settings/hooks")
  124. return
  125. }
  126. ws, err := models.GetWebhooksByOrgId(ctx.Org.Organization.Id)
  127. if err != nil {
  128. ctx.Handle(500, "GetWebhooksByOrgId", err)
  129. return
  130. }
  131. ctx.Data["Webhooks"] = ws
  132. ctx.HTML(200, SETTINGS_HOOKS)
  133. }
  134. func DeleteWebhook(ctx *middleware.Context) {
  135. if err := models.DeleteWebhook(ctx.QueryInt64("id")); err != nil {
  136. ctx.Flash.Error("DeleteWebhook: " + err.Error())
  137. } else {
  138. ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
  139. }
  140. ctx.JSON(200, map[string]interface{}{
  141. "redirect": ctx.Org.OrgLink + "/settings/hooks",
  142. })
  143. }