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.

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