settings.go 2.1 KB

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