settings.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. "errors"
  7. "github.com/miniflux/miniflux2/model"
  8. "net/http"
  9. )
  10. type SettingsForm struct {
  11. Username string
  12. Password string
  13. Confirmation string
  14. Theme string
  15. Language string
  16. Timezone string
  17. }
  18. func (s *SettingsForm) Merge(user *model.User) *model.User {
  19. user.Username = s.Username
  20. user.Theme = s.Theme
  21. user.Language = s.Language
  22. user.Timezone = s.Timezone
  23. if s.Password != "" {
  24. user.Password = s.Password
  25. }
  26. return user
  27. }
  28. func (s *SettingsForm) Validate() error {
  29. if s.Username == "" || s.Theme == "" || s.Language == "" || s.Timezone == "" {
  30. return errors.New("The username, theme, language and timezone fields are mandatory.")
  31. }
  32. if s.Password != "" {
  33. if s.Password != s.Confirmation {
  34. return errors.New("Passwords are not the same.")
  35. }
  36. if len(s.Password) < 6 {
  37. return errors.New("You must use at least 6 characters")
  38. }
  39. }
  40. return nil
  41. }
  42. func NewSettingsForm(r *http.Request) *SettingsForm {
  43. return &SettingsForm{
  44. Username: r.FormValue("username"),
  45. Password: r.FormValue("password"),
  46. Confirmation: r.FormValue("confirmation"),
  47. Theme: r.FormValue("theme"),
  48. Language: r.FormValue("language"),
  49. Timezone: r.FormValue("timezone"),
  50. }
  51. }