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 mailers.SendConfirmationKey(email, key) // TODO Print message that a mail was sent 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 = ?", email).First(&u) if u.confirmed == false { // E-Mail is now confirmed u.confirmed = true u.confirmationkey = nil db.Save(&u) } c.Session["login"] = "true" c.Session["uid"] = 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 = nil 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) 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(user) db.Create(&user) db.Save(&user) // Send email with confirmation link mailers.ConfirmRegistration(email, key) c.Flash.Success("A mail with a confirmation link was sent. Please confirm your mail adress now.") return c.Redirect(App.PrintRegister) }