forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct.go
171 lines (145 loc) · 3.77 KB
/
struct.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
package auth
import (
"chat/globals"
"chat/utils"
"database/sql"
"time"
)
type User struct {
ID int64 `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
BindID int64 `json:"bind_id"`
Password string `json:"password"`
Token string `json:"token"`
Admin bool `json:"is_admin"`
Level int `json:"level"`
Subscription *time.Time `json:"subscription"`
Banned bool `json:"is_banned"`
}
func GetUserById(db *sql.DB, id int64) *User {
var user User
if err := globals.QueryRowDb(db, "SELECT id, username FROM auth WHERE id = ?", id).Scan(&user.ID, &user.Username); err != nil {
return nil
}
return &user
}
func GetUserByName(db *sql.DB, username string) *User {
var user User
if err := globals.QueryRowDb(db, "SELECT id, username FROM auth WHERE username = ?", username).Scan(&user.ID, &user.Username); err != nil {
return nil
}
return &user
}
func GetUserByEmail(db *sql.DB, email string) *User {
var user User
if err := globals.QueryRowDb(db, "SELECT id, username FROM auth WHERE email = ?", email).Scan(&user.ID, &user.Username); err != nil {
return nil
}
return &user
}
func GetId(db *sql.DB, user *User) int64 {
if user == nil {
return -1
}
return user.GetID(db)
}
func (u *User) IsBanned(db *sql.DB) bool {
if u.Banned {
return true
}
var banned sql.NullBool
if err := globals.QueryRowDb(db, "SELECT is_banned FROM auth WHERE username = ?", u.Username).Scan(&banned); err != nil {
return false
}
u.Banned = banned.Valid && banned.Bool
return u.Banned
}
func (u *User) IsAdmin(db *sql.DB) bool {
if u.Admin {
return true
}
var admin sql.NullBool
if err := globals.QueryRowDb(db, "SELECT is_admin FROM auth WHERE username = ?", u.Username).Scan(&admin); err != nil {
return false
}
u.Admin = admin.Valid && admin.Bool
return u.Admin
}
func (u *User) GetID(db *sql.DB) int64 {
if u.ID > 0 {
return u.ID
}
if err := globals.QueryRowDb(db, "SELECT id FROM auth WHERE username = ?", u.Username).Scan(&u.ID); err != nil {
return 0
}
return u.ID
}
func (u *User) HitID() int64 {
return u.ID
}
func (u *User) GetEmail(db *sql.DB) string {
if len(u.Email) > 0 {
return u.Email
}
var email sql.NullString
if err := globals.QueryRowDb(db, "SELECT email FROM auth WHERE username = ?", u.Username).Scan(&email); err != nil {
return ""
}
u.Email = email.String
return u.Email
}
func IsUserExist(db *sql.DB, username string) bool {
var count int
if err := globals.QueryRowDb(db, "SELECT COUNT(*) FROM auth WHERE username = ?", username).Scan(&count); err != nil {
return false
}
return count > 0
}
func IsEmailExist(db *sql.DB, email string) bool {
var count int
if err := globals.QueryRowDb(db, "SELECT COUNT(*) FROM auth WHERE email = ?", email).Scan(&count); err != nil {
return false
}
return count > 0
}
func getMaxBindId(db *sql.DB) int64 {
var max int64
if err := globals.QueryRowDb(db, "SELECT MAX(bind_id) FROM auth").Scan(&max); err != nil {
return 0
}
return max
}
func GetGroup(db *sql.DB, user *User) string {
if user == nil {
return globals.AnonymousType
}
level := user.GetSubscriptionLevel(db)
switch level {
case 0:
return globals.NormalType
case 1:
return globals.BasicType
case 2:
return globals.StandardType
case 3:
return globals.ProType
default:
return globals.NormalType
}
}
func HitGroup(db *sql.DB, user *User, group string) bool {
if group == globals.AdminType {
return user != nil && user.IsAdmin(db)
}
return GetGroup(db, user) == group
}
func HitGroups(db *sql.DB, user *User, groups []string) bool {
if utils.Contains(globals.AdminType, groups) {
if user != nil && user.IsAdmin(db) {
return true
}
}
group := GetGroup(db, user)
return utils.Contains(group, groups)
}