This repository has been archived by the owner on Nov 17, 2017. It is now read-only.
/
account.go
597 lines (507 loc) · 17.3 KB
/
account.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
package handlers
import (
"errors"
. "github.com/mvader/sunglasses/error"
"github.com/mvader/sunglasses/middleware"
"github.com/mvader/sunglasses/models"
"github.com/mvader/sunglasses/modules/timeline"
"github.com/mvader/sunglasses/modules/upload"
"github.com/mvader/sunglasses/util"
"labix.org/v2/mgo/bson"
"net/http"
"net/mail"
"os"
"regexp"
"strconv"
"strings"
)
// CreateAccount is a handler in charge of creating an user account. After a successful registration the user will be automatically logged in.
//
// This handler needs the following parameters:
// - username: The username provided by the user
// - password: The password chosen by the user
// - password_repeat: The password, again
// - recovery_method (optional): The recovery method chosen by the user (look at the values for the type RecoveryMethod on the models package)
// - recovery_answer and recovery_question (optional): A recovery answer and question if the provided recovery method is question.
func CreateAccount(c middleware.Context) {
// TODO: Max retries
var (
username = c.Form("username")
password = c.Form("password")
passwordRepeat = c.Form("password_repeat")
question, answer, email string
errorList = make([]string, 0)
codeList = make([]int, 0)
responseStatus = 400
)
recoveryMethod, err := strconv.ParseInt(c.Form("recovery_method"), 10, 0)
if err != nil {
recoveryMethod = models.RecoveryNone
}
switch models.RecoveryMethod(recoveryMethod) {
case models.RecoveryNone:
break
case models.RecoverByEMail:
email = c.Form("email")
if _, err := mail.ParseAddress(email); err != nil {
errorList = append(errorList, MsgInvalidEmail)
codeList = append(codeList, CodeInvalidEmail)
}
break
case models.RecoverByQuestion:
question = c.Form("recovery_question")
answer = c.Form("recovery_answer")
if question == "" || answer == "" {
errorList = append(errorList, MsgInvalidRecoveryQuestion)
codeList = append(codeList, CodeInvalidRecoveryQuestion)
}
break
default:
errorList = append(errorList, MsgInvalidRecoveryMethod)
codeList = append(codeList, CodeInvalidRecoveryMethod)
}
reg, err := regexp.Compile("^[a-zA-Z_0-9]{2,30}$")
if err != nil {
errorList = append(errorList, MsgInvalidUsername)
codeList = append(codeList, CodeInvalidUsername)
}
if !reg.MatchString(username) {
errorList = append(errorList, MsgInvalidUsername)
codeList = append(codeList, CodeInvalidUsername)
}
if util.Strlen(password) < 6 {
errorList = append(errorList, MsgPasswordLength)
codeList = append(codeList, CodePasswordLength)
}
if password != passwordRepeat {
errorList = append(errorList, MsgPasswordMatch)
codeList = append(codeList, CodePasswordMatch)
}
if len(errorList) == 0 {
user := models.NewUser()
user.Username = username
user.SetPassword(password)
user.Settings.PasswordRecoveryMethod = models.RecoveryMethod(recoveryMethod)
switch recoveryMethod {
case models.RecoverByEMail:
if err := user.SetEmail(email); err != nil {
user.Settings.PasswordRecoveryMethod = models.RecoveryNone
}
break
case models.RecoverByQuestion:
user.Settings.RecoveryQuestion = question
user.Settings.RecoveryAnswer = answer
break
}
if err = user.Save(c.Conn); err == nil {
c.Request.PostForm.Add("token_type", "session")
c.Request.PostForm.Add("username", user.Username)
c.Request.PostForm.Add("password", password)
GetUserToken(c)
return
} else {
if err.Error() == "username already in use" {
errorList = append(errorList, MsgUsernameTaken)
codeList = append(codeList, CodeInvalidRecoveryQuestion)
} else {
responseStatus = 500
errorList = append(errorList, MsgUnexpected)
codeList = append(codeList, CodeUnexpected)
}
}
}
c.Errors(responseStatus, codeList, errorList)
}
// IsUsernameTaken checks if the given username is already taken
func IsUsernameTaken(c middleware.Context) {
count, _ := c.Count("users", bson.M{"username_lower": strings.ToLower(c.Form("username"))})
c.Success(200, map[string]interface{}{"taken": count > 0})
}
// GetAccountInfo retrieves the info of the user
func GetAccountInfo(c middleware.Context) {
c.Success(200, map[string]interface{}{
"account_info": c.User.Info,
})
}
// UpdateAccountInfo updates the user's information
func UpdateAccountInfo(c middleware.Context) {
info := c.User.Info
// Check user websites
if v, ok := c.Request.PostForm["websites"]; ok {
sites := make([]string, 0, len(v))
for _, site := range v {
if !strings.HasPrefix(site, "http://") && !strings.HasPrefix(site, "https://") {
site = "http://" + site
}
if !util.IsValidURL(site) || util.Strlen(site) > 500 {
c.Error(400, CodeInvalidWebsites, MsgInvalidWebsites)
return
}
sites = append(sites, site)
}
info.Websites = sites
}
// Check gender
if gender := c.Form("gender"); gender != "" {
gender, err := strconv.ParseInt(gender, 10, 8)
if err == nil {
if gender == models.Male || gender == models.Female || gender == models.Other {
info.Gender = models.Gender(gender)
} else {
err = errors.New("invalid gender")
}
}
if err != nil {
c.Error(400, CodeInvalidGender, MsgInvalidGender)
return
}
}
// Check status
if status := c.Form("status"); status != "" {
status, err := strconv.ParseInt(status, 10, 8)
if err == nil {
if status >= 0 && status <= 4 {
info.Status = models.UserStatus(status)
} else {
err = errors.New("invalid status")
}
}
if err != nil {
c.Error(400, CodeInvalidStatus, MsgInvalidStatus)
return
}
}
for _, field := range []string{"work", "education", "hobbies", "books", "movies", "tv", "about"} {
if util.Strlen(c.Form(field)) > 500 {
c.Error(400, CodeInvalidInfoLength, MsgInvalidInfoLength)
return
}
}
// The rest of the fields need no validation since their length was already checked before
info.Work = strings.TrimSpace(c.Form("work"))
info.Education = strings.TrimSpace(c.Form("education"))
info.Hobbies = strings.TrimSpace(c.Form("hobbies"))
info.Books = strings.TrimSpace(c.Form("books"))
info.Movies = strings.TrimSpace(c.Form("movies"))
info.TV = strings.TrimSpace(c.Form("tv"))
info.About = strings.TrimSpace(c.Form("about"))
// Update the user info in the database
c.User.Info = info
if err := c.User.Save(c.Conn); err != nil {
c.Error(500, CodeUnexpected, MsgUnexpected)
return
}
c.Success(200, map[string]interface{}{
"message": "User info updated successfully",
})
}
// GetAccountSettings retrieves the settings of the user
func GetAccountSettings(c middleware.Context) {
c.Success(200, map[string]interface{}{
"account_info": c.User.Info,
})
}
// UpdateAccountData updates some data about the user like the names and the preferred language
func UpdateAccountData(c middleware.Context) {
var (
publicName = c.Form("public_name")
privateName = c.Form("private_name")
preferredLang = c.Form("preferred_lang")
)
if util.Strlen(publicName) > 35 || util.Strlen(privateName) > 35 {
c.Error(400, CodeInvalidData, MsgInvalidData)
return
}
// TODO: Check lang is available
c.User.PublicName = publicName
c.User.PrivateName = privateName
c.User.PreferredLanguage = preferredLang
if err := c.User.Save(c.Conn); err != nil {
c.Error(500, CodeUnexpected, MsgUnexpected)
return
}
c.Success(200, map[string]interface{}{
"message": "Data updated successfully",
})
}
// UpdateAccountPassword updates the user password
func UpdateAccountPassword(c middleware.Context) {
var (
password = c.Form("password")
passwordRepeat = c.Form("password_repeat")
currentPassword = c.Form("current_password")
)
if !c.User.CheckPassword(currentPassword) {
c.Error(400, CodePasswordCurrentError, MsgPasswordCurrentError)
return
}
if util.Strlen(password) < 6 {
c.Error(400, CodePasswordLength, MsgPasswordLength)
return
}
if password != passwordRepeat {
c.Error(400, CodePasswordMatch, MsgPasswordMatch)
return
}
if err := c.User.SetPassword(password); err != nil {
c.Error(500, CodeUnexpected, MsgUnexpected)
return
}
if err := c.User.Save(c.Conn); err != nil {
c.Error(500, CodeUnexpected, MsgUnexpected)
return
}
c.Success(200, map[string]interface{}{
"message": "Password updated successfully",
})
}
// UpdateAccountSettings updates the user's settings
func UpdateAccountSettings(c middleware.Context) {
s := c.User.Settings
getPrivacy := func(r *http.Request, kind string) (models.PrivacySettings, error) {
p := models.PrivacySettings{}
// Get the privacy type for the current kind of privacy
if privacyType := c.Form("privacy_" + kind + "_type"); privacyType != "" {
pType, err := strconv.ParseInt(privacyType, 10, 8)
if !models.IsValidPrivacyType(models.PrivacyType(pType)) || err != nil {
return p, errors.New("invalid data provided")
}
p.Type = models.PrivacyType(pType)
} else {
return p, errors.New("privacy type is required")
}
// Get users for the current kind of privacy
if users, ok := r.Form["privacy_"+kind+"_users"]; ok {
uids := make([]bson.ObjectId, 0, len(users))
for _, u := range users {
if !bson.IsObjectIdHex(u) {
return p, errors.New("invalid data provided")
}
uids = append(uids, bson.ObjectIdHex(u))
}
// Check that you only selected users you follow or that follow you
count, err := c.Count("follows", bson.M{"user_from": c.User.ID, "user_to": bson.M{"$in": uids}})
if err != nil || count != len(p.Users) {
count2, err := c.Count("follows", bson.M{"user_to": c.User.ID, "user_from": bson.M{"$in": uids}})
if err != nil || count+count2 != len(uids) {
return p, errors.New("invalid user list provided")
}
}
p.Users = uids
} else if p.Type > models.PrivacyNone {
return p, errors.New("users param required for this privacy type")
}
return p, nil
}
// If override_default_privacy is true the privacy settings used will be just status for
// all privacy kinds
s.OverrideDefaultPrivacy = c.GetBoolean("override_default_privacy")
if s.OverrideDefaultPrivacy {
p, err := getPrivacy(c.Request, "status")
if err != nil {
c.Error(400, CodeInvalidPrivacySettings, MsgInvalidPrivacySettings)
return
}
s.DefaultStatusPrivacy = p
} else {
for _, k := range []string{"status", "video", "photo", "link", "album"} {
p, err := getPrivacy(c.Request, k)
if err != nil {
c.Error(400, CodeInvalidPrivacySettings, MsgInvalidPrivacySettings)
return
}
switch k {
case "status":
s.DefaultStatusPrivacy = p
break
case "video":
s.DefaultVideoPrivacy = p
break
case "photo":
s.DefaultPhotoPrivacy = p
break
case "link":
s.DefaultLinkPrivacy = p
break
case "album":
s.DefaultAlbumPrivacy = p
break
}
}
}
// Get other boolean settings
s.Invisible = c.GetBoolean("invisible")
s.CanReceiveRequests = c.GetBoolean("can_receive_requests")
s.FollowApprovalRequired = c.GetBoolean("follow_approval_required")
s.DisplayAvatarBeforeApproval = c.GetBoolean("display_avatar_before_approval")
s.NotifyNewComment = c.GetBoolean("notify_new_comment")
s.NotifyNewCommentOthers = c.GetBoolean("notify_new_comment_others")
s.NotifyPostsInMyProfile = c.GetBoolean("notify_posts_in_my_profile")
s.NotifyLikes = c.GetBoolean("notify_likes")
s.AllowPostsInMyProfile = c.GetBoolean("allow_posts_in_my_profile")
s.AllowCommentsInPosts = c.GetBoolean("allow_comments_in_posts")
s.DisplayInfoFollowersOnly = c.GetBoolean("display_info_followers_only")
// Recovery method settings. If no recovery_method value is sent or an invalid recovery_method
// is entered RecoveryNone will be set!
if recoveryMethod := c.Form("recovery_method"); recoveryMethod != "" {
method, err := strconv.ParseInt(recoveryMethod, 10, 8)
if err != nil || (method != models.RecoveryNone && method != models.RecoverByQuestion && method != models.RecoverByEMail) {
s.PasswordRecoveryMethod = models.RecoveryNone
}
s.PasswordRecoveryMethod = models.RecoveryMethod(method)
if s.PasswordRecoveryMethod == models.RecoverByQuestion {
s.RecoveryQuestion = c.Form("recovery_question")
s.RecoveryAnswer = c.Form("recovery_answer")
if s.RecoveryAnswer == "" || s.RecoveryQuestion == "" {
c.Error(400, CodeInvalidRecoveryQuestion, MsgInvalidRecoveryQuestion)
return
}
}
// TODO: Recover by email
} else {
s.PasswordRecoveryMethod = models.RecoveryNone
}
// Update the user settings in the database
c.User.Settings = s
if err := c.User.Save(c.Conn); err != nil {
c.Error(500, CodeUnexpected, MsgUnexpected)
return
}
c.Success(200, map[string]interface{}{
"message": "User settings updated successfully",
})
}
// UpldateProfilePicture updates the current profile picture of the user
func UpdateProfilePicture(c middleware.Context) {
// Try to retrieve the uploaded image
file, err := upload.RetrieveUploadedImage(c.Request, "account_picture")
if err != nil {
code, msg := upload.CodeAndMessageForUploadError(err)
c.Error(400, code, msg)
return
}
// Try to upload the image
imagePath, thumbnailPath, err := upload.StoreImage(file, upload.ProfileUploadOptions(c.Config))
if err != nil {
code, msg := upload.CodeAndMessageForUploadError(err)
c.Error(400, code, msg)
return
}
var prevAvatar, prevThumbnail string
// If there was any previously set avatar we store its value to destroy them later
if c.Form("picture_type") == "public" {
prevAvatar = c.User.PublicAvatar
c.User.PublicAvatar = imagePath
prevThumbnail = c.User.PublicAvatarThumbnail
c.User.PublicAvatarThumbnail = thumbnailPath
} else {
prevAvatar = c.User.Avatar
c.User.Avatar = imagePath
prevThumbnail = c.User.AvatarThumbnail
c.User.AvatarThumbnail = thumbnailPath
}
// Update the data
if c.User.Save(c.Conn); err != nil {
c.Error(500, CodeUnexpected, MsgUnexpected)
// If it's been an error updating the changes in the database the files
// need to be erased
os.Remove(upload.ToLocalImagePath(imagePath, c.Config))
os.Remove(upload.ToLocalThumbnailPath(thumbnailPath, c.Config))
return
} else {
// Remove previous avatar
if prevAvatar != "" {
os.Remove(upload.ToLocalImagePath(prevAvatar, c.Config))
}
if prevThumbnail != "" {
os.Remove(upload.ToLocalThumbnailPath(prevThumbnail, c.Config))
}
}
c.Success(200, map[string]interface{}{
"message": "User settings updated successfully",
"thumbnail": thumbnailPath,
})
}
// DestroyAccount destroys the user account and all its related content such as comments, posts, images, etc.
func DestroyAccount(c middleware.Context) {
// TODO: Log errors removing images and push image remove to tasks
confirmed := c.GetBoolean("confirmed")
if confirmed {
var (
p models.Post
cmt models.Comment
)
// Destroy all user tokens
c.RemoveAll("tokens", bson.M{"user_id": c.User.ID})
// Destroy all user material (posts + comments + images)
iter := c.Find("posts", bson.M{"user_id": c.User.ID}).Iter()
for iter.Next(&p) {
if p.Type == models.PostPhoto {
os.Remove(upload.ToLocalImagePath(p.PhotoURL, c.Config))
os.Remove(upload.ToLocalThumbnailPath(p.Thumbnail, c.Config))
}
go timeline.PropagatePostsOnDeletion(c, p.ID)
}
iter.Close()
// Destroy all user comments
iter = c.Find("comments", bson.M{"user_id": c.User.ID}).Iter()
for iter.Next(&cmt) {
go timeline.PropagatePostOnCommentDeleted(c, cmt.PostID, cmt.ID)
}
iter.Close()
// Remove other stuff related to the user
c.RemoveAll("posts", bson.M{"user_id": c.User.ID})
c.RemoveAll("comments", bson.M{"user_id": c.User.ID})
c.RemoveAll("follows", bson.M{"user_to": c.User.ID})
c.RemoveAll("follows", bson.M{"user_from": c.User.ID})
c.RemoveAll("blocks", bson.M{"user_to": c.User.ID})
c.RemoveAll("blocks", bson.M{"user_from": c.User.ID})
c.RemoveAll("timelines", bson.M{"user_id": c.User.ID})
c.RemoveAll("likes", bson.M{"user_id": c.User.ID})
go timeline.PropagatePostOnUserDeleted(c, c.User.ID)
// Logout user
c.Session.Values["user_token"] = nil
c.Session.Values["csrf_key"] = nil
c.Session.Save(c.Request, c.ResponseWriter)
// Destroy user
c.Remove("users", bson.M{"_id": c.User.ID})
// Destroy all user notifications
c.RemoveAll("notifications", bson.M{"user_id": c.User.ID})
// Remove user avatars
if c.User.Avatar != "" {
os.Remove(upload.ToLocalImagePath(c.User.Avatar, c.Config))
}
if c.User.AvatarThumbnail != "" {
os.Remove(upload.ToLocalThumbnailPath(c.User.AvatarThumbnail, c.Config))
}
if c.User.PublicAvatar != "" {
os.Remove(upload.ToLocalImagePath(c.User.PublicAvatar, c.Config))
}
if c.User.PublicAvatarThumbnail != "" {
os.Remove(upload.ToLocalThumbnailPath(c.User.PublicAvatarThumbnail, c.Config))
}
c.Success(200, map[string]interface{}{"message": "User account has been successfully destroyed"})
} else {
c.Success(200, map[string]interface{}{"message": "User account has not been destroyed"})
}
}
// ChangeLanguage changes the default language for the user
func ChangeLanguage(c middleware.Context) {
preferredLang := ""
lang := c.Form("lang")
for _, l := range models.AvailableLanguages {
if lang == l {
preferredLang = l
}
}
if preferredLang == "" {
c.Error(400, CodeInvalidData, MsgInvalidData)
return
}
c.User.PreferredLanguage = preferredLang
if err := c.User.Save(c.Conn); err != nil {
c.Error(500, CodeUnexpected, MsgUnexpected)
return
}
c.Success(200, map[string]interface{}{"message": "Language changed successfully"})
}