2018-10-17 02:30:38 +02:00
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/guregu/null/zero"
|
|
|
|
"github.com/writeas/web-core/data"
|
|
|
|
"github.com/writeas/web-core/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2018-11-08 07:31:01 +01:00
|
|
|
userCredentials struct {
|
|
|
|
Alias string `json:"alias" schema:"alias"`
|
|
|
|
Pass string `json:"pass" schema:"pass"`
|
|
|
|
Email string `json:"email" schema:"email"`
|
|
|
|
Web bool `json:"web" schema:"-"`
|
|
|
|
To string `json:"-" schema:"to"`
|
|
|
|
|
|
|
|
EmailLogin bool `json:"via_email" schema:"via_email"`
|
|
|
|
}
|
|
|
|
|
|
|
|
userRegistration struct {
|
|
|
|
userCredentials
|
|
|
|
Honeypot string `json:"fullname" schema:"fullname"`
|
|
|
|
Normalize bool `json:"normalize" schema:"normalize"`
|
|
|
|
Signup bool `json:"signup" schema:"signup"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthUser contains information for a newly authenticated user (either
|
|
|
|
// from signing up or logging in).
|
|
|
|
AuthUser struct {
|
|
|
|
AccessToken string `json:"access_token,omitempty"`
|
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
User *User `json:"user"`
|
|
|
|
|
|
|
|
// Verbose user data
|
|
|
|
Posts *[]PublicPost `json:"posts,omitempty"`
|
|
|
|
Collections *[]Collection `json:"collections,omitempty"`
|
|
|
|
}
|
|
|
|
|
2018-10-17 02:30:38 +02:00
|
|
|
// User is a consistent user object in the database and all contexts (auth
|
|
|
|
// and non-auth) in the API.
|
|
|
|
User struct {
|
|
|
|
ID int64 `json:"-"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
HashedPass []byte `json:"-"`
|
|
|
|
HasPass bool `json:"has_pass"`
|
|
|
|
Email zero.String `json:"email"`
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
|
|
|
|
clearEmail string `json:"email"`
|
|
|
|
}
|
2018-11-08 07:31:01 +01:00
|
|
|
|
|
|
|
userMeStats struct {
|
|
|
|
TotalCollections, TotalArticles, CollectionPosts uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
ExportUser struct {
|
|
|
|
*User
|
|
|
|
Collections *[]CollectionObj `json:"collections"`
|
|
|
|
AnonymousPosts []PublicPost `json:"posts"`
|
|
|
|
}
|
|
|
|
|
|
|
|
PublicUser struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
}
|
2018-10-17 02:30:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// EmailClear decrypts and returns the user's email, caching it in the user
|
|
|
|
// object.
|
|
|
|
func (u *User) EmailClear(keys *keychain) string {
|
|
|
|
if u.clearEmail != "" {
|
|
|
|
return u.clearEmail
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Email.Valid && u.Email.String != "" {
|
|
|
|
email, err := data.Decrypt(keys.emailKey, []byte(u.Email.String))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error decrypting user email: %v", err)
|
|
|
|
} else {
|
|
|
|
u.clearEmail = string(email)
|
|
|
|
return u.clearEmail
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cookie strips down an AuthUser to contain only information necessary for
|
|
|
|
// cookies.
|
|
|
|
func (u User) Cookie() *User {
|
|
|
|
u.HashedPass = []byte{}
|
|
|
|
|
|
|
|
return &u
|
|
|
|
}
|
2018-11-19 02:18:22 +01:00
|
|
|
|
|
|
|
func (u *User) IsAdmin() bool {
|
|
|
|
// TODO: get this from database
|
|
|
|
return u.ID == 1
|
|
|
|
}
|