setting.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. )
  18. func Settings(c *context.Context) {
  19. c.Title("org.settings")
  20. c.Data["PageIsSettingsOptions"] = true
  21. c.Success(SETTINGS_OPTIONS)
  22. }
  23. func SettingsPost(c *context.Context, f form.UpdateOrgSetting) {
  24. c.Title("org.settings")
  25. c.Data["PageIsSettingsOptions"] = true
  26. if c.HasError() {
  27. c.Success(SETTINGS_OPTIONS)
  28. return
  29. }
  30. org := c.Org.Organization
  31. // Check if organization name has been changed.
  32. if org.LowerName != strings.ToLower(f.Name) {
  33. isExist, err := db.IsUserExist(org.ID, f.Name)
  34. if err != nil {
  35. c.Error(err, "check if user exists")
  36. return
  37. } else if isExist {
  38. c.Data["OrgName"] = true
  39. c.RenderWithErr(c.Tr("form.username_been_taken"), SETTINGS_OPTIONS, &f)
  40. return
  41. } else if err = db.ChangeUserName(org, f.Name); err != nil {
  42. c.Data["OrgName"] = true
  43. switch {
  44. case db.IsErrNameReserved(err):
  45. c.RenderWithErr(c.Tr("user.form.name_reserved"), SETTINGS_OPTIONS, &f)
  46. case db.IsErrNamePatternNotAllowed(err):
  47. c.RenderWithErr(c.Tr("user.form.name_pattern_not_allowed"), SETTINGS_OPTIONS, &f)
  48. default:
  49. c.Error(err, "change user name")
  50. }
  51. return
  52. }
  53. // reset c.org.OrgLink with new name
  54. c.Org.OrgLink = conf.Server.Subpath + "/org/" + f.Name
  55. log.Trace("Organization name changed: %s -> %s", org.Name, f.Name)
  56. }
  57. // In case it's just a case change.
  58. org.Name = f.Name
  59. org.LowerName = strings.ToLower(f.Name)
  60. if c.User.IsAdmin {
  61. org.MaxRepoCreation = f.MaxRepoCreation
  62. }
  63. org.FullName = f.FullName
  64. org.Description = f.Description
  65. org.Website = f.Website
  66. org.Location = f.Location
  67. if err := db.UpdateUser(org); err != nil {
  68. c.Error(err, "update user")
  69. return
  70. }
  71. log.Trace("Organization setting updated: %s", org.Name)
  72. c.Flash.Success(c.Tr("org.settings.update_setting_success"))
  73. c.Redirect(c.Org.OrgLink + "/settings")
  74. }
  75. func SettingsAvatar(c *context.Context, f form.Avatar) {
  76. f.Source = form.AVATAR_LOCAL
  77. if err := user.UpdateAvatarSetting(c, f, c.Org.Organization); err != nil {
  78. c.Flash.Error(err.Error())
  79. } else {
  80. c.Flash.Success(c.Tr("org.settings.update_avatar_success"))
  81. }
  82. c.Redirect(c.Org.OrgLink + "/settings")
  83. }
  84. func SettingsDeleteAvatar(c *context.Context) {
  85. if err := c.Org.Organization.DeleteAvatar(); err != nil {
  86. c.Flash.Error(err.Error())
  87. }
  88. c.Redirect(c.Org.OrgLink + "/settings")
  89. }
  90. func SettingsDelete(c *context.Context) {
  91. c.Title("org.settings")
  92. c.PageIs("SettingsDelete")
  93. org := c.Org.Organization
  94. if c.Req.Method == "POST" {
  95. if _, err := db.UserLogin(c.User.Name, c.Query("password"), c.User.LoginSource); err != nil {
  96. if db.IsErrUserNotExist(err) {
  97. c.RenderWithErr(c.Tr("form.enterred_invalid_password"), SETTINGS_DELETE, nil)
  98. } else {
  99. c.Error(err, "authenticate user")
  100. }
  101. return
  102. }
  103. if err := db.DeleteOrganization(org); err != nil {
  104. if db.IsErrUserOwnRepos(err) {
  105. c.Flash.Error(c.Tr("form.org_still_own_repo"))
  106. c.Redirect(c.Org.OrgLink + "/settings/delete")
  107. } else {
  108. c.Error(err, "delete organization")
  109. }
  110. } else {
  111. log.Trace("Organization deleted: %s", org.Name)
  112. c.Redirect(conf.Server.Subpath + "/")
  113. }
  114. return
  115. }
  116. c.Success(SETTINGS_DELETE)
  117. }