setting.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. "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/context"
  11. "github.com/gogits/gogs/modules/log"
  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 *context.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 *context.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.LowerName != strings.ToLower(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. // reset ctx.org.OrgLink with new name
  53. ctx.Org.OrgLink = setting.AppSubUrl + "/org/" + form.Name
  54. log.Trace("Organization name changed: %s -> %s", org.Name, form.Name)
  55. }
  56. // In case it's just a case change.
  57. org.Name = form.Name
  58. org.LowerName = strings.ToLower(form.Name)
  59. if ctx.User.IsAdmin {
  60. org.MaxRepoCreation = form.MaxRepoCreation
  61. }
  62. org.FullName = form.FullName
  63. org.Description = form.Description
  64. org.Website = form.Website
  65. org.Location = form.Location
  66. if err := models.UpdateUser(org); err != nil {
  67. ctx.Handle(500, "UpdateUser", err)
  68. return
  69. }
  70. log.Trace("Organization setting updated: %s", org.Name)
  71. ctx.Flash.Success(ctx.Tr("org.settings.update_setting_success"))
  72. ctx.Redirect(ctx.Org.OrgLink + "/settings")
  73. }
  74. func SettingsAvatar(ctx *context.Context, form auth.AvatarForm) {
  75. form.Source = auth.AVATAR_LOCAL
  76. if err := user.UpdateAvatarSetting(ctx, form, ctx.Org.Organization); err != nil {
  77. ctx.Flash.Error(err.Error())
  78. } else {
  79. ctx.Flash.Success(ctx.Tr("org.settings.update_avatar_success"))
  80. }
  81. ctx.Redirect(ctx.Org.OrgLink + "/settings")
  82. }
  83. func SettingsDeleteAvatar(ctx *context.Context) {
  84. if err := ctx.Org.Organization.DeleteAvatar(); err != nil {
  85. ctx.Flash.Error(err.Error())
  86. }
  87. ctx.Redirect(ctx.Org.OrgLink + "/settings")
  88. }
  89. func SettingsDelete(ctx *context.Context) {
  90. ctx.Data["Title"] = ctx.Tr("org.settings")
  91. ctx.Data["PageIsSettingsDelete"] = true
  92. org := ctx.Org.Organization
  93. if ctx.Req.Method == "POST" {
  94. if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  95. if models.IsErrUserNotExist(err) {
  96. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_DELETE, nil)
  97. } else {
  98. ctx.Handle(500, "UserSignIn", err)
  99. }
  100. return
  101. }
  102. if err := models.DeleteOrganization(org); err != nil {
  103. if models.IsErrUserOwnRepos(err) {
  104. ctx.Flash.Error(ctx.Tr("form.org_still_own_repo"))
  105. ctx.Redirect(ctx.Org.OrgLink + "/settings/delete")
  106. } else {
  107. ctx.Handle(500, "DeleteOrganization", err)
  108. }
  109. } else {
  110. log.Trace("Organization deleted: %s", org.Name)
  111. ctx.Redirect(setting.AppSubUrl + "/")
  112. }
  113. return
  114. }
  115. ctx.HTML(200, SETTINGS_DELETE)
  116. }
  117. func Webhooks(ctx *context.Context) {
  118. ctx.Data["Title"] = ctx.Tr("org.settings")
  119. ctx.Data["PageIsSettingsHooks"] = true
  120. ctx.Data["BaseLink"] = ctx.Org.OrgLink
  121. ctx.Data["Description"] = ctx.Tr("org.settings.hooks_desc")
  122. ws, err := models.GetWebhooksByOrgID(ctx.Org.Organization.ID)
  123. if err != nil {
  124. ctx.Handle(500, "GetWebhooksByOrgId", err)
  125. return
  126. }
  127. ctx.Data["Webhooks"] = ws
  128. ctx.HTML(200, SETTINGS_HOOKS)
  129. }
  130. func DeleteWebhook(ctx *context.Context) {
  131. if err := models.DeleteWebhookOfOrgByID(ctx.Org.Organization.ID, ctx.QueryInt64("id")); err != nil {
  132. ctx.Flash.Error("DeleteWebhookByOrgID: " + err.Error())
  133. } else {
  134. ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
  135. }
  136. ctx.JSON(200, map[string]interface{}{
  137. "redirect": ctx.Org.OrgLink + "/settings/hooks",
  138. })
  139. }