A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

120 řádky
3.0 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. )
  17. type (
  18. userCredentials struct {
  19. Alias string `json:"alias" schema:"alias"`
  20. Pass string `json:"pass" schema:"pass"`
  21. Email string `json:"email" schema:"email"`
  22. Web bool `json:"web" schema:"-"`
  23. To string `json:"-" schema:"to"`
  24. EmailLogin bool `json:"via_email" schema:"via_email"`
  25. }
  26. userRegistration struct {
  27. userCredentials
  28. InviteCode string `json:"invite_code" schema:"invite_code"`
  29. Honeypot string `json:"fullname" schema:"fullname"`
  30. Normalize bool `json:"normalize" schema:"normalize"`
  31. Signup bool `json:"signup" schema:"signup"`
  32. }
  33. // AuthUser contains information for a newly authenticated user (either
  34. // from signing up or logging in).
  35. AuthUser struct {
  36. AccessToken string `json:"access_token,omitempty"`
  37. Password string `json:"password,omitempty"`
  38. User *User `json:"user"`
  39. // Verbose user data
  40. Posts *[]PublicPost `json:"posts,omitempty"`
  41. Collections *[]Collection `json:"collections,omitempty"`
  42. }
  43. // User is a consistent user object in the database and all contexts (auth
  44. // and non-auth) in the API.
  45. User struct {
  46. ID int64 `json:"-"`
  47. Username string `json:"username"`
  48. HashedPass []byte `json:"-"`
  49. HasPass bool `json:"has_pass"`
  50. Email zero.String `json:"email"`
  51. Created time.Time `json:"created"`
  52. clearEmail string `json:"email"`
  53. }
  54. userMeStats struct {
  55. TotalCollections, TotalArticles, CollectionPosts uint64
  56. }
  57. ExportUser struct {
  58. *User
  59. Collections *[]CollectionObj `json:"collections"`
  60. AnonymousPosts []PublicPost `json:"posts"`
  61. }
  62. PublicUser struct {
  63. Username string `json:"username"`
  64. }
  65. )
  66. // EmailClear decrypts and returns the user's email, caching it in the user
  67. // object.
  68. func (u *User) EmailClear(keys *keychain) string {
  69. if u.clearEmail != "" {
  70. return u.clearEmail
  71. }
  72. if u.Email.Valid && u.Email.String != "" {
  73. email, err := data.Decrypt(keys.emailKey, []byte(u.Email.String))
  74. if err != nil {
  75. log.Error("Error decrypting user email: %v", err)
  76. } else {
  77. u.clearEmail = string(email)
  78. return u.clearEmail
  79. }
  80. }
  81. return ""
  82. }
  83. func (u User) CreatedFriendly() string {
  84. /*
  85. // TODO: accept a locale in this method and use that for the format
  86. var loc monday.Locale = monday.LocaleEnUS
  87. return monday.Format(u.Created, monday.DateTimeFormatsByLocale[loc], loc)
  88. */
  89. return u.Created.Format("January 2, 2006, 3:04 PM")
  90. }
  91. // Cookie strips down an AuthUser to contain only information necessary for
  92. // cookies.
  93. func (u User) Cookie() *User {
  94. u.HashedPass = []byte{}
  95. return &u
  96. }
  97. func (u *User) IsAdmin() bool {
  98. // TODO: get this from database
  99. return u.ID == 1
  100. }