-
Notifications
You must be signed in to change notification settings - Fork 1
/
userStorageMySQL.go
94 lines (79 loc) · 2.08 KB
/
userStorageMySQL.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
// An implementation of user storage in MySQL
package main
import (
"errors"
"code.google.com/p/go.crypto/bcrypt"
"github.com/coopernurse/gorp"
"github.com/nyc-camp/authenticator/libuser"
)
const USER_NOT_UPDATED = "userstoragemysql: User Not Updated."
const USER_NOT_DELETED = "userstoragemysql: User not deleted or does not exist."
type UserStorageMySQL struct {
Dbmap *gorp.DbMap
}
func (usm UserStorageMySQL) RetrieveUser(username string) (libuser.User, error) {
var user libuser.User
err := usm.Dbmap.SelectOne(&user, "SELECT * FROM user WHERE username=?", username)
if err != nil {
return libuser.User{}, err
}
return user, nil
}
func (usm UserStorageMySQL) CreateUser(user libuser.User) (bool, error) {
err := usm.Dbmap.Insert(&user)
if err != nil {
return false, err
}
return true, nil
}
func (usm UserStorageMySQL) UpdateUser(user libuser.User) (bool, error) {
count, err := usm.Dbmap.Update(&user)
if err != nil {
return false, err
}
if count == 0 {
return false, errors.New(USER_NOT_UPDATED)
}
return true, nil
}
func (usm UserStorageMySQL) DeleteUser(user libuser.User) (bool, error) {
count, err := usm.Dbmap.Delete(user)
if err != nil {
return false, err
}
if count == 0 {
return false, errors.New(USER_NOT_DELETED)
}
return true, nil
}
func (usm UserStorageMySQL) CheckUsername(username string) (bool, error) {
count, err := usm.Dbmap.SelectInt("select count(*) from user where username=?", username)
if err != nil {
return false, err
}
if count == 0 {
return false, nil
}
return true, nil
}
func (usm UserStorageMySQL) CheckEmail(email string) (bool, error) {
count, err := usm.Dbmap.SelectInt("select count(*) from user where email=?", email)
if err != nil {
return false, err
}
if count == 0 {
return false, nil
}
return true, nil
}
func (usm UserStorageMySQL) CheckPassword(username, password string) (bool, error) {
user, err := usm.RetrieveUser(username)
if err != nil {
return false, err
}
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
if err != nil {
return false, err
}
return true, nil
}