settings.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. }
  25. // Merge updates the fields of the given user.
  26. func (s *SettingsForm) Merge(user *model.User) *model.User {
  27. user.Username = s.Username
  28. user.Theme = s.Theme
  29. user.Language = s.Language
  30. user.Timezone = s.Timezone
  31. user.EntryDirection = s.EntryDirection
  32. user.EntriesPerPage = s.EntriesPerPage
  33. user.KeyboardShortcuts = s.KeyboardShortcuts
  34. user.ShowReadingTime = s.ShowReadingTime
  35. user.Extra["custom_css"] = s.CustomCSS
  36. if s.Password != "" {
  37. user.Password = s.Password
  38. }
  39. return user
  40. }
  41. // Validate makes sure the form values are valid.
  42. func (s *SettingsForm) Validate() error {
  43. if s.Username == "" || s.Theme == "" || s.Language == "" || s.Timezone == "" || s.EntryDirection == "" {
  44. return errors.NewLocalizedError("error.settings_mandatory_fields")
  45. }
  46. if s.EntriesPerPage < 1 {
  47. return errors.NewLocalizedError("error.entries_per_page_invalid")
  48. }
  49. if s.Confirmation == "" {
  50. // Firefox insists on auto-completing the password field.
  51. // If the confirmation field is blank, the user probably
  52. // didn't intend to change their password.
  53. s.Password = ""
  54. } else if s.Password != "" {
  55. if s.Password != s.Confirmation {
  56. return errors.NewLocalizedError("error.different_passwords")
  57. }
  58. if len(s.Password) < 6 {
  59. return errors.NewLocalizedError("error.password_min_length")
  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, 64)
  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. EntriesPerPage: int(entriesPerPage),
  79. KeyboardShortcuts: r.FormValue("keyboard_shortcuts") == "1",
  80. ShowReadingTime: r.FormValue("show_reading_time") == "1",
  81. CustomCSS: r.FormValue("custom_css"),
  82. }
  83. }