user.go 4.6 KB

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