settings.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. "miniflux.app/errors"
  8. "miniflux.app/model"
  9. )
  10. // SettingsForm represents the settings form.
  11. type SettingsForm struct {
  12. Username string
  13. Password string
  14. Confirmation string
  15. Theme string
  16. Language string
  17. Timezone string
  18. EntryDirection string
  19. KeyboardShortcuts bool
  20. CustomCSS string
  21. }
  22. // Merge updates the fields of the given user.
  23. func (s *SettingsForm) Merge(user *model.User) *model.User {
  24. user.Username = s.Username
  25. user.Theme = s.Theme
  26. user.Language = s.Language
  27. user.Timezone = s.Timezone
  28. user.EntryDirection = s.EntryDirection
  29. user.KeyboardShortcuts = s.KeyboardShortcuts
  30. user.Extra["custom_css"] = s.CustomCSS
  31. if s.Password != "" {
  32. user.Password = s.Password
  33. }
  34. return user
  35. }
  36. // Validate makes sure the form values are valid.
  37. func (s *SettingsForm) Validate() error {
  38. if s.Username == "" || s.Theme == "" || s.Language == "" || s.Timezone == "" || s.EntryDirection == "" {
  39. return errors.NewLocalizedError("error.settings_mandatory_fields")
  40. }
  41. if s.Confirmation == "" {
  42. // Firefox insists on auto-completing the password field.
  43. // If the confirmation field is blank, the user probably
  44. // didn't intend to change their password.
  45. s.Password = ""
  46. } else if s.Password != "" {
  47. if s.Password != s.Confirmation {
  48. return errors.NewLocalizedError("error.different_passwords")
  49. }
  50. if len(s.Password) < 6 {
  51. return errors.NewLocalizedError("error.password_min_length")
  52. }
  53. }
  54. return nil
  55. }
  56. // NewSettingsForm returns a new SettingsForm.
  57. func NewSettingsForm(r *http.Request) *SettingsForm {
  58. return &SettingsForm{
  59. Username: r.FormValue("username"),
  60. Password: r.FormValue("password"),
  61. Confirmation: r.FormValue("confirmation"),
  62. Theme: r.FormValue("theme"),
  63. Language: r.FormValue("language"),
  64. Timezone: r.FormValue("timezone"),
  65. EntryDirection: r.FormValue("entry_direction"),
  66. KeyboardShortcuts: r.FormValue("keyboard_shortcuts") == "1",
  67. CustomCSS: r.FormValue("custom_css"),
  68. }
  69. }