user.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. EntryOrder string `json:"entry_sorting_order"`
  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"`
  28. DisplayMode string `json:"display_mode"`
  29. DefaultReadingSpeed int `json:"default_reading_speed"`
  30. CJKReadingSpeed int `json:"cjk_reading_speed"`
  31. DefaultHomePage string `json:"default_home_page"`
  32. CategoriesSortOrder string `json:"categories_sort_order"`
  33. }
  34. // UserCreationRequest represents the request to create a user.
  35. type UserCreationRequest struct {
  36. Username string `json:"username"`
  37. Password string `json:"password"`
  38. IsAdmin bool `json:"is_admin"`
  39. GoogleID string `json:"google_id"`
  40. OpenIDConnectID string `json:"openid_connect_id"`
  41. }
  42. // UserModificationRequest represents the request to update a user.
  43. type UserModificationRequest struct {
  44. Username *string `json:"username"`
  45. Password *string `json:"password"`
  46. Theme *string `json:"theme"`
  47. Language *string `json:"language"`
  48. Timezone *string `json:"timezone"`
  49. EntryDirection *string `json:"entry_sorting_direction"`
  50. EntryOrder *string `json:"entry_sorting_order"`
  51. Stylesheet *string `json:"stylesheet"`
  52. GoogleID *string `json:"google_id"`
  53. OpenIDConnectID *string `json:"openid_connect_id"`
  54. EntriesPerPage *int `json:"entries_per_page"`
  55. IsAdmin *bool `json:"is_admin"`
  56. KeyboardShortcuts *bool `json:"keyboard_shortcuts"`
  57. ShowReadingTime *bool `json:"show_reading_time"`
  58. EntrySwipe *bool `json:"entry_swipe"`
  59. DisplayMode *string `json:"display_mode"`
  60. DefaultReadingSpeed *int `json:"default_reading_speed"`
  61. CJKReadingSpeed *int `json:"cjk_reading_speed"`
  62. DefaultHomePage *string `json:"default_home_page"`
  63. CategoriesSortOrder *string `json:"categories_sort_order"`
  64. }
  65. // Patch updates the User object with the modification request.
  66. func (u *UserModificationRequest) Patch(user *User) {
  67. if u.Username != nil {
  68. user.Username = *u.Username
  69. }
  70. if u.Password != nil {
  71. user.Password = *u.Password
  72. }
  73. if u.IsAdmin != nil {
  74. user.IsAdmin = *u.IsAdmin
  75. }
  76. if u.Theme != nil {
  77. user.Theme = *u.Theme
  78. }
  79. if u.Language != nil {
  80. user.Language = *u.Language
  81. }
  82. if u.Timezone != nil {
  83. user.Timezone = *u.Timezone
  84. }
  85. if u.EntryDirection != nil {
  86. user.EntryDirection = *u.EntryDirection
  87. }
  88. if u.EntryOrder != nil {
  89. user.EntryOrder = *u.EntryOrder
  90. }
  91. if u.Stylesheet != nil {
  92. user.Stylesheet = *u.Stylesheet
  93. }
  94. if u.GoogleID != nil {
  95. user.GoogleID = *u.GoogleID
  96. }
  97. if u.OpenIDConnectID != nil {
  98. user.OpenIDConnectID = *u.OpenIDConnectID
  99. }
  100. if u.EntriesPerPage != nil {
  101. user.EntriesPerPage = *u.EntriesPerPage
  102. }
  103. if u.KeyboardShortcuts != nil {
  104. user.KeyboardShortcuts = *u.KeyboardShortcuts
  105. }
  106. if u.ShowReadingTime != nil {
  107. user.ShowReadingTime = *u.ShowReadingTime
  108. }
  109. if u.EntrySwipe != nil {
  110. user.EntrySwipe = *u.EntrySwipe
  111. }
  112. if u.DisplayMode != nil {
  113. user.DisplayMode = *u.DisplayMode
  114. }
  115. if u.DefaultReadingSpeed != nil {
  116. user.DefaultReadingSpeed = *u.DefaultReadingSpeed
  117. }
  118. if u.CJKReadingSpeed != nil {
  119. user.CJKReadingSpeed = *u.CJKReadingSpeed
  120. }
  121. if u.DefaultHomePage != nil {
  122. user.DefaultHomePage = *u.DefaultHomePage
  123. }
  124. if u.CategoriesSortOrder != nil {
  125. user.CategoriesSortOrder = *u.CategoriesSortOrder
  126. }
  127. }
  128. // UseTimezone converts last login date to the given timezone.
  129. func (u *User) UseTimezone(tz string) {
  130. if u.LastLoginAt != nil {
  131. *u.LastLoginAt = timezone.Convert(tz, *u.LastLoginAt)
  132. }
  133. }
  134. // Users represents a list of users.
  135. type Users []*User
  136. // UseTimezone converts last login timestamp of all users to the given timezone.
  137. func (u Users) UseTimezone(tz string) {
  138. for _, user := range u {
  139. user.UseTimezone(tz)
  140. }
  141. }