settings.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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
  5. import (
  6. "net/http"
  7. "github.com/miniflux/miniflux2/errors"
  8. "github.com/miniflux/miniflux2/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. }
  19. // Merge updates the fields of the given user.
  20. func (s *SettingsForm) Merge(user *model.User) *model.User {
  21. user.Username = s.Username
  22. user.Theme = s.Theme
  23. user.Language = s.Language
  24. user.Timezone = s.Timezone
  25. if s.Password != "" {
  26. user.Password = s.Password
  27. }
  28. return user
  29. }
  30. // Validate makes sure the form values are valid.
  31. func (s *SettingsForm) Validate() error {
  32. if s.Username == "" || s.Theme == "" || s.Language == "" || s.Timezone == "" {
  33. return errors.NewLocalizedError("The username, theme, language and timezone fields are mandatory.")
  34. }
  35. if s.Password != "" {
  36. if s.Password != s.Confirmation {
  37. return errors.NewLocalizedError("Passwords are not the same.")
  38. }
  39. if len(s.Password) < 6 {
  40. return errors.NewLocalizedError("You must use at least 6 characters")
  41. }
  42. }
  43. return nil
  44. }
  45. // NewSettingsForm returns a new SettingsForm.
  46. func NewSettingsForm(r *http.Request) *SettingsForm {
  47. return &SettingsForm{
  48. Username: r.FormValue("username"),
  49. Password: r.FormValue("password"),
  50. Confirmation: r.FormValue("confirmation"),
  51. Theme: r.FormValue("theme"),
  52. Language: r.FormValue("language"),
  53. Timezone: r.FormValue("timezone"),
  54. }
  55. }