setting.go 4.8 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. log "gopkg.in/clog.v1"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/models/errors"
  10. "github.com/gogits/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/context"
  12. "github.com/gogits/gogs/modules/form"
  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_WEBHOOKS base.TplName = "org/settings/webhooks"
  20. )
  21. func Settings(ctx *context.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 *context.Context, f form.UpdateOrgSetting) {
  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(f.Name) {
  36. isExist, err := models.IsUserExist(org.ID, f.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, &f)
  43. return
  44. } else if err = models.ChangeUserName(org, f.Name); err != nil {
  45. ctx.Data["OrgName"] = true
  46. switch {
  47. case models.IsErrNameReserved(err):
  48. ctx.RenderWithErr(ctx.Tr("user.form.name_reserved"), SETTINGS_OPTIONS, &f)
  49. case models.IsErrNamePatternNotAllowed(err):
  50. ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed"), SETTINGS_OPTIONS, &f)
  51. default:
  52. ctx.Handle(500, "ChangeUserName", err)
  53. }
  54. return
  55. }
  56. // reset ctx.org.OrgLink with new name
  57. ctx.Org.OrgLink = setting.AppSubUrl + "/org/" + f.Name
  58. log.Trace("Organization name changed: %s -> %s", org.Name, f.Name)
  59. }
  60. // In case it's just a case change.
  61. org.Name = f.Name
  62. org.LowerName = strings.ToLower(f.Name)
  63. if ctx.User.IsAdmin {
  64. org.MaxRepoCreation = f.MaxRepoCreation
  65. }
  66. org.FullName = f.FullName
  67. org.Description = f.Description
  68. org.Website = f.Website
  69. org.Location = f.Location
  70. if err := models.UpdateUser(org); err != nil {
  71. ctx.Handle(500, "UpdateUser", err)
  72. return
  73. }
  74. log.Trace("Organization setting updated: %s", org.Name)
  75. ctx.Flash.Success(ctx.Tr("org.settings.update_setting_success"))
  76. ctx.Redirect(ctx.Org.OrgLink + "/settings")
  77. }
  78. func SettingsAvatar(ctx *context.Context, f form.Avatar) {
  79. f.Source = form.AVATAR_LOCAL
  80. if err := user.UpdateAvatarSetting(ctx, f, ctx.Org.Organization); err != nil {
  81. ctx.Flash.Error(err.Error())
  82. } else {
  83. ctx.Flash.Success(ctx.Tr("org.settings.update_avatar_success"))
  84. }
  85. ctx.Redirect(ctx.Org.OrgLink + "/settings")
  86. }
  87. func SettingsDeleteAvatar(ctx *context.Context) {
  88. if err := ctx.Org.Organization.DeleteAvatar(); err != nil {
  89. ctx.Flash.Error(err.Error())
  90. }
  91. ctx.Redirect(ctx.Org.OrgLink + "/settings")
  92. }
  93. func SettingsDelete(ctx *context.Context) {
  94. ctx.Data["Title"] = ctx.Tr("org.settings")
  95. ctx.Data["PageIsSettingsDelete"] = true
  96. org := ctx.Org.Organization
  97. if ctx.Req.Method == "POST" {
  98. if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  99. if errors.IsUserNotExist(err) {
  100. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_DELETE, nil)
  101. } else {
  102. ctx.Handle(500, "UserSignIn", err)
  103. }
  104. return
  105. }
  106. if err := models.DeleteOrganization(org); err != nil {
  107. if models.IsErrUserOwnRepos(err) {
  108. ctx.Flash.Error(ctx.Tr("form.org_still_own_repo"))
  109. ctx.Redirect(ctx.Org.OrgLink + "/settings/delete")
  110. } else {
  111. ctx.Handle(500, "DeleteOrganization", err)
  112. }
  113. } else {
  114. log.Trace("Organization deleted: %s", org.Name)
  115. ctx.Redirect(setting.AppSubUrl + "/")
  116. }
  117. return
  118. }
  119. ctx.HTML(200, SETTINGS_DELETE)
  120. }
  121. func Webhooks(ctx *context.Context) {
  122. ctx.Data["Title"] = ctx.Tr("org.settings")
  123. ctx.Data["PageIsSettingsHooks"] = true
  124. ctx.Data["BaseLink"] = ctx.Org.OrgLink
  125. ctx.Data["Description"] = ctx.Tr("org.settings.hooks_desc")
  126. ctx.Data["Types"] = setting.Webhook.Types
  127. ws, err := models.GetWebhooksByOrgID(ctx.Org.Organization.ID)
  128. if err != nil {
  129. ctx.Handle(500, "GetWebhooksByOrgId", err)
  130. return
  131. }
  132. ctx.Data["Webhooks"] = ws
  133. ctx.HTML(200, SETTINGS_WEBHOOKS)
  134. }
  135. func DeleteWebhook(ctx *context.Context) {
  136. if err := models.DeleteWebhookOfOrgByID(ctx.Org.Organization.ID, ctx.QueryInt64("id")); err != nil {
  137. ctx.Flash.Error("DeleteWebhookByOrgID: " + err.Error())
  138. } else {
  139. ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
  140. }
  141. ctx.JSON(200, map[string]interface{}{
  142. "redirect": ctx.Org.OrgLink + "/settings/hooks",
  143. })
  144. }