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