user.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. // UserForm represents the user form.
  10. type UserForm struct {
  11. Username string
  12. Password string
  13. Confirmation string
  14. IsAdmin bool
  15. }
  16. // ValidateCreation validates user creation.
  17. func (u UserForm) ValidateCreation() *locale.LocalizedError {
  18. if u.Username == "" || u.Password == "" || u.Confirmation == "" {
  19. return locale.NewLocalizedError("error.fields_mandatory")
  20. }
  21. if u.Password != u.Confirmation {
  22. return locale.NewLocalizedError("error.different_passwords")
  23. }
  24. return nil
  25. }
  26. // ValidateModification validates user modification.
  27. func (u UserForm) ValidateModification() *locale.LocalizedError {
  28. if u.Username == "" {
  29. return locale.NewLocalizedError("error.user_mandatory_fields")
  30. }
  31. if u.Password != "" {
  32. if u.Password != u.Confirmation {
  33. return locale.NewLocalizedError("error.different_passwords")
  34. }
  35. if len(u.Password) < 6 {
  36. return locale.NewLocalizedError("error.password_min_length")
  37. }
  38. }
  39. return nil
  40. }
  41. // Merge updates the fields of the given user.
  42. func (u UserForm) Merge(user *model.User) *model.User {
  43. user.Username = u.Username
  44. user.IsAdmin = u.IsAdmin
  45. if u.Password != "" {
  46. user.Password = u.Password
  47. }
  48. return user
  49. }
  50. // NewUserForm returns a new UserForm.
  51. func NewUserForm(r *http.Request) *UserForm {
  52. return &UserForm{
  53. Username: r.FormValue("username"),
  54. Password: r.FormValue("password"),
  55. Confirmation: r.FormValue("confirmation"),
  56. IsAdmin: r.FormValue("is_admin") == "1",
  57. }
  58. }