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.
 
 
 
 
 

110 lines
2.7 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. Honeypot string `json:"fullname" schema:"fullname"`
  29. Normalize bool `json:"normalize" schema:"normalize"`
  30. Signup bool `json:"signup" schema:"signup"`
  31. }
  32. // AuthUser contains information for a newly authenticated user (either
  33. // from signing up or logging in).
  34. AuthUser struct {
  35. AccessToken string `json:"access_token,omitempty"`
  36. Password string `json:"password,omitempty"`
  37. User *User `json:"user"`
  38. // Verbose user data
  39. Posts *[]PublicPost `json:"posts,omitempty"`
  40. Collections *[]Collection `json:"collections,omitempty"`
  41. }
  42. // User is a consistent user object in the database and all contexts (auth
  43. // and non-auth) in the API.
  44. User struct {
  45. ID int64 `json:"-"`
  46. Username string `json:"username"`
  47. HashedPass []byte `json:"-"`
  48. HasPass bool `json:"has_pass"`
  49. Email zero.String `json:"email"`
  50. Created time.Time `json:"created"`
  51. clearEmail string `json:"email"`
  52. }
  53. userMeStats struct {
  54. TotalCollections, TotalArticles, CollectionPosts uint64
  55. }
  56. ExportUser struct {
  57. *User
  58. Collections *[]CollectionObj `json:"collections"`
  59. AnonymousPosts []PublicPost `json:"posts"`
  60. }
  61. PublicUser struct {
  62. Username string `json:"username"`
  63. }
  64. )
  65. // EmailClear decrypts and returns the user's email, caching it in the user
  66. // object.
  67. func (u *User) EmailClear(keys *keychain) string {
  68. if u.clearEmail != "" {
  69. return u.clearEmail
  70. }
  71. if u.Email.Valid && u.Email.String != "" {
  72. email, err := data.Decrypt(keys.emailKey, []byte(u.Email.String))
  73. if err != nil {
  74. log.Error("Error decrypting user email: %v", err)
  75. } else {
  76. u.clearEmail = string(email)
  77. return u.clearEmail
  78. }
  79. }
  80. return ""
  81. }
  82. // Cookie strips down an AuthUser to contain only information necessary for
  83. // cookies.
  84. func (u User) Cookie() *User {
  85. u.HashedPass = []byte{}
  86. return &u
  87. }
  88. func (u *User) IsAdmin() bool {
  89. // TODO: get this from database
  90. return u.ID == 1
  91. }