auth_settings.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package form // import "miniflux.app/v2/internal/ui/form"
  4. import (
  5. "net/http"
  6. "miniflux.app/v2/internal/locale"
  7. "miniflux.app/v2/internal/model"
  8. )
  9. // AuthSettingsForm represents the authentication settings form.
  10. type AuthSettingsForm struct {
  11. Username string
  12. CurrentPassword string
  13. Password string
  14. Confirmation string
  15. }
  16. // Merge updates the fields of the given user.
  17. func (a *AuthSettingsForm) Merge(user *model.User) *model.User {
  18. user.Username = a.Username
  19. if a.Password != "" {
  20. user.Password = a.Password
  21. }
  22. return user
  23. }
  24. // Validate makes sure the form values are valid.
  25. func (a *AuthSettingsForm) Validate() *locale.LocalizedError {
  26. if a.Username == "" {
  27. return locale.NewLocalizedError("error.settings_mandatory_fields")
  28. }
  29. if a.Confirmation == "" {
  30. // Firefox insists on auto-completing the password field.
  31. // If the confirmation field is blank, the user probably
  32. // didn't intend to change their password.
  33. a.Password = ""
  34. } else if a.Password != "" {
  35. if a.Password != a.Confirmation {
  36. return locale.NewLocalizedError("error.different_passwords")
  37. }
  38. }
  39. return nil
  40. }
  41. // NewAuthSettingsForm returns a new AuthSettingsForm.
  42. func NewAuthSettingsForm(r *http.Request) *AuthSettingsForm {
  43. return &AuthSettingsForm{
  44. Username: r.FormValue("username"),
  45. CurrentPassword: r.FormValue("current_password"),
  46. Password: r.FormValue("password"),
  47. Confirmation: r.FormValue("confirmation"),
  48. }
  49. }