|
|
@@ -4,23 +4,14 @@ import (
|
|
|
"github.com/kataras/iris"
|
|
|
"github.com/kataras/go-template/html"
|
|
|
"fmt"
|
|
|
+ "errors"
|
|
|
"git.mmnx.de/Moe/usermanager"
|
|
|
"git.mmnx.de/Moe/databaseutils"
|
|
|
"git.mmnx.de/Moe/configutils"
|
|
|
"git.mmnx.de/Moe/errorhelpers"
|
|
|
-
|
|
|
- // "errors"
|
|
|
+ "git.mmnx.de/Moe/templatehelpers"
|
|
|
)
|
|
|
|
|
|
-type pageUserParams struct { // TODO outsource
|
|
|
- HasError string
|
|
|
- Error string
|
|
|
- ReqDir string
|
|
|
- Username string
|
|
|
- Email string
|
|
|
- Admin string
|
|
|
- }
|
|
|
-
|
|
|
func main() {
|
|
|
|
|
|
conf := configutils.ReadConfig("config.json") // read config
|
|
|
@@ -51,12 +42,14 @@ func main() {
|
|
|
Layout: "layouts/main.html",
|
|
|
}))
|
|
|
|
|
|
+ iris.UseFunc(templatehelpers.InitPageParams) // dynamic page params, initialization
|
|
|
+
|
|
|
iris.Static("/js", "./static/js", 1) // make js files in static/js available via /js
|
|
|
iris.Static("/css", "./static/css", 1)
|
|
|
iris.Static("/img", "./static/img", 1)
|
|
|
iris.Static("/static", "./static/static", 1)
|
|
|
|
|
|
- iris.Post("/login", loginHandler) // login form handler
|
|
|
+ iris.Post("/login", loginHandler, usermanager.AuthHandler) // login form handler
|
|
|
iris.Post("/register", usermanager.CanBeAuthedHandler, usermanager.RegisterHandler, usermanager.LogoutHandler) // handles registration, logs user out
|
|
|
iris.Post("/account", usermanager.AuthHandler, accountUpdateHandler, usermanager.LogoutHandler) // account management
|
|
|
iris.Post("/admin", usermanager.AuthHandler, usermanager.AdminHandler, adminPostHandler) // admin panel
|
|
|
@@ -74,7 +67,7 @@ func main() {
|
|
|
iris.Listen(":8080")
|
|
|
}
|
|
|
|
|
|
-func loginHandler(ctx *iris.Context) {
|
|
|
+func loginHandler(ctx *iris.Context) { // TODO outsource?
|
|
|
username := ctx.FormValueString("username") // POST values from login form
|
|
|
password := ctx.FormValueString("password")
|
|
|
|
|
|
@@ -82,7 +75,12 @@ func loginHandler(ctx *iris.Context) {
|
|
|
tokenString, err := user.Login(username, password) // try to login
|
|
|
ctx.SetCookieKV("token", tokenString)
|
|
|
|
|
|
- errorhelpers.HandleError(err, ctx, []string{usermanager.SUCCESS_LOGIN, "home"})
|
|
|
+ if err != nil {
|
|
|
+ errorhelpers.HandleError(err, ctx)
|
|
|
+ } else {
|
|
|
+ templatehelpers.UpdatePageParam(ctx, "notification", errorhelpers.SUCCESS_LOGIN) // TODO this for TODO down ?
|
|
|
+ ctx.Next()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
func accountUpdateHandler(ctx *iris.Context) {
|
|
|
@@ -91,49 +89,33 @@ func accountUpdateHandler(ctx *iris.Context) {
|
|
|
userID := ctx.GetString("userID")
|
|
|
|
|
|
err := usermanager.UserUpdateProcessor(username, password, userID)
|
|
|
- errorhelpers.HandleError(err, ctx, []string{usermanager.SUCCESS_UPDATE, "home"})
|
|
|
+ if err != nil { // TODO handle err nil stuff somewhere
|
|
|
+ errorhelpers.HandleError(err, ctx)
|
|
|
+ } else {
|
|
|
+ err = errors.New(errorhelpers.SUCCESS_UPDATE)
|
|
|
+ errorhelpers.HandleError(err, ctx)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
func adminPostHandler(ctx *iris.Context) {
|
|
|
_, err := usermanager.GenerateTokens(5) // generate tokens and store in db, we don't need them now, TODO error handling?
|
|
|
- errorhelpers.HandleError(err, ctx, []string{usermanager.SUCCESS_TOKENS_GENERATED, "home"})
|
|
|
+ if err != nil {
|
|
|
+ errorhelpers.HandleError(err, ctx)
|
|
|
+ } else {
|
|
|
+ err = errors.New(errorhelpers.SUCCESS_TOKENS_GENERATED)
|
|
|
+ errorhelpers.HandleError(err, ctx)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
func templateHandler(ctx *iris.Context) {
|
|
|
- var params usermanager.PageUserParams
|
|
|
- userID := ctx.GetString("userID")
|
|
|
- user, err := usermanager.GetUser(userID)
|
|
|
- if err != nil {
|
|
|
- if err.Error() != "User not logged in" {
|
|
|
- fmt.Println(err.Error())
|
|
|
- }
|
|
|
- }
|
|
|
+ params := ctx.Get("params").(map[string]string)
|
|
|
|
|
|
- template := ""
|
|
|
- switch ctx.RequestPath(false) {
|
|
|
- default:
|
|
|
- template = "home"
|
|
|
- params = usermanager.PageUserParams{"0", "", template, user.Username, user.Admin, []string{}}
|
|
|
- case "/":
|
|
|
- template = "home"
|
|
|
- params = usermanager.PageUserParams{"0", "", template, user.Username, user.Admin, []string{}}
|
|
|
- case "/account":
|
|
|
- template = "account"
|
|
|
- params = usermanager.PageUserParams{"0", "", template, user.Username, user.Admin, []string{}}
|
|
|
- case "/help":
|
|
|
- template = "help"
|
|
|
- params = usermanager.PageUserParams{"0", "", template, user.Username, user.Admin, []string{}}
|
|
|
- case "/admin":
|
|
|
- template = "admin"
|
|
|
- tokens := usermanager.GetTokens(false)
|
|
|
- params = usermanager.PageUserParams{"0", "", template, user.Username, user.Admin, tokens}
|
|
|
- case "/login":
|
|
|
- template = "login"
|
|
|
- params = usermanager.PageUserParams{"0", "", template, "", "0", []string{}}
|
|
|
- case "/register":
|
|
|
- template = "register"
|
|
|
- params = usermanager.PageUserParams{"0", "", template, "", "0", []string{}}
|
|
|
+ switch params["reqDir"] {
|
|
|
+ case "admin":
|
|
|
+ tokens := usermanager.GetTokensAsString(false)
|
|
|
+ params["tokens"] = tokens
|
|
|
+ ctx.Set("params", params)
|
|
|
}
|
|
|
|
|
|
- ctx.MustRender(template + "_box.html", params);
|
|
|
+ ctx.MustRender(params["reqDir"] + "_box.html", params);
|
|
|
}
|