-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
333 lines (281 loc) · 8.06 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package main
import (
"errors"
"fmt"
"strings"
"time"
"github.com/jinzhu/gorm"
"github.com/satori/go.uuid"
"github.com/smtc/glog"
)
type User struct {
//Id int64 `json:"id"`
Id string `gorm:"primary_key"` //`sql:"size:64;unique_index" json:"object_id"`
SiteId int64 `json:"site_id"`
Name string `sql:"size:40;unique_index" json:"name"`
Email string `sql:"size:100;index" json:"email"`
Msisdn string `sql:"size:20;index" json:"msisdn"`
Password string `sql:"size:200" json:"-"`
MainId int64 `json:"main_id"` // 如果不是主用户, 主用户id;否则为0
Approved bool `json:"approved"`
Activing bool `json:"acitiving"`
Banned bool `json:"banned"`
ApprovedBy string `sql:"size:20" json:"approved_by"`
IpAddr string `sql:"size:30" json:"ipaddr"`
DaysLogin int `json:"days_login"`
Birthday string `json:"birthday"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ApprovedAt time.Time `json:"approved_at"`
LastLogin time.Time `json:"last_login"`
LastPost int64 `json:"last_post"`
Avatar string `sql:"size:120" json:"avatar"`
City string `sql:"size:40" json:"city"`
Blog string `sql:"size:200"`
Qq string `sql:"size:40"`
Weibo string `sql:"size:40"`
Weixin string `sql:"size:40"`
Goldcoins int64 `json:"gold_coins"` // 金币
SilverCoins int64 `json:"silver_coins"` // 银币
Coppercoins int64 `json:"copper_coins"` // 铜币
Reputation int `json:"reputation"` // 声望值
Credits int `json:"credits"` // 信用等级
Experience int `json:"experience"` // 经验值
Activation int `json:"activation"` // 活跃度
Redcards int `json:"red_cards"`
Yellowcards int `json:"yellow_cards"`
Notifications int `json:"notifications"`
// 第三方登录的数据
ProviderName string `json:"provider_name"` // 第三方登录名称
ProviderId string `json:"provider_id"` // 用户在第三方的唯一id
// user capability
capability map[string]bool `sql:"-"`
capParsed bool `sql:"-"`
roles string `sql:"type:text"` // 这是一个string数组, 以,分割
// other meta data
metaData map[string]interface{} `sql:"-"`
}
// 用户收藏,喜欢,反对,打赏等
type UserAction struct {
Id string `gorm:"primary_key" sql:"size:120" json:"id"`
UserId string `sql:"size:120" json:"user_id"`
Action string `sql:"size:40" json:"action"`
TargetTyp string `sql:"size:40" json:"target_typ"`
TargetId string `sql:"size:120" json:"target_id"`
Data string `sql:"size:200" json:"data"`
CreatedAt time.Time `json:"created_at"`
}
// 创建用户
// 必须字段:
// id: uuid, if empty, create it
// name: username, must be uniqe
func createProviderUser(u *User) (err error) {
var exist bool
u.Id = strings.TrimSpace(u.Id)
if u.Id != "" && validUUID(u.Id) == false {
err = errors.New("invliad uuid")
return
}
u.Name = strings.TrimSpace(u.Name)
u.Email = strings.TrimSpace(u.Email)
u.ProviderName = strings.TrimSpace(u.ProviderName)
u.ProviderId = strings.TrimSpace(u.ProviderId)
if u.ProviderId == "" || u.ProviderName == "" {
err = errors.New("provider name and provider id should NOT be empty.")
return
}
if u.Id == "" {
u.Id = uuid.NewV4().String()
fmt.Println("Id:", u.Id)
} else {
if exist, err = idHasExist(u.Id); exist || err != nil {
err = fmt.Errorf("create user failed: id %s exist or db query failed: %s", u.Id, err.Error())
return
}
}
if u.Name == "" {
err = errors.New("create user failed: name should not be empty.")
return
} else if exist, err = nameHasExist(u.Name); exist || err != nil {
err = fmt.Errorf("create user failed: name %s exist or db query failed: %s", u.Name, err.Error())
return
}
if u.Email != "" {
if exist, err = emailHasExist(u.Email); exist || err != nil {
err = fmt.Errorf("create user failed: email %s exist or db query failed: %s", u.Email, err.Error())
return
}
}
u.MainId = 0
u.Approved = true
u.Activing = true
u.Banned = false
u.ApprovedBy = "auto"
u.IpAddr = ""
u.DaysLogin = 0
u.Birthday = ""
now := time.Now()
u.CreatedAt = now
u.UpdatedAt = now
u.ApprovedAt = now
u.LastPost = 0
u.Goldcoins, u.SilverCoins, u.Coppercoins = 0, 0, 0
u.Reputation, u.Credits, u.Experience, u.Activation = 0, 0, 0, 0
u.Redcards, u.Yellowcards, u.Notifications = 0, 0, 0
err = db.Create(u).Error
return nil
}
// 根据ObjectId来查找用户
func getUserById(oid string) *User {
var u User
if err := db.Where("id=?", oid).First(&u).Error; err != nil {
if err != gorm.RecordNotFound {
glog.Error("getUserById failed: oid=%s err=%v\n", oid, err)
}
return nil
}
return &u
}
func getUserByEmail(email string) (*User, error) {
var u User
if err := db.Where("email=?", email).First(&u).Error; err != nil {
if err != gorm.RecordNotFound {
glog.Error("getUserByEmail failed: email=%s err=%v\n", email, err)
}
return nil, err
}
return &u, nil
}
// 根据用户名查找用户
//
func getUserByName(name string) *User {
var u User
if err := db.Where("name=?", name).First(&u).Error; err != nil {
if err != gorm.RecordNotFound {
glog.Error("getUserByName failed: name=%s err=%v\n", name, err)
}
return nil
}
return &u
}
// 根据provider name和provider id来查找该用户是否存在
func providerUserHasExist(pname, pid string) (bool, *User, error) {
var (
err error
user User
)
err = db.Where("provider_name=? AND provider_id=?", pname, pid).Find(&user).Error
if err == gorm.RecordNotFound {
return false, nil, nil
}
if err != nil {
return false, nil, err
}
return true, &user, nil
}
// id是否已经存在
func idHasExist(id string) (exist bool, err error) {
var u User
err = db.Where("id=?", id).Find(&u).Error
if err == gorm.RecordNotFound {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
// email是否已经存在
func emailHasExist(email string) (exist bool, err error) {
var u User
err = db.Where("email=?", email).Find(&u).Error
if err == gorm.RecordNotFound {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
// 名字是否存在
func nameHasExist(name string) (exist bool, err error) {
var u User
err = db.Where("name=?", name).Find(&u).Error
if err == gorm.RecordNotFound {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
// 更新用户资料
// 目前只有city和avatar可以变更
// 哪些资料可以变更?哪些资料不能变更?
func updateUserById(u *User) (err error) {
var (
ou *User
nu User
changed bool
)
ou = getUserById(u.Id)
if ou == nil {
return errors.New("Not found user by id " + u.Id)
}
if u.Avatar != "" && u.Avatar != ou.Avatar {
nu.Avatar = u.Avatar
changed = true
}
if u.City != "" {
nu.City = u.City
changed = true
}
if changed {
err = db.Model(ou).Updates(nu).Error
}
return err
}
func getPostsByUserAction(u *User, action string, start, count int) ([]*Post, error) {
var (
err error
actions []UserAction
posts []*Post
)
err = db.Where("user_id=? and action=?", u.Id, action).Offset(start).Limit(count).Find(&actions).Error
if err != nil {
if err == gorm.RecordNotFound {
return []*Post{}, nil
}
return []*Post{}, err
}
for _, a := range actions {
var p = new(Post)
p.Id = a.TargetId
err = db.Find(p).Error
if err != nil {
glog.Error("Find Post by Id %s failed: %s\n", p.Id, err.Error())
continue
}
posts = append(posts, p)
}
return posts, nil
}
// 2015-08-18 Todo:
// user的动作,例如收藏,star,up,down,pay等
// 根据用户的角色,分析、填充用户的权限
func (u *User) parseUserCap() {
// todo: parse user's role, fill user capability
u.capability = make(map[string]bool)
// 2015-08-29
// 为了测试:设置用户administrator的权限
if u.Id == "administrator" {
u.capability["create_taxonomy"] = true
u.capability["delete_others_posts"] = true
}
u.capParsed = true
}
// 创建管理员账户
func createAdministrator() {
}
func grantUserAdminitrator(u *User) {
}