user.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 model // import "miniflux.app/model"
  5. import (
  6. "errors"
  7. "time"
  8. "miniflux.app/timezone"
  9. )
  10. // User represents a user in the system.
  11. type User struct {
  12. ID int64 `json:"id"`
  13. Username string `json:"username"`
  14. Password string `json:"password,omitempty"`
  15. IsAdmin bool `json:"is_admin"`
  16. Theme string `json:"theme"`
  17. Language string `json:"language"`
  18. Timezone string `json:"timezone"`
  19. EntryDirection string `json:"entry_sorting_direction"`
  20. Stylesheet string `json:"stylesheet"`
  21. GoogleID string `json:"google_id"`
  22. OpenIDConnectID string `json:"openid_connect_id"`
  23. EntriesPerPage int `json:"entries_per_page"`
  24. KeyboardShortcuts bool `json:"keyboard_shortcuts"`
  25. ShowReadingTime bool `json:"show_reading_time"`
  26. EntrySwipe bool `json:"entry_swipe"`
  27. LastLoginAt *time.Time `json:"last_login_at,omitempty"`
  28. }
  29. // NewUser returns a new User.
  30. func NewUser() *User {
  31. return &User{}
  32. }
  33. // ValidateUserCreation validates new user.
  34. func (u User) ValidateUserCreation() error {
  35. if err := u.ValidateUserLogin(); err != nil {
  36. return err
  37. }
  38. return u.ValidatePassword()
  39. }
  40. // ValidateUserModification validates user modification payload.
  41. func (u User) ValidateUserModification() error {
  42. if u.Theme != "" {
  43. return ValidateTheme(u.Theme)
  44. }
  45. if u.Password != "" {
  46. return u.ValidatePassword()
  47. }
  48. return nil
  49. }
  50. // ValidateUserLogin validates user credential requirements.
  51. func (u User) ValidateUserLogin() error {
  52. if u.Username == "" {
  53. return errors.New("The username is mandatory")
  54. }
  55. if u.Password == "" {
  56. return errors.New("The password is mandatory")
  57. }
  58. return nil
  59. }
  60. // ValidatePassword validates user password requirements.
  61. func (u User) ValidatePassword() error {
  62. if u.Password != "" && len(u.Password) < 6 {
  63. return errors.New("The password must have at least 6 characters")
  64. }
  65. return nil
  66. }
  67. // UseTimezone converts last login date to the given timezone.
  68. func (u *User) UseTimezone(tz string) {
  69. if u.LastLoginAt != nil {
  70. *u.LastLoginAt = timezone.Convert(tz, *u.LastLoginAt)
  71. }
  72. }
  73. // Users represents a list of users.
  74. type Users []*User
  75. // UseTimezone converts last login timestamp of all users to the given timezone.
  76. func (u Users) UseTimezone(tz string) {
  77. for _, user := range u {
  78. user.UseTimezone(tz)
  79. }
  80. }