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. log "gopkg.in/clog.v1"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/models/errors"
  10. "github.com/gogits/gogs/pkg/context"
  11. "github.com/gogits/gogs/pkg/form"
  12. "github.com/gogits/gogs/pkg/setting"
  13. "github.com/gogits/gogs/routers/user"
  14. )
  15. const (
  16. SETTINGS_OPTIONS = "org/settings/options"
  17. SETTINGS_DELETE = "org/settings/delete"
  18. SETTINGS_WEBHOOKS = "org/settings/webhooks"
  19. )
  20. func Settings(ctx *context.Context) {
  21. ctx.Data["Title"] = ctx.Tr("org.settings")
  22. ctx.Data["PageIsSettingsOptions"] = true
  23. ctx.HTML(200, SETTINGS_OPTIONS)
  24. }
  25. func SettingsPost(ctx *context.Context, f form.UpdateOrgSetting) {
  26. ctx.Data["Title"] = ctx.Tr("org.settings")
  27. ctx.Data["PageIsSettingsOptions"] = true
  28. if ctx.HasError() {
  29. ctx.HTML(200, SETTINGS_OPTIONS)
  30. return
  31. }
  32. org := ctx.Org.Organization
  33. // Check if organization name has been changed.
  34. if org.LowerName != strings.ToLower(f.Name) {
  35. isExist, err := models.IsUserExist(org.ID, f.Name)
  36. if err != nil {
  37. ctx.Handle(500, "IsUserExist", err)
  38. return
  39. } else if isExist {
  40. ctx.Data["OrgName"] = true
  41. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_OPTIONS, &f)
  42. return
  43. } else if err = models.ChangeUserName(org, f.Name); err != nil {
  44. ctx.Data["OrgName"] = true
  45. switch {
  46. case models.IsErrNameReserved(err):
  47. ctx.RenderWithErr(ctx.Tr("user.form.name_reserved"), SETTINGS_OPTIONS, &f)
  48. case models.IsErrNamePatternNotAllowed(err):
  49. ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed"), SETTINGS_OPTIONS, &f)
  50. default:
  51. ctx.Handle(500, "ChangeUserName", err)
  52. }
  53. return
  54. }
  55. // reset ctx.org.OrgLink with new name
  56. ctx.Org.OrgLink = setting.AppSubURL + "/org/" + f.Name
  57. log.Trace("Organization name changed: %s -> %s", org.Name, f.Name)
  58. }
  59. // In case it's just a case change.
  60. org.Name = f.Name
  61. org.LowerName = strings.ToLower(f.Name)
  62. if ctx.User.IsAdmin {
  63. org.MaxRepoCreation = f.MaxRepoCreation
  64. }
  65. org.FullName = f.FullName
  66. org.Description = f.Description
  67. org.Website = f.Website
  68. org.Location = f.Location
  69. if err := models.UpdateUser(org); err != nil {
  70. ctx.Handle(500, "UpdateUser", err)
  71. return
  72. }
  73. log.Trace("Organization setting updated: %s", org.Name)
  74. ctx.Flash.Success(ctx.Tr("org.settings.update_setting_success"))
  75. ctx.Redirect(ctx.Org.OrgLink + "/settings")
  76. }
  77. func SettingsAvatar(ctx *context.Context, f form.Avatar) {
  78. f.Source = form.AVATAR_LOCAL
  79. if err := user.UpdateAvatarSetting(ctx, f, ctx.Org.Organization); err != nil {
  80. ctx.Flash.Error(err.Error())
  81. } else {
  82. ctx.Flash.Success(ctx.Tr("org.settings.update_avatar_success"))
  83. }
  84. ctx.Redirect(ctx.Org.OrgLink + "/settings")
  85. }
  86. func SettingsDeleteAvatar(ctx *context.Context) {
  87. if err := ctx.Org.Organization.DeleteAvatar(); err != nil {
  88. ctx.Flash.Error(err.Error())
  89. }
  90. ctx.Redirect(ctx.Org.OrgLink + "/settings")
  91. }
  92. func SettingsDelete(ctx *context.Context) {
  93. ctx.Data["Title"] = ctx.Tr("org.settings")
  94. ctx.Data["PageIsSettingsDelete"] = true
  95. org := ctx.Org.Organization
  96. if ctx.Req.Method == "POST" {
  97. if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  98. if errors.IsUserNotExist(err) {
  99. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_DELETE, nil)
  100. } else {
  101. ctx.Handle(500, "UserSignIn", err)
  102. }
  103. return
  104. }
  105. if err := models.DeleteOrganization(org); err != nil {
  106. if models.IsErrUserOwnRepos(err) {
  107. ctx.Flash.Error(ctx.Tr("form.org_still_own_repo"))
  108. ctx.Redirect(ctx.Org.OrgLink + "/settings/delete")
  109. } else {
  110. ctx.Handle(500, "DeleteOrganization", err)
  111. }
  112. } else {
  113. log.Trace("Organization deleted: %s", org.Name)
  114. ctx.Redirect(setting.AppSubURL + "/")
  115. }
  116. return
  117. }
  118. ctx.HTML(200, SETTINGS_DELETE)
  119. }
  120. func Webhooks(ctx *context.Context) {
  121. ctx.Data["Title"] = ctx.Tr("org.settings")
  122. ctx.Data["PageIsSettingsHooks"] = true
  123. ctx.Data["BaseLink"] = ctx.Org.OrgLink
  124. ctx.Data["Description"] = ctx.Tr("org.settings.hooks_desc")
  125. ctx.Data["Types"] = setting.Webhook.Types
  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_WEBHOOKS)
  133. }
  134. func DeleteWebhook(ctx *context.Context) {
  135. if err := models.DeleteWebhookOfOrgByID(ctx.Org.Organization.ID, ctx.QueryInt64("id")); err != nil {
  136. ctx.Flash.Error("DeleteWebhookByOrgID: " + 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. }