-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathuser.go
175 lines (138 loc) · 4.04 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
package kilonova
import (
"log/slog"
"time"
)
type PreferredTheme string
const (
PreferredThemeNone = ""
PreferredThemeLight = "light"
PreferredThemeDark = "dark"
)
type UserBrief struct {
ID int `json:"id"`
Name string `json:"name"`
Admin bool `json:"admin"`
Proposer bool `json:"proposer"`
DisplayName string `json:"display_name"`
Generated bool `json:"generated"`
// Do not JSON encode, for now.
// Used for logging
DiscordID *string `json:"-"`
}
func (u *UserBrief) LogValue() slog.Value {
if u == nil {
return slog.Value{}
}
return slog.GroupValue(slog.Int("id", u.ID), slog.String("name", u.Name))
}
func (u *UserBrief) IsAuthed() bool {
return u != nil && u.ID != 0
}
func (u *UserBrief) IsAdmin() bool {
if !u.IsAuthed() {
return false
}
return u.Admin
}
func (u *UserBrief) IsProposer() bool {
if !u.IsAuthed() {
return false
}
return u.Admin || u.Proposer
}
func (u *UserBrief) AppropriateName() string {
if len(u.DisplayName) == 0 {
return u.Name
}
return u.DisplayName
}
type UserFull struct {
UserBrief
Bio string `json:"bio,omitempty"`
Email string `json:"email,omitempty"`
VerifiedEmail bool `json:"verified_email"`
PreferredLanguage string `json:"preferred_language"`
PreferredTheme PreferredTheme `json:"preferred_theme"`
EmailVerifResent time.Time `json:"-"`
CreatedAt time.Time `json:"created_at"`
LockedLogin bool `json:"locked_login"`
NameChangeForced bool `json:"name_change_forced"`
DiscordID *string `json:"discord_id"`
AvatarType string `json:"avatar_type"`
}
func (uf *UserFull) Brief() *UserBrief {
if uf == nil {
return nil
}
return &uf.UserBrief
}
// UserFilter is the struct with all filterable fields on the user
// It also provides a Limit and Offset field, for pagination
type UserFilter struct {
ID *int `json:"id"`
IDs []int `json:"ids"`
// Name is case insensitive
Name *string `json:"name"`
Email *string `json:"email"`
// For user filtering
FuzzyName *string `json:"name_fuzzy"`
Admin *bool `json:"admin"`
Proposer *bool `json:"proposer"`
// For registrations
ContestID *int `json:"contest_id"`
// For filtering in leaderboards
Generated *bool `json:"generated"`
// For session recognition
SessionID *string `json:"session_id"`
Limit int `json:"limit"`
Offset int `json:"offset"`
}
// UserUpdate is the struct with updatable fields that can be easily changed.
// Stuff like admin and proposer should be updated through their dedicated
// SudoAPI methods
type UserUpdate struct {
DisplayName *string `json:"display_name"`
Bio *string `json:"bio"`
AvatarType *string `json:"avatar_type"`
PreferredLanguage string `json:"-"`
PreferredTheme PreferredTheme `json:"-"`
}
// UserFullUpdate is the struct with all updatable fields on the user. Internal use only
type UserFullUpdate struct {
UserUpdate
Name *string `json:"name"`
Email *string `json:"email"`
Admin *bool `json:"admin"`
Proposer *bool `json:"proposer"`
LockedLogin *bool `json:"locked_login"`
NameChangeRequired *bool `json:"name_change_required"`
SetDiscordID bool `json:"set_discord_id"`
DiscordID *string `json:"discord_id"`
VerifiedEmail *bool `json:"verified_email"`
EmailVerifSentAt *time.Time `json:"-"`
}
type UsernameChange struct {
UserID int `json:"user_id" db:"user_id"`
Name string `json:"name" db:"name"`
ChangedAt time.Time `json:"changed_at" db:"changed_at"`
}
//func HashPassword(password string) (string, error) {
// hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
// if err != nil {
// return "", err
// }
// return string(hash), err
//}
//
//func CheckPwdHash(password, hash string) bool {
// err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
// if errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) {
// return false
// }
// if err != nil {
// slog.WarnContext(context.Background(), "Bcrypt failure", slog.Any("err", err)
// return false
// }
// return true
//}