This repository has been archived on 2025-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
trantor/user.go

90 lines
2.3 KiB
Go
Raw Normal View History

package main
import (
2014-08-30 13:17:50 -05:00
log "github.com/cihub/seelog"
"net/http"
)
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
}
var data statusData
data.S = GetStatus(h)
loadTemplate(h, "login", data)
}
func loginPostHandler(h handler) {
user := h.r.FormValue("user")
pass := h.r.FormValue("pass")
2014-06-29 19:41:29 -05:00
if h.db.User(user).Valid(pass) {
log.Info("User ", user, " log in")
h.sess.LogIn(user)
h.sess.Notify("Successful login!", "Welcome "+user, "success")
} else {
2014-06-29 19:41:29 -05:00
log.Warn("User ", user, " bad user or password")
h.sess.Notify("Invalid login!", "user or password invalid", "error")
}
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
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 {
h.sess.Notify("Registration error!", "Passwords don't match", "error")
2013-09-03 14:15:30 +02:00
} else {
user := h.r.FormValue("user")
err := h.db.AddUser(user, pass)
2013-09-03 14:15:30 +02:00
if err == nil {
h.sess.Notify("Account created!", "Welcome "+user+". Now you can login", "success")
2013-09-03 14:15:30 +02:00
} else {
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
}
}
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, "dashboard", data)
2013-09-03 14:43:55 +02:00
}
func settingsHandler(h handler) {
if h.sess.User == "" {
notFound(h)
2013-09-03 14:43:55 +02:00
return
}
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 {
2014-06-29 19:41:29 -05:00
case !h.db.User(h.sess.User).Valid(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:
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:
2014-06-29 19:41:29 -05:00
h.db.User(h.sess.User).SetPassword(pass1)
h.sess.Notify("Password updated!", "Your new password is correctly set.", "success")
2013-09-03 14:43:55 +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
data.S = GetStatus(h)
loadTemplate(h, "settings", data)
2013-09-03 14:43:55 +02:00
}