You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.8 KiB

  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. "github.com/gogits/gogs/models"
  7. "github.com/gogits/gogs/modules/auth"
  8. "github.com/gogits/gogs/modules/base"
  9. "github.com/gogits/gogs/modules/log"
  10. "github.com/gogits/gogs/modules/middleware"
  11. )
  12. const (
  13. SETTINGS_OPTIONS base.TplName = "org/settings/options"
  14. SETTINGS_DELETE base.TplName = "org/settings/delete"
  15. )
  16. func Settings(ctx *middleware.Context) {
  17. ctx.Data["Title"] = ctx.Tr("org.settings")
  18. ctx.Data["PageIsSettingsOptions"] = true
  19. ctx.HTML(200, SETTINGS_OPTIONS)
  20. }
  21. func SettingsPost(ctx *middleware.Context, form auth.UpdateOrgSettingForm) {
  22. ctx.Data["Title"] = ctx.Tr("org.settings")
  23. ctx.Data["PageIsSettingsOptions"] = true
  24. if ctx.HasError() {
  25. ctx.HTML(200, SETTINGS_OPTIONS)
  26. return
  27. }
  28. org := ctx.Org.Organization
  29. // Check if organization name has been changed.
  30. if org.Name != form.OrgUserName {
  31. isExist, err := models.IsUserExist(form.OrgUserName)
  32. if err != nil {
  33. ctx.Handle(500, "IsUserExist", err)
  34. return
  35. } else if isExist {
  36. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_OPTIONS, &form)
  37. return
  38. } else if err = models.ChangeUserName(org, form.OrgUserName); err != nil {
  39. if err == models.ErrUserNameIllegal {
  40. ctx.Flash.Error(ctx.Tr("form.illegal_username"))
  41. ctx.Redirect("/org/" + org.LowerName + "/settings")
  42. return
  43. } else {
  44. ctx.Handle(500, "ChangeUserName", err)
  45. }
  46. return
  47. }
  48. log.Trace("Organization name changed: %s -> %s", org.Name, form.OrgUserName)
  49. org.Name = form.OrgUserName
  50. }
  51. org.FullName = form.OrgFullName
  52. org.Email = form.Email
  53. org.Description = form.Description
  54. org.Website = form.Website
  55. org.Location = form.Location
  56. org.Avatar = base.EncodeMd5(form.Avatar)
  57. org.AvatarEmail = form.Avatar
  58. if err := models.UpdateUser(org); err != nil {
  59. ctx.Handle(500, "UpdateUser", err)
  60. return
  61. }
  62. log.Trace("Organization setting updated: %s", org.Name)
  63. ctx.Flash.Success(ctx.Tr("org.settings.update_setting_success"))
  64. ctx.Redirect("/org/" + org.Name + "/settings")
  65. }
  66. func SettingsDelete(ctx *middleware.Context) {
  67. ctx.Data["Title"] = ctx.Tr("org.settings")
  68. ctx.Data["PageIsSettingsDelete"] = true
  69. org := ctx.Org.Organization
  70. if ctx.Req.Method == "POST" {
  71. // TODO: validate password.
  72. if err := models.DeleteOrganization(org); err != nil {
  73. switch err {
  74. case models.ErrUserOwnRepos:
  75. ctx.Flash.Error(ctx.Tr("form.org_still_own_repo"))
  76. ctx.Redirect("/org/" + org.LowerName + "/settings/delete")
  77. default:
  78. ctx.Handle(500, "DeleteOrganization", err)
  79. }
  80. } else {
  81. log.Trace("Organization deleted: %s", ctx.User.Name)
  82. ctx.Redirect("/")
  83. }
  84. return
  85. }
  86. ctx.HTML(200, SETTINGS_DELETE)
  87. }