generated from cjlapao/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
82 lines (72 loc) · 2.14 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package models
import (
cryptorand "github.com/cjlapao/common-go-cryptorand"
"github.com/cjlapao/common-go/constants"
"github.com/cjlapao/common-go/security"
"github.com/cjlapao/common-go/validators"
)
// User entity
type User struct {
ID string `json:"id" bson:"_id"`
Email string `json:"email" bson:"email"`
EmailVerified bool `json:"emailVerified" bson:"emailVerified"`
Username string `json:"username" bson:"username"`
FirstName string `json:"firstName" bson:"firstName"`
LastName string `json:"lastName" bson:"lastName"`
DisplayName string `json:"displayName" bson:"displayName"`
Password string `json:"password" bson:"password"`
Token string `json:"-" bson:"-"`
RefreshToken string `json:"refreshToken" bson:"refreshToken"`
RecoveryToken string `json:"recoveryToken" bson:"recoveryToken"`
EmailVerifyToken string `json:"emailVerifyToken" bson:"emailVerifyToken"`
InvalidAttempts int `json:"invalidAttempts" bson:"invalidAttempts"`
Blocked bool `json:"blocked" bson:"blocked"`
BlockedUntil string `json:"blockedUntil" bson:"blockedUntil"`
Roles []UserRole `json:"roles" bson:"roles"`
Claims []UserClaim `json:"claims" bson:"claims"`
}
func NewUser() *User {
user := User{
ID: cryptorand.GetRandomString(constants.ID_SIZE),
}
user.Roles = make([]UserRole, 0)
user.Claims = make([]UserClaim, 0)
return &user
}
func (u User) GetHashedPassword() string {
if u.Password != "" {
return u.HashPassword(u.Password)
}
return ""
}
func (u User) HashPassword(password string) string {
if password != "" {
return security.SHA256Encode(password)
}
return ""
}
func (u User) IsValid() bool {
if u.ID == "" {
return false
}
if u.Username == "" {
return false
}
if u.Email == "" {
return false
}
isEmailValid := validators.ValidateEmailAddress(u.Email)
if !isEmailValid {
return false
}
if u.Password == "" {
return false
}
// if len(u.Claims) == 0 {
// return false
// }
if len(u.Roles) == 0 {
return false
}
return true
}