4
0

user.go 1.7 KB

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