-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.go
121 lines (95 loc) · 2.38 KB
/
register.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
package models
import (
"errors"
"html"
"github.com/asaskevich/govalidator"
"github.com/microcosm-cc/bluemonday"
"github.com/eirka/eirka-libs/config"
"github.com/eirka/eirka-libs/db"
e "github.com/eirka/eirka-libs/errors"
"github.com/eirka/eirka-libs/validate"
)
// RegisterModel contains information for initial account creation
type RegisterModel struct {
UID uint
Name string
Email string
Password string
Hashed []byte
}
// IsValid will check struct validity
func (r *RegisterModel) IsValid() bool {
if r.Name == "" {
return false
}
if r.Hashed == nil {
return false
}
return true
}
// Validate will check the provided name length and email
func (r *RegisterModel) Validate() (err error) {
// Initialize bluemonday
p := bluemonday.StrictPolicy()
// sanitize for html and xss
r.Name = html.UnescapeString(p.Sanitize(r.Name))
// Validate name input
name := validate.Validate{Input: r.Name, Max: config.Settings.Limits.NameMaxLength, Min: config.Settings.Limits.NameMinLength}
if name.IsEmpty() {
return e.ErrNameEmpty
} else if name.MinLength() {
return e.ErrNameShort
} else if name.MaxLength() {
return e.ErrNameLong
}
// Validate password input
password := validate.Validate{Input: r.Password, Max: config.Settings.Limits.PasswordMaxLength, Min: config.Settings.Limits.PasswordMinLength}
if password.IsEmpty() {
return e.ErrPasswordEmpty
} else if password.MinLength() {
return e.ErrPasswordShort
} else if password.MaxLength() {
return e.ErrPasswordLong
}
// if theres an email validate it
if r.Email != "" {
// Validate email
if !govalidator.IsEmail(r.Email) {
return e.ErrInvalidEmail
}
}
return
}
// Register will create a new user
func (r *RegisterModel) Register() (err error) {
// check model validity
if !r.IsValid() {
return errors.New("RegisterModel is not valid")
}
// Get transaction handle
tx, err := db.GetTransaction()
if err != nil {
return
}
defer tx.Rollback()
e1, err := tx.Exec("INSERT into users (user_name, user_email, user_password, user_confirmed) VALUES (?,?,?,?)",
r.Name, r.Email, r.Hashed, 1)
if err != nil {
return
}
uid, err := e1.LastInsertId()
if err != nil {
return err
}
r.UID = uint(uid)
_, err = tx.Exec("INSERT into user_role_map VALUES (?,?)", r.UID, 2)
if err != nil {
return
}
// Commit transaction
err = tx.Commit()
if err != nil {
return
}
return
}