setting.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "gopkg.in/clog.v1"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/models/errors"
  10. "github.com/gogits/gogs/pkg/context"
  11. "github.com/gogits/gogs/pkg/form"
  12. "github.com/gogits/gogs/pkg/setting"
  13. "github.com/gogits/gogs/routes/user"
  14. )
  15. const (
  16. SETTINGS_OPTIONS = "org/settings/options"
  17. SETTINGS_DELETE = "org/settings/delete"
  18. SETTINGS_WEBHOOKS = "org/settings/webhooks"
  19. )
  20. func Settings(c *context.Context) {
  21. c.Data["Title"] = c.Tr("org.settings")
  22. c.Data["PageIsSettingsOptions"] = true
  23. c.HTML(200, SETTINGS_OPTIONS)
  24. }
  25. func SettingsPost(c *context.Context, f form.UpdateOrgSetting) {
  26. c.Data["Title"] = c.Tr("org.settings")
  27. c.Data["PageIsSettingsOptions"] = true
  28. if c.HasError() {
  29. c.HTML(200, SETTINGS_OPTIONS)
  30. return
  31. }
  32. org := c.Org.Organization
  33. // Check if organization name has been changed.
  34. if org.LowerName != strings.ToLower(f.Name) {
  35. isExist, err := models.IsUserExist(org.ID, f.Name)
  36. if err != nil {
  37. c.Handle(500, "IsUserExist", err)
  38. return
  39. } else if isExist {
  40. c.Data["OrgName"] = true
  41. c.RenderWithErr(c.Tr("form.username_been_taken"), SETTINGS_OPTIONS, &f)
  42. return
  43. } else if err = models.ChangeUserName(org, f.Name); err != nil {
  44. c.Data["OrgName"] = true
  45. switch {
  46. case models.IsErrNameReserved(err):
  47. c.RenderWithErr(c.Tr("user.form.name_reserved"), SETTINGS_OPTIONS, &f)
  48. case models.IsErrNamePatternNotAllowed(err):
  49. c.RenderWithErr(c.Tr("user.form.name_pattern_not_allowed"), SETTINGS_OPTIONS, &f)
  50. default:
  51. c.Handle(500, "ChangeUserName", err)
  52. }
  53. return
  54. }
  55. // reset c.org.OrgLink with new name
  56. c.Org.OrgLink = setting.AppSubURL + "/org/" + f.Name
  57. log.Trace("Organization name changed: %s -> %s", org.Name, f.Name)
  58. }
  59. // In case it's just a case change.
  60. org.Name = f.Name
  61. org.LowerName = strings.ToLower(f.Name)
  62. if c.User.IsAdmin {
  63. org.MaxRepoCreation = f.MaxRepoCreation
  64. }
  65. org.FullName = f.FullName
  66. org.Description = f.Description
  67. org.Website = f.Website
  68. org.Location = f.Location
  69. if err := models.UpdateUser(org); err != nil {
  70. c.Handle(500, "UpdateUser", err)
  71. return
  72. }
  73. log.Trace("Organization setting updated: %s", org.Name)
  74. c.Flash.Success(c.Tr("org.settings.update_setting_success"))
  75. c.Redirect(c.Org.OrgLink + "/settings")
  76. }
  77. func SettingsAvatar(c *context.Context, f form.Avatar) {
  78. f.Source = form.AVATAR_LOCAL
  79. if err := user.UpdateAvatarSetting(c, f, c.Org.Organization); err != nil {
  80. c.Flash.Error(err.Error())
  81. } else {
  82. c.Flash.Success(c.Tr("org.settings.update_avatar_success"))
  83. }
  84. c.Redirect(c.Org.OrgLink + "/settings")
  85. }
  86. func SettingsDeleteAvatar(c *context.Context) {
  87. if err := c.Org.Organization.DeleteAvatar(); err != nil {
  88. c.Flash.Error(err.Error())
  89. }
  90. c.Redirect(c.Org.OrgLink + "/settings")
  91. }
  92. func SettingsDelete(c *context.Context) {
  93. c.Title("org.settings")
  94. c.PageIs("SettingsDelete")
  95. org := c.Org.Organization
  96. if c.Req.Method == "POST" {
  97. if _, err := models.UserLogin(c.User.Name, c.Query("password"), c.User.LoginSource); err != nil {
  98. if errors.IsUserNotExist(err) {
  99. c.RenderWithErr(c.Tr("form.enterred_invalid_password"), SETTINGS_DELETE, nil)
  100. } else {
  101. c.ServerError("UserLogin", err)
  102. }
  103. return
  104. }
  105. if err := models.DeleteOrganization(org); err != nil {
  106. if models.IsErrUserOwnRepos(err) {
  107. c.Flash.Error(c.Tr("form.org_still_own_repo"))
  108. c.Redirect(c.Org.OrgLink + "/settings/delete")
  109. } else {
  110. c.ServerError("DeleteOrganization", err)
  111. }
  112. } else {
  113. log.Trace("Organization deleted: %s", org.Name)
  114. c.Redirect(setting.AppSubURL + "/")
  115. }
  116. return
  117. }
  118. c.Success(SETTINGS_DELETE)
  119. }
  120. func Webhooks(c *context.Context) {
  121. c.Data["Title"] = c.Tr("org.settings")
  122. c.Data["PageIsSettingsHooks"] = true
  123. c.Data["BaseLink"] = c.Org.OrgLink
  124. c.Data["Description"] = c.Tr("org.settings.hooks_desc")
  125. c.Data["Types"] = setting.Webhook.Types
  126. ws, err := models.GetWebhooksByOrgID(c.Org.Organization.ID)
  127. if err != nil {
  128. c.Handle(500, "GetWebhooksByOrgId", err)
  129. return
  130. }
  131. c.Data["Webhooks"] = ws
  132. c.HTML(200, SETTINGS_WEBHOOKS)
  133. }
  134. func DeleteWebhook(c *context.Context) {
  135. if err := models.DeleteWebhookOfOrgByID(c.Org.Organization.ID, c.QueryInt64("id")); err != nil {
  136. c.Flash.Error("DeleteWebhookByOrgID: " + err.Error())
  137. } else {
  138. c.Flash.Success(c.Tr("repo.settings.webhook_deletion_success"))
  139. }
  140. c.JSON(200, map[string]interface{}{
  141. "redirect": c.Org.OrgLink + "/settings/hooks",
  142. })
  143. }