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
|
package controllers
import (
"github.com/jinzhu/gorm"
"github.com/revel/revel"
"github.com/revel/revel/modules/jobs/app/jobs"
)
type App struct {
*revel.Controller
}
func (c App) Index() revel.Result {
return c.Render()
}
func (c App) PrintLogin(legacy bool) revel.Result {
return c.Render(legacy)
}
func (c App) PrintRegister() revel.Result {
return c.Render()
}
func (c App) Account() revel.Result {
return c.Render()
}
func (c App) Login(email string, legacy bool, user string, password string, passwordConfirm string) revel.Result {
if legacy {
// Show login form with username and password
c.Validation.Required(user).Message("Please enter a user name.")
c.Validation.Required(password).Message("Please enter a password.")
c.Validation.Required(passwordConfirm).Message("Please confirm your password.")
c.Validation.Required(password == passwordConfirm).Message("The passwords do not match.")
} else {
// Show login form only with email
c.Validation.Required(email).Message("Please provide a mail adress.")
}
if c.Validation.HasErrors() {
c.Validation.Keep()
c.FlashParams()
return c.Redirect(App.PrintLogin)
}
u := User{}
if legacy {
// do database lookup and show if its matched
db.Where("name = ?", user).First(&u)
c.Validation.Required(u.Confirmed).Message("Your mail adress is not confirmed yet.")
verify, _ := VerifyPassword(password, u.Password)
c.Validation.Required(verify).Message("The user/password combination does not exists.")
if c.Validation.HasErrors() {
c.Validation.Keep()
c.FlashParams()
return c.Redirect(App.PrintLogin)
}
c.Session["login"] = "true"
c.Session["uid"] = string(u.Id)
return c.Redirect(App.Account)
} else {
if db.Where("email = ?", email).First(&u).Error == gorm.RecordNotFound {
// Invalid Email
c.Flash.Error("No valid mail adress.")
return c.Redirect(App.PrintLogin)
}
// Get random string
key := RandomKey()
// Set key in redis
conn := pool.Get()
defer conn.Close()
_, _ = conn.Do("SET", key, u.Email, 86400)
// Send email with confirmation link
//jobs.Now(Mailer{}.SendConfirmationKey(email, key))
_ = Mailer{}.SendConfirmationKey(email, key)
c.Flash.Success("A mail with a confirmation link was sent. Follow the instructions there.")
return c.Redirect(App.PrintLogin)
}
}
func (c App) Confirm(key, registration string) revel.Result {
if registration == "" {
// Processing login
c.Validation.Required(key).Message("No key provided.")
conn := pool.Get()
confirmKey, err := conn.Do("GET", key)
c.Validation.Required(err == nil).Message("Oops, there is currently an internal problem. Please check later again.")
c.Validation.Required(confirmKey).Message("Key does not seem to be valid.")
_, _ = conn.Do("DEL", key)
if c.Validation.HasErrors() {
c.Validation.Keep()
c.FlashParams()
return c.Redirect(App.PrintLogin)
}
u := User{}
db.Where("email = ?", confirmKey).First(&u)
if u.Confirmed == false {
// E-Mail is now confirmed
u.Confirmed = true
u.ConfirmationKey = ""
db.Save(&u)
}
c.Session["login"] = "true"
c.Session["uid"] = string(u.Id)
} else {
// Processing registration confirmation
c.Validation.Required(registration).Message("No confirmation key provided.")
u := User{}
db.Where("confirmationkey = ?").First(&u)
c.Validation.Required(registration == u.ConfirmationKey).Message("Key does not seem to be valid.")
if c.Validation.HasErrors() {
c.Validation.Keep()
c.FlashParams()
return c.Redirect(App.PrintLogin)
}
u.Confirmed = true
u.ConfirmationKey = ""
db.Save(&u)
c.Session["login"] = "true"
c.Session["uid"] = string(u.Id)
}
return c.Redirect(App.Account)
}
func (c App) Register(email, confirmEmail, user, password, confirmPassword string) revel.Result {
c.Validation.Required(email).Message("Please provide a mail adress.")
c.Validation.Required(email == confirmEmail).Message("The mail adresses do not match.")
c.Validation.Required(user).Message("Please provide a user name.")
if password != "" {
c.Validation.Required(password == confirmPassword).Message("Passwords do not match.")
}
if c.Validation.HasErrors() {
c.Validation.Keep()
c.FlashParams()
return c.Redirect(App.PrintRegister)
}
p, _ := HashPassword(password)
key := RandomKey() // Create key to confirm mail adress
u := User{
Name: user,
Email: email,
Password: p,
Confirmed: false,
ConfirmationKey: key,
Alerts: []Alert{{Email: email}},
}
db.NewRecord(u)
db.Create(&u)
db.Save(&u)
// Send email with confirmation link
//jobs.Now(Mailer{}.ConfirmRegistration(email, key))
//_ = Mailer{}.ConfirmRegistration(user, email, key)
jobs.Now(JobRegistration{User: user, Email: email, Key: key})
c.Flash.Success("A mail with a confirmation link was sent. Please confirm your mail adress now.")
return c.Redirect(App.PrintRegister)
}
func (c App) Test(email, key string) revel.Result {
// jobs.Now(JobRegistration{User: "foobar", Email: email, Key: key})
Mailer{}.ConfirmRegistration("foobar", "raspi@dns.iamfabulous.de", "string")
c.Flash.Success("A mail with a confirmation link was sent. Please confirm your mail adress now.")
return c.Redirect(App.Index)
}
|