user.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2021 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 validator // import "miniflux.app/validator"
  5. import (
  6. "miniflux.app/locale"
  7. "miniflux.app/model"
  8. "miniflux.app/storage"
  9. )
  10. // ValidateUserCreationWithPassword validates user creation with a password.
  11. func ValidateUserCreationWithPassword(store *storage.Storage, request *model.UserCreationRequest) *ValidationError {
  12. if request.Username == "" {
  13. return NewValidationError("error.user_mandatory_fields")
  14. }
  15. if store.UserExists(request.Username) {
  16. return NewValidationError("error.user_already_exists")
  17. }
  18. if err := validatePassword(request.Password); err != nil {
  19. return err
  20. }
  21. return nil
  22. }
  23. // ValidateUserModification validates user modifications.
  24. func ValidateUserModification(store *storage.Storage, userID int64, changes *model.UserModificationRequest) *ValidationError {
  25. if changes.Username != nil {
  26. if *changes.Username == "" {
  27. return NewValidationError("error.user_mandatory_fields")
  28. } else if store.AnotherUserExists(userID, *changes.Username) {
  29. return NewValidationError("error.user_already_exists")
  30. }
  31. }
  32. if changes.Password != nil {
  33. if err := validatePassword(*changes.Password); err != nil {
  34. return err
  35. }
  36. }
  37. if changes.Theme != nil {
  38. if err := validateTheme(*changes.Theme); err != nil {
  39. return err
  40. }
  41. }
  42. if changes.Language != nil {
  43. if err := validateLanguage(*changes.Language); err != nil {
  44. return err
  45. }
  46. }
  47. if changes.Timezone != nil {
  48. if err := validateTimezone(store, *changes.Timezone); err != nil {
  49. return err
  50. }
  51. }
  52. if changes.EntryDirection != nil {
  53. if err := validateEntryDirection(*changes.EntryDirection); err != nil {
  54. return err
  55. }
  56. }
  57. if changes.EntriesPerPage != nil {
  58. if err := validateEntriesPerPage(*changes.EntriesPerPage); err != nil {
  59. return err
  60. }
  61. }
  62. if changes.DisplayMode != nil {
  63. if err := validateDisplayMode(*changes.DisplayMode); err != nil {
  64. return err
  65. }
  66. }
  67. return nil
  68. }
  69. func validatePassword(password string) *ValidationError {
  70. if len(password) < 6 {
  71. return NewValidationError("error.password_min_length")
  72. }
  73. return nil
  74. }
  75. func validateTheme(theme string) *ValidationError {
  76. themes := model.Themes()
  77. if _, found := themes[theme]; !found {
  78. return NewValidationError("error.invalid_theme")
  79. }
  80. return nil
  81. }
  82. func validateLanguage(language string) *ValidationError {
  83. languages := locale.AvailableLanguages()
  84. if _, found := languages[language]; !found {
  85. return NewValidationError("error.invalid_language")
  86. }
  87. return nil
  88. }
  89. func validateTimezone(store *storage.Storage, timezone string) *ValidationError {
  90. timezones, err := store.Timezones()
  91. if err != nil {
  92. return NewValidationError(err.Error())
  93. }
  94. if _, found := timezones[timezone]; !found {
  95. return NewValidationError("error.invalid_timezone")
  96. }
  97. return nil
  98. }
  99. func validateEntryDirection(direction string) *ValidationError {
  100. if direction != "asc" && direction != "desc" {
  101. return NewValidationError("error.invalid_entry_direction")
  102. }
  103. return nil
  104. }
  105. func validateEntriesPerPage(entriesPerPage int) *ValidationError {
  106. if entriesPerPage < 1 {
  107. return NewValidationError("error.entries_per_page_invalid")
  108. }
  109. return nil
  110. }
  111. func validateDisplayMode(displayMode string) *ValidationError {
  112. if displayMode != "fullscreen" && displayMode != "standalone" && displayMode != "minimal-ui" && displayMode != "browser" {
  113. return NewValidationError("error.invalid_display_mode")
  114. }
  115. return nil
  116. }