settings.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.Password != "" {
  38. if s.Password != s.Confirmation {
  39. return errors.NewLocalizedError("error.different_passwords")
  40. }
  41. if len(s.Password) < 6 {
  42. return errors.NewLocalizedError("error.password_min_length")
  43. }
  44. }
  45. return nil
  46. }
  47. // NewSettingsForm returns a new SettingsForm.
  48. func NewSettingsForm(r *http.Request) *SettingsForm {
  49. return &SettingsForm{
  50. Username: r.FormValue("username"),
  51. Password: r.FormValue("password"),
  52. Confirmation: r.FormValue("confirmation"),
  53. Theme: r.FormValue("theme"),
  54. Language: r.FormValue("language"),
  55. Timezone: r.FormValue("timezone"),
  56. EntryDirection: r.FormValue("entry_direction"),
  57. }
  58. }