本站源代码
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.

110 lines
2.9KB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package setting
  6. import (
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/base"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. const (
  13. tplSettingsSecurity base.TplName = "user/settings/security"
  14. tplSettingsTwofaEnroll base.TplName = "user/settings/twofa_enroll"
  15. )
  16. // Security render change user's password page and 2FA
  17. func Security(ctx *context.Context) {
  18. ctx.Data["Title"] = ctx.Tr("settings")
  19. ctx.Data["PageIsSettingsSecurity"] = true
  20. if ctx.Query("openid.return_to") != "" {
  21. settingsOpenIDVerify(ctx)
  22. return
  23. }
  24. loadSecurityData(ctx)
  25. ctx.HTML(200, tplSettingsSecurity)
  26. }
  27. // DeleteAccountLink delete a single account link
  28. func DeleteAccountLink(ctx *context.Context) {
  29. id := ctx.QueryInt64("id")
  30. if id <= 0 {
  31. ctx.Flash.Error("Account link id is not given")
  32. } else {
  33. if _, err := models.RemoveAccountLink(ctx.User, id); err != nil {
  34. ctx.Flash.Error("RemoveAccountLink: " + err.Error())
  35. } else {
  36. ctx.Flash.Success(ctx.Tr("settings.remove_account_link_success"))
  37. }
  38. }
  39. ctx.JSON(200, map[string]interface{}{
  40. "redirect": setting.AppSubURL + "/user/settings/security",
  41. })
  42. }
  43. func loadSecurityData(ctx *context.Context) {
  44. enrolled := true
  45. _, err := models.GetTwoFactorByUID(ctx.User.ID)
  46. if err != nil {
  47. if models.IsErrTwoFactorNotEnrolled(err) {
  48. enrolled = false
  49. } else {
  50. ctx.ServerError("SettingsTwoFactor", err)
  51. return
  52. }
  53. }
  54. ctx.Data["TwofaEnrolled"] = enrolled
  55. if enrolled {
  56. ctx.Data["U2FRegistrations"], err = models.GetU2FRegistrationsByUID(ctx.User.ID)
  57. if err != nil {
  58. ctx.ServerError("GetU2FRegistrationsByUID", err)
  59. return
  60. }
  61. ctx.Data["RequireU2F"] = true
  62. }
  63. tokens, err := models.ListAccessTokens(ctx.User.ID)
  64. if err != nil {
  65. ctx.ServerError("ListAccessTokens", err)
  66. return
  67. }
  68. ctx.Data["Tokens"] = tokens
  69. accountLinks, err := models.ListAccountLinks(ctx.User)
  70. if err != nil {
  71. ctx.ServerError("ListAccountLinks", err)
  72. return
  73. }
  74. // map the provider display name with the LoginSource
  75. sources := make(map[*models.LoginSource]string)
  76. for _, externalAccount := range accountLinks {
  77. if loginSource, err := models.GetLoginSourceByID(externalAccount.LoginSourceID); err == nil {
  78. var providerDisplayName string
  79. if loginSource.IsOAuth2() {
  80. providerTechnicalName := loginSource.OAuth2().Provider
  81. providerDisplayName = models.OAuth2Providers[providerTechnicalName].DisplayName
  82. } else {
  83. providerDisplayName = loginSource.Name
  84. }
  85. sources[loginSource] = providerDisplayName
  86. }
  87. }
  88. ctx.Data["AccountLinks"] = sources
  89. openid, err := models.GetUserOpenIDs(ctx.User.ID)
  90. if err != nil {
  91. ctx.ServerError("GetUserOpenIDs", err)
  92. return
  93. }
  94. ctx.Data["OpenIDs"] = openid
  95. }
上海开阖软件有限公司 沪ICP备12045867号-1