user.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. "time"
  7. "miniflux.app/timezone"
  8. )
  9. // User represents a user in the system.
  10. type User struct {
  11. ID int64 `json:"id"`
  12. Username string `json:"username"`
  13. Password string `json:"-"`
  14. IsAdmin bool `json:"is_admin"`
  15. Theme string `json:"theme"`
  16. Language string `json:"language"`
  17. Timezone string `json:"timezone"`
  18. EntryDirection string `json:"entry_sorting_direction"`
  19. Stylesheet string `json:"stylesheet"`
  20. GoogleID string `json:"google_id"`
  21. OpenIDConnectID string `json:"openid_connect_id"`
  22. EntriesPerPage int `json:"entries_per_page"`
  23. KeyboardShortcuts bool `json:"keyboard_shortcuts"`
  24. ShowReadingTime bool `json:"show_reading_time"`
  25. EntrySwipe bool `json:"entry_swipe"`
  26. LastLoginAt *time.Time `json:"last_login_at"`
  27. }
  28. // UserCreationRequest represents the request to create a user.
  29. type UserCreationRequest struct {
  30. Username string `json:"username"`
  31. Password string `json:"password"`
  32. IsAdmin bool `json:"is_admin"`
  33. GoogleID string `json:"google_id"`
  34. OpenIDConnectID string `json:"openid_connect_id"`
  35. }
  36. // UserModificationRequest represents the request to update a user.
  37. type UserModificationRequest struct {
  38. Username *string `json:"username"`
  39. Password *string `json:"password"`
  40. Theme *string `json:"theme"`
  41. Language *string `json:"language"`
  42. Timezone *string `json:"timezone"`
  43. EntryDirection *string `json:"entry_sorting_direction"`
  44. Stylesheet *string `json:"stylesheet"`
  45. GoogleID *string `json:"google_id"`
  46. OpenIDConnectID *string `json:"openid_connect_id"`
  47. EntriesPerPage *int `json:"entries_per_page"`
  48. IsAdmin *bool `json:"is_admin"`
  49. KeyboardShortcuts *bool `json:"keyboard_shortcuts"`
  50. ShowReadingTime *bool `json:"show_reading_time"`
  51. EntrySwipe *bool `json:"entry_swipe"`
  52. }
  53. // Patch updates the User object with the modification request.
  54. func (u *UserModificationRequest) Patch(user *User) {
  55. if u.Username != nil {
  56. user.Username = *u.Username
  57. }
  58. if u.Password != nil {
  59. user.Password = *u.Password
  60. }
  61. if u.IsAdmin != nil {
  62. user.IsAdmin = *u.IsAdmin
  63. }
  64. if u.Theme != nil {
  65. user.Theme = *u.Theme
  66. }
  67. if u.Language != nil {
  68. user.Language = *u.Language
  69. }
  70. if u.Timezone != nil {
  71. user.Timezone = *u.Timezone
  72. }
  73. if u.EntryDirection != nil {
  74. user.EntryDirection = *u.EntryDirection
  75. }
  76. if u.Stylesheet != nil {
  77. user.Stylesheet = *u.Stylesheet
  78. }
  79. if u.GoogleID != nil {
  80. user.GoogleID = *u.GoogleID
  81. }
  82. if u.OpenIDConnectID != nil {
  83. user.OpenIDConnectID = *u.OpenIDConnectID
  84. }
  85. if u.EntriesPerPage != nil {
  86. user.EntriesPerPage = *u.EntriesPerPage
  87. }
  88. if u.KeyboardShortcuts != nil {
  89. user.KeyboardShortcuts = *u.KeyboardShortcuts
  90. }
  91. if u.ShowReadingTime != nil {
  92. user.ShowReadingTime = *u.ShowReadingTime
  93. }
  94. if u.EntrySwipe != nil {
  95. user.EntrySwipe = *u.EntrySwipe
  96. }
  97. }
  98. // UseTimezone converts last login date to the given timezone.
  99. func (u *User) UseTimezone(tz string) {
  100. if u.LastLoginAt != nil {
  101. *u.LastLoginAt = timezone.Convert(tz, *u.LastLoginAt)
  102. }
  103. }
  104. // Users represents a list of users.
  105. type Users []*User
  106. // UseTimezone converts last login timestamp of all users to the given timezone.
  107. func (u Users) UseTimezone(tz string) {
  108. for _, user := range u {
  109. user.UseTimezone(tz)
  110. }
  111. }