-
Notifications
You must be signed in to change notification settings - Fork 3
/
users.go
84 lines (61 loc) · 1.53 KB
/
users.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
package database
import (
"strings"
"time"
"github.com/globalsign/mgo/bson"
)
func (mg *mongo) GetUserByEmail(email string) User {
session, c := mg.collection(USERS_COL)
defer session.Close()
email = strings.ToLower(email)
var usr User
c.Find(bson.M{"email": email}).One(&usr)
if len(usr.Email) > 0 {
return usr
}
return User{}
}
func (mg *mongo) GetUserByID(id bson.ObjectId) User {
session, c := mg.collection(USERS_COL)
defer session.Close()
var usr User
c.Find(bson.M{"_id": id}).One(&usr)
if len(usr.Email) > 0 {
return usr
}
return User{}
}
// This sets the user ID and the users Join & Update times
func (mg *mongo) InsertUser(user User) User {
session, c := mg.collection(USERS_COL)
defer session.Close()
user.Email = strings.ToLower(user.Email)
user.Username = strings.ToLower(user.Username)
user.MGID = bson.NewObjectId()
user.Joined = time.Now()
user.Updated = user.Joined
user.LastLogin = user.Joined
c.Insert(user)
return user
}
func (mg *mongo) ChangePasswordUser(user User, newPass string) User {
session, c := mg.collection(USERS_COL)
defer session.Close()
user.Password = newPass
user.Updated = time.Now()
c.Update(
bson.M{"_id": user.MGID},
bson.M{"$set": bson.M{"password": user.Password, "updated": user.Updated}},
)
return user
}
func (mg *mongo) SetLastLogin(user User) User {
session, c := mg.collection(USERS_COL)
defer session.Close()
user.LastLogin = time.Now()
c.Update(
bson.M{"_id": user.MGID},
bson.M{"$set": bson.M{"ll": user.LastLogin}},
)
return user
}