summaryrefslogtreecommitdiff
path: root/app/controllers/app.go
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers/app.go')
-rw-r--r--app/controllers/app.go147
1 files changed, 146 insertions, 1 deletions
diff --git a/app/controllers/app.go b/app/controllers/app.go
index e76d76b..4aad9a3 100644
--- a/app/controllers/app.go
+++ b/app/controllers/app.go
@@ -1,6 +1,9 @@
package controllers
-import "github.com/revel/revel"
+import (
+ "github.com/garyburd/redigo/redis"
+ "github.com/revel/revel"
+)
type App struct {
*revel.Controller
@@ -9,3 +12,145 @@ type App struct {
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)
+}