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.

163 lines
4.6 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
Add support for federated avatars (#3320) * Add support for federated avatars Fixes #3105 Removes avatar fetching duplication code Adds an "Enable Federated Avatar" checkbox in user settings (defaults to unchecked) Moves avatar settings all in the same form, making local and remote avatars mutually exclusive Renames UploadAvatarForm to AvatarForm as it's not anymore only for uploading * Run gofmt on all modified files * Move Avatar form in its own page * Add go-libravatar dependency to vendor/ dir Hopefully helps with accepting the contribution. See also #3214 * Revert "Add go-libravatar dependency to vendor/ dir" This reverts commit a8cb93ae640bbb90f7d25012fc257bda9fae9b82. * Make federated avatar setting a global configuration Removes the per-user setting * Move avatar handling back to base tool, disable federated avatar in offline mode * Format, handle error * Properly set fallback host * Use unsupported github.com mirror for importing go-libravatar * Remove comment showing life exists outside of github.com ... pity, but contribution would not be accepted otherwise * Use Combo for Get and Post methods over /avatar * FEDERATED_AVATAR -> ENABLE_FEDERATED_AVATAR * Fix persistance of federated avatar lookup checkbox at install time * Federated Avatars -> Enable Federated Avatars * Use len(string) == 0 instead of string == "" * Move import line where it belong See https://github.com/Unknwon/go-code-convention/blob/master/en-US/import_packages.md Pity the import url is still the unofficial one, but oh well... * Save a line (and waste much more expensive time) * Remove redundant parens * Remove an empty line * Remove empty lines * Reorder lines to make diff smaller * Remove another newline Unknwon review got me start a fight against newlines * Move DISABLE_GRAVATAR and ENABLE_FEDERATED_AVATAR after OFFLINE_MODE On re-reading the diff I figured what Unknwon meant here: https://github.com/gogits/gogs/pull/3320/files#r73741106 * Remove newlines that weren't there before my intervention
8 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/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.DeleteWebhookByOrgID(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. }