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.

158 lines
4.4 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/Unknwon/com"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/modules/auth"
  9. "github.com/gogits/gogs/modules/base"
  10. "github.com/gogits/gogs/modules/log"
  11. "github.com/gogits/gogs/modules/middleware"
  12. "github.com/gogits/gogs/modules/setting"
  13. "github.com/gogits/gogs/routers/user"
  14. )
  15. const (
  16. SETTINGS_OPTIONS base.TplName = "org/settings/options"
  17. SETTINGS_DELETE base.TplName = "org/settings/delete"
  18. SETTINGS_HOOKS base.TplName = "org/settings/hooks"
  19. )
  20. func Settings(ctx *middleware.Context) {
  21. ctx.Data["Title"] = ctx.Tr("org.settings")
  22. ctx.Data["PageIsSettingsOptions"] = true
  23. ctx.HTML(200, SETTINGS_OPTIONS)
  24. }
  25. func SettingsPost(ctx *middleware.Context, form auth.UpdateOrgSettingForm) {
  26. ctx.Data["Title"] = ctx.Tr("org.settings")
  27. ctx.Data["PageIsSettingsOptions"] = true
  28. if ctx.HasError() {
  29. ctx.HTML(200, SETTINGS_OPTIONS)
  30. return
  31. }
  32. org := ctx.Org.Organization
  33. // Check if organization name has been changed.
  34. if org.Name != form.Name {
  35. isExist, err := models.IsUserExist(org.Id, form.Name)
  36. if err != nil {
  37. ctx.Handle(500, "IsUserExist", err)
  38. return
  39. } else if isExist {
  40. ctx.Data["OrgName"] = true
  41. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_OPTIONS, &form)
  42. return
  43. } else if err = models.ChangeUserName(org, form.Name); err != nil {
  44. if err == models.ErrUserNameIllegal {
  45. ctx.Data["OrgName"] = true
  46. ctx.RenderWithErr(ctx.Tr("form.illegal_username"), SETTINGS_OPTIONS, &form)
  47. } else {
  48. ctx.Handle(500, "ChangeUserName", err)
  49. }
  50. return
  51. }
  52. log.Trace("Organization name changed: %s -> %s", org.Name, form.Name)
  53. org.Name = form.Name
  54. }
  55. org.FullName = form.FullName
  56. org.Description = form.Description
  57. org.Website = form.Website
  58. org.Location = form.Location
  59. if err := models.UpdateUser(org); err != nil {
  60. ctx.Handle(500, "UpdateUser", err)
  61. return
  62. }
  63. log.Trace("Organization setting updated: %s", org.Name)
  64. ctx.Flash.Success(ctx.Tr("org.settings.update_setting_success"))
  65. ctx.Redirect(org.HomeLink() + "/settings")
  66. }
  67. func SettingsAvatar(ctx *middleware.Context, form auth.UploadAvatarForm) {
  68. form.Enable = true
  69. if err := user.UpdateAvatarSetting(ctx, form, ctx.Org.Organization); err != nil {
  70. ctx.Flash.Error(err.Error())
  71. } else {
  72. ctx.Flash.Success(ctx.Tr("org.settings.update_avatar_success"))
  73. }
  74. ctx.Redirect(ctx.Org.OrgLink + "/settings")
  75. }
  76. func SettingsDelete(ctx *middleware.Context) {
  77. ctx.Data["Title"] = ctx.Tr("org.settings")
  78. ctx.Data["PageIsSettingsDelete"] = true
  79. org := ctx.Org.Organization
  80. if ctx.Req.Method == "POST" {
  81. if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  82. if models.IsErrUserNotExist(err) {
  83. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_DELETE, nil)
  84. } else {
  85. ctx.Handle(500, "UserSignIn", err)
  86. }
  87. return
  88. }
  89. if err := models.DeleteOrganization(org); err != nil {
  90. if models.IsErrUserOwnRepos(err) {
  91. ctx.Flash.Error(ctx.Tr("form.org_still_own_repo"))
  92. ctx.Redirect(org.HomeLink() + "/settings/delete")
  93. } else {
  94. ctx.Handle(500, "DeleteOrganization", err)
  95. }
  96. } else {
  97. log.Trace("Organization deleted: %s", org.Name)
  98. ctx.Redirect(setting.AppSubUrl + "/")
  99. }
  100. return
  101. }
  102. ctx.HTML(200, SETTINGS_DELETE)
  103. }
  104. func Webhooks(ctx *middleware.Context) {
  105. ctx.Data["Title"] = ctx.Tr("org.settings")
  106. ctx.Data["PageIsSettingsHooks"] = true
  107. ctx.Data["BaseLink"] = ctx.Org.OrgLink
  108. ctx.Data["Description"] = ctx.Tr("org.settings.hooks_desc")
  109. // Delete web hook.
  110. remove := com.StrTo(ctx.Query("remove")).MustInt64()
  111. if remove > 0 {
  112. if err := models.DeleteWebhook(remove); err != nil {
  113. ctx.Handle(500, "DeleteWebhook", err)
  114. return
  115. }
  116. ctx.Flash.Success(ctx.Tr("repo.settings.remove_hook_success"))
  117. ctx.Redirect(ctx.Org.OrgLink + "/settings/hooks")
  118. return
  119. }
  120. ws, err := models.GetWebhooksByOrgId(ctx.Org.Organization.Id)
  121. if err != nil {
  122. ctx.Handle(500, "GetWebhooksByOrgId", err)
  123. return
  124. }
  125. ctx.Data["Webhooks"] = ws
  126. ctx.HTML(200, SETTINGS_HOOKS)
  127. }
  128. func DeleteWebhook(ctx *middleware.Context) {
  129. if err := models.DeleteWebhook(ctx.QueryInt64("id")); err != nil {
  130. ctx.Flash.Error("DeleteWebhook: " + err.Error())
  131. } else {
  132. ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
  133. }
  134. ctx.JSON(200, map[string]interface{}{
  135. "redirect": ctx.Org.OrgLink + "/settings/hooks",
  136. })
  137. }