settings.go 2.0 KB

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