builder.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package login
  2. import (
  3. "fmt"
  4. "github.com/qor5/admin/presets"
  5. "github.com/qor5/web"
  6. "github.com/qor5/x/i18n"
  7. "github.com/qor5/x/login"
  8. )
  9. const (
  10. OpenChangePasswordDialogEvent = "login_openChangePasswordDialog"
  11. )
  12. func New(pb *presets.Builder) *login.Builder {
  13. r := login.New()
  14. r.I18n(pb.I18n())
  15. vh := r.ViewHelper()
  16. r.LoginPageFunc(defaultLoginPage(vh, pb))
  17. r.ForgetPasswordPageFunc(defaultForgetPasswordPage(vh, pb))
  18. r.ResetPasswordLinkSentPageFunc(defaultResetPasswordLinkSentPage(vh, pb))
  19. r.ResetPasswordPageFunc(defaultResetPasswordPage(vh, pb))
  20. r.ChangePasswordPageFunc(defaultChangePasswordPage(vh, pb))
  21. r.TOTPSetupPageFunc(defaultTOTPSetupPage(vh, pb))
  22. r.TOTPValidatePageFunc(defaultTOTPValidatePage(vh, pb))
  23. registerChangePasswordEvents(r, pb)
  24. return r
  25. }
  26. func registerChangePasswordEvents(b *login.Builder, pb *presets.Builder) {
  27. vh := b.ViewHelper()
  28. showVar := "showChangePasswordDialog"
  29. pb.GetWebBuilder().RegisterEventFunc(OpenChangePasswordDialogEvent, func(ctx *web.EventContext) (r web.EventResponse, err error) {
  30. r.UpdatePortals = append(r.UpdatePortals, &web.PortalUpdate{
  31. Name: presets.DialogPortalName,
  32. Body: changePasswordDialog(vh, ctx, showVar, defaultChangePasswordDialogContent(vh, pb)(ctx)),
  33. })
  34. web.AppendVarsScripts(&r, fmt.Sprintf("setTimeout(function(){ vars.%s = true }, 100)", showVar))
  35. web.AppendVarsScripts(&r, fmt.Sprintf(`
  36. (function(){
  37. var tag = document.createElement("script");
  38. tag.src = "%s";
  39. document.getElementsByTagName("head")[0].appendChild(tag);
  40. })()
  41. `, login.ZxcvbnJSURL))
  42. return
  43. })
  44. pb.GetWebBuilder().RegisterEventFunc("login_changePassword", func(ctx *web.EventContext) (r web.EventResponse, err error) {
  45. oldPassword := ctx.R.FormValue("old_password")
  46. password := ctx.R.FormValue("password")
  47. confirmPassword := ctx.R.FormValue("confirm_password")
  48. otp := ctx.R.FormValue("otp")
  49. msgr := i18n.MustGetModuleMessages(ctx.R, login.I18nLoginKey, login.Messages_en_US).(*login.Messages)
  50. err = b.ChangePassword(ctx.R, oldPassword, password, confirmPassword, otp)
  51. if err != nil {
  52. msg := msgr.ErrorSystemError
  53. var color string
  54. if ne, ok := err.(*login.NoticeError); ok {
  55. msg = ne.Message
  56. switch ne.Level {
  57. case login.NoticeLevel_Info:
  58. color = "info"
  59. case login.NoticeLevel_Warn:
  60. color = "warning"
  61. case login.NoticeLevel_Error:
  62. color = "error"
  63. }
  64. } else {
  65. switch err {
  66. case login.ErrWrongPassword:
  67. msg = msgr.ErrorIncorrectPassword
  68. case login.ErrEmptyPassword:
  69. msg = msgr.ErrorPasswordCannotBeEmpty
  70. case login.ErrPasswordNotMatch:
  71. msg = msgr.ErrorPasswordNotMatch
  72. case login.ErrWrongTOTPCode:
  73. msg = msgr.ErrorIncorrectTOTPCode
  74. case login.ErrTOTPCodeHasBeenUsed:
  75. msg = msgr.ErrorTOTPCodeReused
  76. }
  77. color = "error"
  78. }
  79. presets.ShowMessage(&r, msg, color)
  80. return r, nil
  81. }
  82. presets.ShowMessage(&r, msgr.InfoPasswordSuccessfullyChanged, "info")
  83. web.AppendVarsScripts(&r, fmt.Sprintf("vars.%s = false", showVar))
  84. return r, nil
  85. })
  86. }