settings.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. EntriesPerPage int
  21. KeyboardShortcuts bool
  22. ShowReadingTime bool
  23. CustomCSS string
  24. EntrySwipe bool
  25. DisplayMode string
  26. }
  27. // Merge updates the fields of the given user.
  28. func (s *SettingsForm) Merge(user *model.User) *model.User {
  29. user.Username = s.Username
  30. user.Theme = s.Theme
  31. user.Language = s.Language
  32. user.Timezone = s.Timezone
  33. user.EntryDirection = s.EntryDirection
  34. user.EntriesPerPage = s.EntriesPerPage
  35. user.KeyboardShortcuts = s.KeyboardShortcuts
  36. user.ShowReadingTime = s.ShowReadingTime
  37. user.Stylesheet = s.CustomCSS
  38. user.EntrySwipe = s.EntrySwipe
  39. user.DisplayMode = s.DisplayMode
  40. if s.Password != "" {
  41. user.Password = s.Password
  42. }
  43. return user
  44. }
  45. // Validate makes sure the form values are valid.
  46. func (s *SettingsForm) Validate() error {
  47. if s.Username == "" || s.Theme == "" || s.Language == "" || s.Timezone == "" || s.EntryDirection == "" || s.DisplayMode == "" {
  48. return errors.NewLocalizedError("error.settings_mandatory_fields")
  49. }
  50. if s.Confirmation == "" {
  51. // Firefox insists on auto-completing the password field.
  52. // If the confirmation field is blank, the user probably
  53. // didn't intend to change their password.
  54. s.Password = ""
  55. } else if s.Password != "" {
  56. if s.Password != s.Confirmation {
  57. return errors.NewLocalizedError("error.different_passwords")
  58. }
  59. }
  60. return nil
  61. }
  62. // NewSettingsForm returns a new SettingsForm.
  63. func NewSettingsForm(r *http.Request) *SettingsForm {
  64. entriesPerPage, err := strconv.ParseInt(r.FormValue("entries_per_page"), 10, 64)
  65. if err != nil {
  66. entriesPerPage = 0
  67. }
  68. return &SettingsForm{
  69. Username: r.FormValue("username"),
  70. Password: r.FormValue("password"),
  71. Confirmation: r.FormValue("confirmation"),
  72. Theme: r.FormValue("theme"),
  73. Language: r.FormValue("language"),
  74. Timezone: r.FormValue("timezone"),
  75. EntryDirection: r.FormValue("entry_direction"),
  76. EntriesPerPage: int(entriesPerPage),
  77. KeyboardShortcuts: r.FormValue("keyboard_shortcuts") == "1",
  78. ShowReadingTime: r.FormValue("show_reading_time") == "1",
  79. CustomCSS: r.FormValue("custom_css"),
  80. EntrySwipe: r.FormValue("entry_swipe") == "1",
  81. DisplayMode: r.FormValue("display_mode"),
  82. }
  83. }