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/lib/user.go

187 lines
4.5 KiB
Go
Raw Normal View History

2016-05-02 21:36:49 -04:00
package trantor
import (
2014-08-30 13:17:50 -05:00
log "github.com/cihub/seelog"
2018-04-08 10:55:13 +00:00
"gitlab.com/trantor/trantor/lib/database"
2014-08-30 13:17:50 -05:00
"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)
data.S.Title = "Login -- " + data.S.Title
h.load("login", data)
}
func loginPostHandler(h handler) {
user := h.r.FormValue("user")
pass := h.r.FormValue("pass")
if h.db.ValidPassword(user, pass) {
2014-06-29 19:41:29 -05:00
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
}
2018-04-09 10:49:10 +00:00
lists, err := h.db.GetListsByUser(h.sess.User)
if err != nil {
log.Error("Error loading user ", h.sess.User, " lists: ", err)
}
var data dashboardData
2014-02-19 11:59:16 +01:00
data.S = GetStatus(h)
data.S.Title = "Dashboard -- " + data.S.Title
2014-02-19 11:59:16 +01:00
data.S.Dasboard = true
2018-04-09 10:49:10 +00:00
data.Lists = lists
h.load("dashboard", data)
2013-09-03 14:43:55 +02:00
}
2018-04-09 10:49:10 +00:00
type dashboardData struct {
S Status
Lists []database.BookList
}
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 {
case !h.db.ValidPassword(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:
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:
err := h.db.SetPassword(h.sess.User, pass1)
if err != nil {
log.Warn("Can't update password for user ", h.sess.User, ": ", err)
h.sess.Notify("Password error!", "An error has ocurred updating the password in the database. Sorry.", "error")
} else {
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)
data.S.Title = "Settings -- " + data.S.Title
h.load("settings", data)
2013-09-03 14:43:55 +02:00
}
2018-04-08 10:55:13 +00:00
func userAdminHandler(h handler) {
if !h.sess.IsAdmin() {
notFound(h)
return
}
users, err := h.db.ListUsers()
if err != nil {
log.Error("Something went wrong listing users: ", err)
notFound(h)
return
}
var data userAdminData
data.S = GetStatus(h)
data.S.Title = "Users admin -- " + data.S.Title
data.Users = users
h.load("user_admin", data)
}
func userAdminPostHandler(h handler) {
if !h.sess.IsAdmin() {
notFound(h)
return
}
username := h.r.FormValue("username")
password := h.r.FormValue("password")
role := h.r.FormValue("role")
if password != "" {
err := h.db.SetPassword(username, password)
if err != nil {
h.sess.Notify("An error ocurred!", err.Error(), "error")
} else {
h.sess.Notify("Password updated!", "", "success")
}
} else if role != "" {
err := h.db.SetRole(username, role)
if err != nil {
h.sess.Notify("An error ocurred!", err.Error(), "error")
} else {
h.sess.Notify("Role updated!", "", "success")
}
}
userAdminHandler(h)
}
2018-04-08 11:11:27 +00:00
func addUserHandler(h handler) {
if !h.sess.IsAdmin() {
notFound(h)
return
}
username := h.r.FormValue("username")
password := h.r.FormValue("password")
role := h.r.FormValue("role")
err := h.db.AddUser(username, password)
if err != nil {
h.sess.Notify("An error ocurred!", err.Error(), "error")
} else {
err := h.db.SetRole(username, role)
if err != nil {
h.sess.Notify("An error ocurred!", err.Error(), "error")
} else {
h.sess.Notify("User created!", "", "success")
}
}
userAdminHandler(h)
}
2018-04-08 10:55:13 +00:00
type userAdminData struct {
S Status
Users []database.User
}