2013-09-03 14:02:54 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2013-09-24 10:53:51 +02:00
|
|
|
func loginHandler(h handler) {
|
2014-02-19 11:59:16 +01:00
|
|
|
if h.sess.User != "" {
|
|
|
|
http.Redirect(h.w, h.r, "/dashboard/", http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-03 14:02:54 +02:00
|
|
|
var data statusData
|
2013-09-24 10:53:51 +02:00
|
|
|
data.S = GetStatus(h)
|
|
|
|
loadTemplate(h.w, "login", data)
|
2013-09-03 14:02:54 +02:00
|
|
|
}
|
|
|
|
|
2013-09-24 10:53:51 +02:00
|
|
|
func loginPostHandler(h handler) {
|
|
|
|
user := h.r.FormValue("user")
|
|
|
|
pass := h.r.FormValue("pass")
|
|
|
|
if h.db.UserValid(user, pass) {
|
2013-09-03 14:02:54 +02:00
|
|
|
log.Println("User", user, "log in")
|
2013-09-24 10:53:51 +02:00
|
|
|
h.sess.LogIn(user)
|
|
|
|
h.sess.Notify("Successful login!", "Welcome "+user, "success")
|
2013-09-03 14:02:54 +02:00
|
|
|
} else {
|
|
|
|
log.Println("User", user, "bad user or password")
|
2013-09-24 10:53:51 +02:00
|
|
|
h.sess.Notify("Invalid login!", "user or password invalid", "error")
|
2013-09-03 14:02:54 +02:00
|
|
|
}
|
2013-09-24 10:53:51 +02:00
|
|
|
h.sess.Save(h.w, h.r)
|
|
|
|
http.Redirect(h.w, h.r, h.r.Referer(), http.StatusFound)
|
2013-09-03 14:02:54 +02:00
|
|
|
}
|
2013-09-03 14:15:30 +02:00
|
|
|
|
2013-09-24 10:53:51 +02:00
|
|
|
func createUserHandler(h handler) {
|
|
|
|
pass := h.r.FormValue("pass")
|
|
|
|
confirmPass := h.r.FormValue("confirmPass")
|
2013-09-03 14:15:30 +02:00
|
|
|
if pass != confirmPass {
|
2013-09-24 10:53:51 +02:00
|
|
|
h.sess.Notify("Registration error!", "Passwords don't match", "error")
|
2013-09-03 14:15:30 +02:00
|
|
|
} else {
|
2013-09-24 10:53:51 +02:00
|
|
|
user := h.r.FormValue("user")
|
|
|
|
err := h.db.AddUser(user, pass)
|
2013-09-03 14:15:30 +02:00
|
|
|
if err == nil {
|
2013-09-24 10:53:51 +02:00
|
|
|
h.sess.Notify("Account created!", "Welcome "+user+". Now you can login", "success")
|
2013-09-03 14:15:30 +02:00
|
|
|
} else {
|
2013-09-24 10:53:51 +02:00
|
|
|
h.sess.Notify("Registration error!", "There was some database problem, if it keeps happening please inform me", "error")
|
2013-09-03 14:15:30 +02:00
|
|
|
}
|
|
|
|
}
|
2013-09-24 10:53:51 +02:00
|
|
|
h.sess.Save(h.w, h.r)
|
|
|
|
http.Redirect(h.w, h.r, h.r.Referer(), http.StatusFound)
|
2013-09-03 14:15:30 +02:00
|
|
|
}
|
2013-09-03 14:43:55 +02:00
|
|
|
|
2014-02-19 11:59:16 +01:00
|
|
|
func dashboardHandler(h handler) {
|
|
|
|
if h.sess.User == "" {
|
|
|
|
notFound(h)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var data statusData
|
|
|
|
data.S = GetStatus(h)
|
|
|
|
data.S.Dasboard = true
|
|
|
|
loadTemplate(h.w, "dashboard", data)
|
2013-09-03 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
2013-09-24 10:53:51 +02:00
|
|
|
func settingsHandler(h handler) {
|
|
|
|
if h.sess.User == "" {
|
|
|
|
notFound(h)
|
2013-09-03 14:43:55 +02:00
|
|
|
return
|
|
|
|
}
|
2013-09-24 10:53:51 +02:00
|
|
|
if h.r.Method == "POST" {
|
|
|
|
current_pass := h.r.FormValue("currpass")
|
|
|
|
pass1 := h.r.FormValue("password1")
|
|
|
|
pass2 := h.r.FormValue("password2")
|
2013-09-03 14:43:55 +02:00
|
|
|
switch {
|
2013-09-24 10:53:51 +02:00
|
|
|
case !h.db.UserValid(h.sess.User, current_pass):
|
|
|
|
h.sess.Notify("Password error!", "The current password given don't match with the user password. Try again", "error")
|
2013-09-03 14:43:55 +02:00
|
|
|
case pass1 != pass2:
|
2013-09-24 10:53:51 +02:00
|
|
|
h.sess.Notify("Passwords don't match!", "The new password and the confirmation password don't match. Try again", "error")
|
2013-09-03 14:43:55 +02:00
|
|
|
default:
|
2013-09-24 10:53:51 +02:00
|
|
|
h.db.SetPassword(h.sess.User, pass1)
|
|
|
|
h.sess.Notify("Password updated!", "Your new password is correctly set.", "success")
|
2013-09-03 14:43:55 +02:00
|
|
|
}
|
2013-09-24 10:53:51 +02:00
|
|
|
h.sess.Save(h.w, h.r)
|
2013-09-03 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
2014-02-19 11:59:16 +01:00
|
|
|
var data statusData
|
2013-09-24 10:53:51 +02:00
|
|
|
data.S = GetStatus(h)
|
|
|
|
loadTemplate(h.w, "settings", data)
|
2013-09-03 14:43:55 +02:00
|
|
|
}
|