setting.go 4.7 KB

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