setting.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.IsErrNameNotAllowed(err):
  45. c.RenderWithErr(c.Tr("user.form.name_not_allowed", err.(db.ErrNameNotAllowed).Value()), SETTINGS_OPTIONS, &f)
  46. default:
  47. c.Error(err, "change user name")
  48. }
  49. return
  50. }
  51. // reset c.org.OrgLink with new name
  52. c.Org.OrgLink = conf.Server.Subpath + "/org/" + f.Name
  53. log.Trace("Organization name changed: %s -> %s", org.Name, f.Name)
  54. }
  55. // In case it's just a case change.
  56. org.Name = f.Name
  57. org.LowerName = strings.ToLower(f.Name)
  58. if c.User.IsAdmin {
  59. org.MaxRepoCreation = f.MaxRepoCreation
  60. }
  61. org.FullName = f.FullName
  62. org.Description = f.Description
  63. org.Website = f.Website
  64. org.Location = f.Location
  65. if err := db.UpdateUser(org); err != nil {
  66. c.Error(err, "update user")
  67. return
  68. }
  69. log.Trace("Organization setting updated: %s", org.Name)
  70. c.Flash.Success(c.Tr("org.settings.update_setting_success"))
  71. c.Redirect(c.Org.OrgLink + "/settings")
  72. }
  73. func SettingsAvatar(c *context.Context, f form.Avatar) {
  74. f.Source = form.AVATAR_LOCAL
  75. if err := user.UpdateAvatarSetting(c, f, c.Org.Organization); err != nil {
  76. c.Flash.Error(err.Error())
  77. } else {
  78. c.Flash.Success(c.Tr("org.settings.update_avatar_success"))
  79. }
  80. c.Redirect(c.Org.OrgLink + "/settings")
  81. }
  82. func SettingsDeleteAvatar(c *context.Context) {
  83. if err := c.Org.Organization.DeleteAvatar(); err != nil {
  84. c.Flash.Error(err.Error())
  85. }
  86. c.Redirect(c.Org.OrgLink + "/settings")
  87. }
  88. func SettingsDelete(c *context.Context) {
  89. c.Title("org.settings")
  90. c.PageIs("SettingsDelete")
  91. org := c.Org.Organization
  92. if c.Req.Method == "POST" {
  93. if _, err := db.Users.Authenticate(c.User.Name, c.Query("password"), c.User.LoginSource); err != nil {
  94. if db.IsErrUserNotExist(err) {
  95. c.RenderWithErr(c.Tr("form.enterred_invalid_password"), SETTINGS_DELETE, nil)
  96. } else {
  97. c.Error(err, "authenticate user")
  98. }
  99. return
  100. }
  101. if err := db.DeleteOrganization(org); err != nil {
  102. if db.IsErrUserOwnRepos(err) {
  103. c.Flash.Error(c.Tr("form.org_still_own_repo"))
  104. c.Redirect(c.Org.OrgLink + "/settings/delete")
  105. } else {
  106. c.Error(err, "delete organization")
  107. }
  108. } else {
  109. log.Trace("Organization deleted: %s", org.Name)
  110. c.Redirect(conf.Server.Subpath + "/")
  111. }
  112. return
  113. }
  114. c.Success(SETTINGS_DELETE)
  115. }