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.

177 lines
4.9 KiB

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