user.go 4.0 KB

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