settings.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package form // import "miniflux.app/ui/form"
  5. import (
  6. "net/http"
  7. "strconv"
  8. "miniflux.app/errors"
  9. "miniflux.app/model"
  10. )
  11. // SettingsForm represents the settings form.
  12. type SettingsForm struct {
  13. Username string
  14. Password string
  15. Confirmation string
  16. Theme string
  17. Language string
  18. Timezone string
  19. EntryDirection string
  20. EntryOrder string
  21. EntriesPerPage int
  22. KeyboardShortcuts bool
  23. ShowReadingTime bool
  24. CustomCSS string
  25. EntrySwipe bool
  26. DisplayMode string
  27. }
  28. // Merge updates the fields of the given user.
  29. func (s *SettingsForm) Merge(user *model.User) *model.User {
  30. user.Username = s.Username
  31. user.Theme = s.Theme
  32. user.Language = s.Language
  33. user.Timezone = s.Timezone
  34. user.EntryDirection = s.EntryDirection
  35. user.EntryOrder = s.EntryOrder
  36. user.EntriesPerPage = s.EntriesPerPage
  37. user.KeyboardShortcuts = s.KeyboardShortcuts
  38. user.ShowReadingTime = s.ShowReadingTime
  39. user.Stylesheet = s.CustomCSS
  40. user.EntrySwipe = s.EntrySwipe
  41. user.DisplayMode = s.DisplayMode
  42. if s.Password != "" {
  43. user.Password = s.Password
  44. }
  45. return user
  46. }
  47. // Validate makes sure the form values are valid.
  48. func (s *SettingsForm) Validate() error {
  49. if s.Username == "" || s.Theme == "" || s.Language == "" || s.Timezone == "" || s.EntryDirection == "" || s.DisplayMode == "" {
  50. return errors.NewLocalizedError("error.settings_mandatory_fields")
  51. }
  52. if s.Confirmation == "" {
  53. // Firefox insists on auto-completing the password field.
  54. // If the confirmation field is blank, the user probably
  55. // didn't intend to change their password.
  56. s.Password = ""
  57. } else if s.Password != "" {
  58. if s.Password != s.Confirmation {
  59. return errors.NewLocalizedError("error.different_passwords")
  60. }
  61. }
  62. return nil
  63. }
  64. // NewSettingsForm returns a new SettingsForm.
  65. func NewSettingsForm(r *http.Request) *SettingsForm {
  66. entriesPerPage, err := strconv.ParseInt(r.FormValue("entries_per_page"), 10, 0)
  67. if err != nil {
  68. entriesPerPage = 0
  69. }
  70. return &SettingsForm{
  71. Username: r.FormValue("username"),
  72. Password: r.FormValue("password"),
  73. Confirmation: r.FormValue("confirmation"),
  74. Theme: r.FormValue("theme"),
  75. Language: r.FormValue("language"),
  76. Timezone: r.FormValue("timezone"),
  77. EntryDirection: r.FormValue("entry_direction"),
  78. EntryOrder: r.FormValue("entry_order"),
  79. EntriesPerPage: int(entriesPerPage),
  80. KeyboardShortcuts: r.FormValue("keyboard_shortcuts") == "1",
  81. ShowReadingTime: r.FormValue("show_reading_time") == "1",
  82. CustomCSS: r.FormValue("custom_css"),
  83. EntrySwipe: r.FormValue("entry_swipe") == "1",
  84. DisplayMode: r.FormValue("display_mode"),
  85. }
  86. }