setting.go 3.5 KB

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