user.go 5.1 KB

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