A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

133 строки
3.2 KiB

  1. /*
  2. * Copyright © 2018 A Bunch Tell LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. package writefreely
  11. import (
  12. "time"
  13. "github.com/guregu/null/zero"
  14. "github.com/writeas/web-core/data"
  15. "github.com/writeas/web-core/log"
  16. "github.com/writeas/writefreely/key"
  17. )
  18. type UserStatus int
  19. const (
  20. UserActive = iota
  21. UserSilenced
  22. )
  23. type (
  24. userCredentials struct {
  25. Alias string `json:"alias" schema:"alias"`
  26. Pass string `json:"pass" schema:"pass"`
  27. Email string `json:"email" schema:"email"`
  28. Web bool `json:"web" schema:"-"`
  29. To string `json:"-" schema:"to"`
  30. EmailLogin bool `json:"via_email" schema:"via_email"`
  31. }
  32. userRegistration struct {
  33. userCredentials
  34. InviteCode string `json:"invite_code" schema:"invite_code"`
  35. Honeypot string `json:"fullname" schema:"fullname"`
  36. Normalize bool `json:"normalize" schema:"normalize"`
  37. Signup bool `json:"signup" schema:"signup"`
  38. }
  39. // AuthUser contains information for a newly authenticated user (either
  40. // from signing up or logging in).
  41. AuthUser struct {
  42. AccessToken string `json:"access_token,omitempty"`
  43. Password string `json:"password,omitempty"`
  44. User *User `json:"user"`
  45. // Verbose user data
  46. Posts *[]PublicPost `json:"posts,omitempty"`
  47. Collections *[]Collection `json:"collections,omitempty"`
  48. }
  49. // User is a consistent user object in the database and all contexts (auth
  50. // and non-auth) in the API.
  51. User struct {
  52. ID int64 `json:"-"`
  53. Username string `json:"username"`
  54. HashedPass []byte `json:"-"`
  55. HasPass bool `json:"has_pass"`
  56. Email zero.String `json:"email"`
  57. Created time.Time `json:"created"`
  58. Status UserStatus `json:"status"`
  59. clearEmail string `json:"email"`
  60. }
  61. userMeStats struct {
  62. TotalCollections, TotalArticles, CollectionPosts uint64
  63. }
  64. ExportUser struct {
  65. *User
  66. Collections *[]CollectionObj `json:"collections"`
  67. AnonymousPosts []PublicPost `json:"posts"`
  68. }
  69. PublicUser struct {
  70. Username string `json:"username"`
  71. }
  72. )
  73. // EmailClear decrypts and returns the user's email, caching it in the user
  74. // object.
  75. func (u *User) EmailClear(keys *key.Keychain) string {
  76. if u.clearEmail != "" {
  77. return u.clearEmail
  78. }
  79. if u.Email.Valid && u.Email.String != "" {
  80. email, err := data.Decrypt(keys.EmailKey, []byte(u.Email.String))
  81. if err != nil {
  82. log.Error("Error decrypting user email: %v", err)
  83. } else {
  84. u.clearEmail = string(email)
  85. return u.clearEmail
  86. }
  87. }
  88. return ""
  89. }
  90. func (u User) CreatedFriendly() string {
  91. /*
  92. // TODO: accept a locale in this method and use that for the format
  93. var loc monday.Locale = monday.LocaleEnUS
  94. return monday.Format(u.Created, monday.DateTimeFormatsByLocale[loc], loc)
  95. */
  96. return u.Created.Format("January 2, 2006, 3:04 PM")
  97. }
  98. // Cookie strips down an AuthUser to contain only information necessary for
  99. // cookies.
  100. func (u User) Cookie() *User {
  101. u.HashedPass = []byte{}
  102. return &u
  103. }
  104. func (u *User) IsAdmin() bool {
  105. // TODO: get this from database
  106. return u.ID == 1
  107. }
  108. func (u *User) IsSilenced() bool {
  109. return u.Status&UserSilenced != 0
  110. }