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) }