diff options
| author | Horus3 | 2015-02-10 14:01:31 +0100 |
|---|---|---|
| committer | Horus3 | 2015-02-10 14:01:31 +0100 |
| commit | 5a5d0fd250c28c3560ff00b9a367938e915eed7f (patch) | |
| tree | 145fa031631390a93e41a226ba9dc19190ba27b2 | |
| parent | c7869f2326d4e8282697c6961f39f1b19b4c8c94 (diff) | |
| download | webmon-5a5d0fd250c28c3560ff00b9a367938e915eed7f.tar.gz | |
Restructuring and bug fixes.
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | app/controllers/app.go | 63 | ||||
| -rw-r--r-- | app/controllers/db.go | 23 | ||||
| -rw-r--r-- | app/controllers/mail.go (renamed from app/mailers/mail.go) | 6 | ||||
| -rw-r--r-- | app/controllers/utilities.go | 20 | ||||
| -rw-r--r-- | app/views/Mailer/ConfirmRegistration.html | 3 | ||||
| -rw-r--r-- | app/views/Mailer/SendConfirmationKey.html | 2 | ||||
| -rw-r--r-- | conf/app.conf | 8 | ||||
| -rw-r--r-- | conf/routes | 7 |
9 files changed, 83 insertions, 50 deletions
@@ -2,3 +2,4 @@ test-results/ tmp/ routes/ *.swp +*.db diff --git a/app/controllers/app.go b/app/controllers/app.go index 4aae8aa..fb11d01 100644 --- a/app/controllers/app.go +++ b/app/controllers/app.go @@ -1,8 +1,9 @@ package controllers import ( - "github.com/garyburd/redigo/redis" + "github.com/jinzhu/gorm" "github.com/revel/revel" + // "github.com/revel/revel/modules/jobs/app/jobs" ) type App struct { @@ -31,7 +32,7 @@ func (c App) Login(email string, legacy bool, user string, password string, pass // 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(passwordConfirm).Message("Please confirm your password.") c.Validation.Required(password == passwordConfirm).Message("The passwords do not match.") } else { // Show login form only with email @@ -48,8 +49,9 @@ func (c App) Login(email string, legacy bool, user string, password string, pass 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.") + 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() @@ -58,21 +60,27 @@ func (c App) Login(email string, legacy bool, user string, password string, pass } c.Session["login"] = "true" - c.Session["uid"] = u.id + c.Session["uid"] = string(u.Id) return c.Redirect(App.Account) } else { - db.Where("email = ?", email).First(&u) + 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() - _, err := conn.Do("SET", key, u.email, 86400) + _, _ = conn.Do("SET", key, u.Email, 86400) // Send email with confirmation link - mailers.SendConfirmationKey(email, key) + //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.") - // TODO Print message that a mail was sent return c.Redirect(App.PrintLogin) } } @@ -98,17 +106,17 @@ func (c App) Confirm(key, registration string) revel.Result { } u := User{} - db.Where("email = ?", email).First(&u) + db.Where("email = ?", confirmKey).First(&u) - if u.confirmed == false { + if u.Confirmed == false { // E-Mail is now confirmed - u.confirmed = true - u.confirmationkey = nil + u.Confirmed = true + u.ConfirmationKey = "" db.Save(&u) } c.Session["login"] = "true" - c.Session["uid"] = u.id + c.Session["uid"] = string(u.Id) } else { // Processing registration confirmation @@ -118,7 +126,7 @@ func (c App) Confirm(key, registration string) revel.Result { u := User{} db.Where("confirmationkey = ?").First(&u) - c.Validation.Required(registration == u.confirmationkey).Message("Key does not seem to be valid.") + c.Validation.Required(registration == u.ConfirmationKey).Message("Key does not seem to be valid.") if c.Validation.HasErrors() { c.Validation.Keep() @@ -126,12 +134,12 @@ func (c App) Confirm(key, registration string) revel.Result { return c.Redirect(App.PrintLogin) } - u.confirmed = true - u.confirmationkey = nil + u.Confirmed = true + u.ConfirmationKey = "" db.Save(&u) c.Session["login"] = "true" - c.Session["uid"] = u.id + c.Session["uid"] = string(u.Id) } @@ -140,12 +148,12 @@ func (c App) Confirm(key, registration string) revel.Result { 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.") + 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).Messagel("Passwords do not match.") + c.Validation.Required(password == confirmPassword).Message("Passwords do not match.") } if c.Validation.HasErrors() { @@ -154,7 +162,7 @@ func (c App) Register(email, confirmEmail, user, password, confirmPassword strin return c.Redirect(App.PrintRegister) } - p := HashPassword(password) + p, _ := HashPassword(password) key := RandomKey() // Create key to confirm mail adress u := User{ Name: user, @@ -165,12 +173,13 @@ func (c App) Register(email, confirmEmail, user, password, confirmPassword strin Alerts: []Alert{{Email: email}}, } - db.NewRecord(user) - db.Create(&user) - db.Save(&user) + db.NewRecord(u) + db.Create(&u) + db.Save(&u) // Send email with confirmation link - mailers.ConfirmRegistration(email, key) + //jobs.Now(Mailer{}.ConfirmRegistration(email, key)) + _ = Mailer{}.ConfirmRegistration(user, email, key) c.Flash.Success("A mail with a confirmation link was sent. Please confirm your mail adress now.") return c.Redirect(App.PrintRegister) diff --git a/app/controllers/db.go b/app/controllers/db.go index 8e0af2a..baa772c 100644 --- a/app/controllers/db.go +++ b/app/controllers/db.go @@ -56,9 +56,28 @@ type Version struct { var db = DBInit() -func DBInit() *gorm.DB { +func DBInit() gorm.DB { // Open database handler - db, err := gorm.Open(revel.Config.String("db.driver"), revel.Config.String("db.spec")) + // !!! FIXME THIS THROWS A PANIC AT COMPILE TIME. WHY? !!! + + //db, err := gorm.Open(revel.Config.StringDefault("db.driver", "sqlite3"), revel.Config.StringDefault("db.spec", "webmon.db")) + + /* OR */ + + /* + var ( + d, s string + ) + d = revel.Config.StringDefault("test.d", "sqlite3") + s = revel.Config.StringDefault("test.s", "webmon.db") + //db, err := gorm.Open(driver, spec) + + revel.WARN.Println(d) + revel.WARN.Println(s) + */ + + // This works. + db, err := gorm.Open("sqlite3", "webmon.db") // Set database logging to TRACE db.LogMode(true) diff --git a/app/mailers/mail.go b/app/controllers/mail.go index 6593f95..b05da4e 100644 --- a/app/mailers/mail.go +++ b/app/controllers/mail.go @@ -1,4 +1,4 @@ -package mailers +package controllers import "github.com/tanema/revel_mailer" @@ -6,7 +6,7 @@ type Mailer struct { revel_mailer.Mailer } -func (u Mailer) SendConfirmationKey(email, key) { +func (u Mailer) SendConfirmationKey(email, key string) error { return u.Send(revel_mailer.H{ "subject": "Confirmation Key", "to": []string{email}, @@ -14,7 +14,7 @@ func (u Mailer) SendConfirmationKey(email, key) { }) } -func (u Mailer) ConfirmRegistraion(user, email, key) { +func (u Mailer) ConfirmRegistration(user, email, key string) error { return u.Send(revel_mailer.H{ "name": user, "subject": "Confirm registration", diff --git a/app/controllers/utilities.go b/app/controllers/utilities.go index 2eae775..1aa5d6d 100644 --- a/app/controllers/utilities.go +++ b/app/controllers/utilities.go @@ -4,7 +4,7 @@ import ( "crypto/md5" "fmt" "github.com/garyburd/redigo/redis" - "github.com/tanema/revel_mailer" + "github.com/revel/revel" "golang.org/x/crypto/bcrypt" "io" "io/ioutil" @@ -22,7 +22,7 @@ func Get(url string) (string, error) { defer response.Body.Close() contents, err := ioutil.ReadAll(response.Body) - if er != nil { + if err != nil { return "Reading body failed.", err } @@ -58,13 +58,7 @@ func newPool() *redis.Pool { IdleTimeout: 240 * time.Second, Dial: func() (redis.Conn, error) { //c, err := redis.Dial("tcp", ":6379") - if revel.Config.Bool("cache.redis") { - // If we use redis as cache we reuse the config part - c, err := redis.Dial("tcp", revel.Config.String("cache.hosts")) - } else { - // Otherwise we use our own configuration - c, err := redis.Dial("tcp", revel.Config.String("redis.server")+":"+revel.Config.String("redis.port")) - } + c, err := redis.Dial("tcp", revel.Config.StringDefault("redis.server", "127.0.0.1")+":"+revel.Config.StringDefault("redis.port", "6379")) if err != nil { return nil, err } @@ -80,19 +74,19 @@ func newPool() *redis.Pool { // Hashs password with bcrypt and returns the string func HashPassword(password string) (string, error) { if password == "" { - return nil, nil + return "", nil } p := []byte(password) hash, err := bcrypt.GenerateFromPassword(p, 10) if err != nil { - return nil, err + return "", err } - return string(hash) + return string(hash), nil } // Verify password and hash func VerifyPassword(password, hash string) (bool, error) { - err := bcrypt.CompareHashAndPassword(hash, password) + err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) if err != nil { return false, err } diff --git a/app/views/Mailer/ConfirmRegistration.html b/app/views/Mailer/ConfirmRegistration.html index f1bf3e9..a63fcba 100644 --- a/app/views/Mailer/ConfirmRegistration.html +++ b/app/views/Mailer/ConfirmRegistration.html @@ -1,4 +1,5 @@ <h1>{{.subject}}</h1> <p>Hello {{.user}},<br> -to confirm your registration please follow this link: https://webmon.iamfabulous.de/confirm? + to confirm your registration please follow this link: <a href="https://webmon.iamfabulous.de/confirm?registration={{.key}}" title="Confirmation Link">Link</a> +</p> diff --git a/app/views/Mailer/SendConfirmationKey.html b/app/views/Mailer/SendConfirmationKey.html index 38f366d..0fef96b 100644 --- a/app/views/Mailer/SendConfirmationKey.html +++ b/app/views/Mailer/SendConfirmationKey.html @@ -1,4 +1,4 @@ <h1>{{.subject}}</h1> <p>Hello,<br> -please follow this link to confirm your action: https://webmon.iamfabulous.de/confirm?key={{.key}}</p> + please follow this link to confirm your action: <a href="https://webmon.iamfabulous.de/confirm?key={{.key}}">Link</a></p> diff --git a/conf/app.conf b/conf/app.conf index 0c2dbf3..f410612 100644 --- a/conf/app.conf +++ b/conf/app.conf @@ -15,9 +15,9 @@ app.name = webmon # into your application app.secret = vQmfAomzCfewNr0rJlmvQ9Dv42wVMy6x3l5A0X3MsGgAgl95huCHW4fO1hsjkjnn -db.import = "github.com/mattn/go-sqlite3" -db.driver = "sqlite3" -db.spec = "/tmp/gorb.db" +# db.import = "github.com/mattn/go-sqlite3" +#database.driver = "sqlite3" +#database.spec = "/tmp/gorb.db" redis.server = "127.0.0.1" redis.port = "6379" @@ -27,6 +27,8 @@ mail.port = 25 mail.from = webmon mail.user = webmon@iamfabulous.de +module.jobs = github.com/revel/revel/modules/jobs + # The IP address on which to listen. http.addr = diff --git a/conf/routes b/conf/routes index 9ed462f..8af3aed 100644 --- a/conf/routes +++ b/conf/routes @@ -3,8 +3,15 @@ # ~~~~ module:testrunner +module:jobs GET / App.Index +GET /login App.PrintLogin +POST /login App.Login +GET /register App.PrintRegister +POST /register App.Register +GET /confirm App.Confirm +GET /account App.Account # Ignore favicon requests GET /favicon.ico 404 |
