A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

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