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
|
package controllers
import (
"github.com/garyburd/redigo/redis"
"github.com/revel/revel"
)
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(confirm).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.")
c.Validation.Required(VerifyPassword(password, u.password)).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"] = u.id
return c.Redirect(App.Account)
} else {
db.Where("email = ?", email).First(&u)
// Get random string
key := RandomKey()
// Set key in redis
conn := pool.Get()
defer conn.Close()
_, err := conn.Do("SET", key, u.email, 86400)
// Send email with confirmation link
// TODO Implementing the function
SendConfirmationKey(email, key)
// TODO Print message that a mail was sent
return c.Redirect(App.PrintLogin)
}
}
func (c App) Confirm(key string) revel.Result {
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 = ?", email).First(&u)
if u.confirmed == false {
// E-Mail is now confirmed
u.confirmed = true
db.Save(&u)
}
c.Session["login"] = "true"
c.Session["uid"] = u.id
return c.Redirect(App.Account)
}
func (c App) Register(email, confirmEmail, user, password, confirmPassword string) revel.Result {
c.Validation.Required(email).Messagel("Please provide a mail adress.")
c.Validation.Required(email == confirmEmail).Messagel("The mail adresses do not match.")
c.Validation.Required(user).Messagel("Please provide a user name.")
if password != "" {
c.Validation.Required(password == confirmPassword).Messagel("Passwords do not match.")
}
if c.Validation.HasErrors() {
c.Validation.Keep()
c.FlashParams()
return c.Redirect(App.PrintRegister)
}
p := HashPassword(password)
u := User{
Name: user,
Email: email,
Password: p,
Confirmed: false,
Alerts: []Alert{{Email: email}},
}
db.NewRecord(user)
db.Create(&user)
db.Save(&user)
// Create key to confirm mail adress
key := RandomKey()
// Redis
conn := pool.Get()
defer conn.Close()
_, err := conn.Do("SET", key, email)
// Send email with confirmation link
// TODO Implementing the function
SendConfirmationKey(email, key)
c.Flash.Success("A mail with a confirmation link was sent. Please confirm your mail adress now.")
return c.Redirect(App.PrintLogin)
}
|