diff options
| author | Horus_Arch | 2015-02-10 10:10:22 +0100 |
|---|---|---|
| committer | Horus_Arch | 2015-02-10 10:10:22 +0100 |
| commit | c7869f2326d4e8282697c6961f39f1b19b4c8c94 (patch) | |
| tree | c4ac577605c894ac6e0baa8660f740823f609790 | |
| parent | 8c30749613bcf1ce47fff0a6d1a60c34f91f01c4 (diff) | |
| download | webmon-c7869f2326d4e8282697c6961f39f1b19b4c8c94.tar.gz | |
Mailer views.
| -rw-r--r-- | app/controllers/app.go | 95 | ||||
| -rw-r--r-- | app/controllers/db.go | 19 | ||||
| -rw-r--r-- | app/mailers/mail.go | 9 | ||||
| -rw-r--r-- | app/views/Mailer/ConfirmRegistration.html | 4 | ||||
| -rw-r--r-- | app/views/Mailer/SendConfirmationKey.html | 4 |
5 files changed, 85 insertions, 46 deletions
diff --git a/app/controllers/app.go b/app/controllers/app.go index 4aad9a3..4aae8aa 100644 --- a/app/controllers/app.go +++ b/app/controllers/app.go @@ -70,42 +70,70 @@ func (c App) Login(email string, legacy bool, user string, password string, pass defer conn.Close() _, err := conn.Do("SET", key, u.email, 86400) // Send email with confirmation link - // TODO Implementing the function - SendConfirmationKey(email, key) + mailers.SendConfirmationKey(email, key) // TODO Print message that a mail was sent return c.Redirect(App.PrintLogin) } } -func (c App) Confirm(key string) revel.Result { +func (c App) Confirm(key, registration string) revel.Result { - c.Validation.Required(key).Message("No key provided.") + if registration == "" { + // Processing login - 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.") + c.Validation.Required(key).Message("No key provided.") - _, _ = conn.Do("DEL", key) + 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.") - if c.Validation.HasErrors() { - c.Validation.Keep() - c.FlashParams() - return c.Redirect(App.PrintLogin) - } + _, _ = conn.Do("DEL", key) - u := User{} - db.Where("email = ?", email).First(&u) + 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) + } - 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 + c.Session["login"] = "true" + c.Session["uid"] = u.id + + } return c.Redirect(App.Account) } @@ -127,30 +155,23 @@ func (c App) Register(email, confirmEmail, user, password, confirmPassword strin } p := HashPassword(password) + key := RandomKey() // Create key to confirm mail adress u := User{ - Name: user, - Email: email, - Password: p, - Confirmed: false, - Alerts: []Alert{{Email: email}}, + Name: user, + Email: email, + Password: p, + Confirmed: false, + ConfirmationKey: key, + 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) + 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.PrintLogin) + return c.Redirect(App.PrintRegister) } diff --git a/app/controllers/db.go b/app/controllers/db.go index f6a535e..8e0af2a 100644 --- a/app/controllers/db.go +++ b/app/controllers/db.go @@ -8,15 +8,16 @@ import ( ) type User struct { - Id int64 - Email string `sql:"unique"` - Name string `sql:"unique"` - Password string - Confirmed bool - Alerts []Alert - CreatedAt time.Time - DeletedAt time.Time - UpdatedAt time.Time + Id int64 + Email string `sql:"unique"` + Name string `sql:"unique"` + Password string + Confirmed bool + ConfirmationKey string + Alerts []Alert + CreatedAt time.Time + DeletedAt time.Time + UpdatedAt time.Time } // Multiple accounts which are alerted diff --git a/app/mailers/mail.go b/app/mailers/mail.go index 1c3a198..6593f95 100644 --- a/app/mailers/mail.go +++ b/app/mailers/mail.go @@ -13,3 +13,12 @@ func (u Mailer) SendConfirmationKey(email, key) { "key": key, }) } + +func (u Mailer) ConfirmRegistraion(user, email, key) { + return u.Send(revel_mailer.H{ + "name": user, + "subject": "Confirm registration", + "to": []string{email}, + "key": key, + }) +} diff --git a/app/views/Mailer/ConfirmRegistration.html b/app/views/Mailer/ConfirmRegistration.html new file mode 100644 index 0000000..f1bf3e9 --- /dev/null +++ b/app/views/Mailer/ConfirmRegistration.html @@ -0,0 +1,4 @@ +<h1>{{.subject}}</h1> + +<p>Hello {{.user}},<br> +to confirm your registration please follow this link: https://webmon.iamfabulous.de/confirm? diff --git a/app/views/Mailer/SendConfirmationKey.html b/app/views/Mailer/SendConfirmationKey.html new file mode 100644 index 0000000..38f366d --- /dev/null +++ b/app/views/Mailer/SendConfirmationKey.html @@ -0,0 +1,4 @@ +<h1>{{.subject}}</h1> + +<p>Hello,<br> +please follow this link to confirm your action: https://webmon.iamfabulous.de/confirm?key={{.key}}</p> |
