-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
177 lines (144 loc) · 3.45 KB
/
models.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package auth
import (
"crypto/md5"
"encoding/hex"
"fmt"
"strings"
"time"
"github.com/google/uuid"
"github.com/kapmahc/fly/web"
)
const (
// RoleAdmin admin role
RoleAdmin = "admin"
// RoleRoot root role
RoleRoot = "root"
// UserTypeEmail email user
UserTypeEmail = "email"
// DefaultResourceType default resource type
DefaultResourceType = "-"
// DefaultResourceID default resourc id
DefaultResourceID = 0
)
// User user
type User struct {
web.Model
Name string `json:"name"`
Email string `json:"email"`
UID string `json:"uid"`
Password []byte `json:"-"`
ProviderID string `json:"-"`
ProviderType string `json:"providerType"`
Home string `json:"home"`
Logo string `json:"logo"`
SignInCount uint `json:"signInCount"`
LastSignInAt *time.Time `json:"lastSignInAt"`
LastSignInIP string `json:"lastSignInIp"`
CurrentSignInAt *time.Time `json:"currentSignInAt"`
CurrentSignInIP string `json:"currentSignInIp"`
ConfirmedAt *time.Time `json:"confirmedAt"`
LockedAt *time.Time `json:"lockedAt"`
Logs []Log `json:"-"`
}
// TableName table name
func (User) TableName() string {
return "users"
}
// IsConfirm is confirm?
func (p *User) IsConfirm() bool {
return p.ConfirmedAt != nil
}
// IsLock is lock?
func (p *User) IsLock() bool {
return p.LockedAt != nil
}
//SetGravatarLogo set logo by gravatar
func (p *User) SetGravatarLogo() {
buf := md5.Sum([]byte(strings.ToLower(p.Email)))
p.Logo = fmt.Sprintf("https://gravatar.com/avatar/%s.png", hex.EncodeToString(buf[:]))
}
//SetUID generate uid
func (p *User) SetUID() {
p.UID = uuid.New().String()
}
func (p User) String() string {
return fmt.Sprintf("%s<%s>", p.Name, p.Email)
}
// Attachment attachment
type Attachment struct {
web.Model
Title string `json:"title"`
URL string `json:"url"`
Length int64 `json:"length"`
MediaType string `json:"mediaType"`
ResourceType string `json:"resourceType"`
ResourceID uint `json:"resourceId"`
UserID uint `json:"userId"`
User User `json:"-"`
}
// TableName table name
func (Attachment) TableName() string {
return "attachments"
}
// IsPicture is picture?
func (p *Attachment) IsPicture() bool {
return strings.HasPrefix(p.MediaType, "image/")
}
// Log log
type Log struct {
ID uint `json:"id"`
Message string `json:"message"`
Type string `json:"type"`
CreatedAt time.Time `json:"createdAt"`
IP string `json:"ip"`
UserID uint `json:"userId"`
User User `json:"-"`
}
// TableName table name
func (Log) TableName() string {
return "logs"
}
// Policy policy
type Policy struct {
web.Model
StartUp time.Time
ShutDown time.Time
UserID uint
User User
RoleID uint
Role Role
}
//Enable is enable?
func (p *Policy) Enable() bool {
now := time.Now()
return now.After(p.StartUp) && now.Before(p.ShutDown)
}
// TableName table name
func (Policy) TableName() string {
return "policies"
}
// Role role
type Role struct {
web.Model
Name string
ResourceID uint
ResourceType string
}
// TableName table name
func (Role) TableName() string {
return "roles"
}
func (p Role) String() string {
return fmt.Sprintf("%s@%s://%d", p.Name, p.ResourceType, p.ResourceID)
}
// Vote vote
type Vote struct {
web.Model
Point int
ResourceID uint
ResourceType string
}
// TableName table name
func (Vote) TableName() string {
return "votes"
}