-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
177 lines (168 loc) · 4.38 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
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 vbcore
// User defines a user with all it's properties. All vikebot code
// should be able to access all information from the database through
// this struct
type User struct {
ID *int
Permission *int
PermissionString *string
Username *string
Name *string
Emails []Email
Bio *string
Location *string
Web []string
Company *string
Social map[string]string
OAuth map[string]string
}
// Validate checks whether a `vbcore.User` struct, passed from clients, is in
// a valid state or not.
func (u *User) Validate() (safeUser *SafeUser, valid bool) {
// It's not allowed to have the following attributes
if u.ID != nil {
return nil, false
}
if u.Permission != nil {
return nil, false
}
if u.PermissionString != nil {
return nil, false
}
if len(u.OAuth) != 0 {
return nil, false
}
// You must have these attributes
if u.Username == nil || len(*u.Username) > 32 {
return nil, false
}
if u.Name == nil || len(*u.Name) > 32 {
return nil, false
}
if len(u.Emails) == 0 {
return nil, false
}
for _, i := range u.Emails {
if len(i.Email) > 64 {
return nil, false
}
if i.Status != EmailLinked && i.Status != EmailVerified && i.Status != EmailPrimary {
return nil, false
}
}
if u.Bio == nil || len(*u.Bio) > 1024 {
return nil, false
}
if u.Location == nil || len(*u.Location) > 64 {
return nil, false
}
if u.Company == nil || len(*u.Company) > 64 {
return nil, false
}
for _, i := range u.Web {
if len(i) > 128 {
return nil, false
}
}
// User is valid state -> convert and return
return u.SafeUser(), true
}
// SafeUser converts a User to a SafeUser struct. All properties that
// aren't set in the User struct will have the type's default value
// in the SafeUser struct.
func (u *User) SafeUser() *SafeUser {
su := SafeUser{}
if u.ID != nil {
su.ID = *u.ID
}
if u.Permission != nil {
su.Permission = *u.Permission
}
if u.PermissionString != nil {
su.PermissionString = *u.PermissionString
}
if u.Username != nil {
su.Username = *u.Username
}
if u.Name != nil {
su.Name = *u.Name
}
if u.Emails != nil {
su.Emails = u.Emails
}
if u.Bio != nil {
su.Bio = *u.Bio
}
if u.Location != nil {
su.Location = *u.Location
}
if u.Web != nil {
su.Web = u.Web
}
if u.Company != nil {
su.Company = *u.Company
}
if u.Social != nil {
su.Social = u.Social
}
if u.OAuth != nil {
su.OAuth = u.OAuth
}
return &su
}
// SafeUser equals to User itself with one exception: all variables are
// already value fields (and not pointers) and there aren't any json
// declarations.
type SafeUser struct {
ID int `json:"id"`
Permission int `json:"permission"`
PermissionString string `json:"permission_string"`
Username string `json:"username"`
Name string `json:"name"`
Emails []Email `json:"emails"`
Bio string `json:"bio"`
Location string `json:"location"`
Web []string `json:"web"`
Company string `json:"company"`
Social map[string]string `json:"social"`
OAuth map[string]string `json:"-"`
}
// User converts the SafeUser struct to a User struct.
func (su *SafeUser) User() *User {
return &User{
ID: &su.ID,
Permission: &su.Permission,
PermissionString: &su.PermissionString,
Username: &su.Username,
Name: &su.Name,
Emails: su.Emails,
Bio: &su.Bio,
Location: &su.Location,
Web: su.Web,
Company: &su.Company,
Social: su.Social,
OAuth: su.OAuth,
}
}
// MakePublic converts the user into it's public version. Everyone is allowed
// to access this version without authentication. Currently only emails which
// are ether private or not verified are stripped away.
func (su *SafeUser) MakePublic() {
emails := []Email{}
for _, v := range su.Emails {
if v.Public && (v.IsVerified() || v.IsPrimary()) {
emails = append(emails, v)
}
}
su.Emails = emails
}
// PrimaryEmail returns the primary email of this user. If there isn't anyone
// `nil` is returned.
func (su *SafeUser) PrimaryEmail() *Email {
for _, v := range su.Emails {
if v.IsPrimary() {
return &v
}
}
return nil
}