user.go 3.8 KB

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