2023-05-20 20:15:57 +02:00
|
|
|
// Copyright 2023 wanderer <a_mirre at utb dot cz>
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-05-10 19:09:41 +02:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.dotya.ml/mirre-mt/pcmt/ent"
|
|
|
|
moduser "git.dotya.ml/mirre-mt/pcmt/modules/user"
|
|
|
|
"github.com/labstack/echo-contrib/session"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Home(client *ent.Client) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
var username string
|
|
|
|
|
2023-05-11 23:50:40 +02:00
|
|
|
addHeaders(c)
|
|
|
|
|
2023-05-10 19:09:41 +02:00
|
|
|
sess, _ := session.Get(setting.SessionCookieName(), c)
|
|
|
|
if sess == nil {
|
|
|
|
log.Info("no session, redirecting to /signin", "endpoint", "/home")
|
2023-05-11 04:55:56 +02:00
|
|
|
return c.Redirect(http.StatusSeeOther, "/signin")
|
2023-05-10 19:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if sess.Values["foo"] != nil {
|
|
|
|
log.Info("gorilla session", "custom field test", sess.Values["foo"].(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
uname := sess.Values["username"]
|
|
|
|
if uname == nil {
|
|
|
|
log.Info("session cookie found but username invalid, redirecting to signin", "endpoint", "/home")
|
|
|
|
|
|
|
|
return c.Redirect(http.StatusSeeOther, "/signin")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("gorilla session", "username", sess.Values["username"].(string))
|
|
|
|
username = sess.Values["username"].(string)
|
|
|
|
|
|
|
|
// example denial.
|
|
|
|
// if _, err := c.Cookie("aha"); err != nil {
|
|
|
|
// log.Printf("error: %q", err)
|
|
|
|
// return echo.NewHTTPError(http.StatusUnauthorized, http.StatusText(http.StatusUnauthorized))
|
|
|
|
// }
|
|
|
|
|
|
|
|
var u moduser.User
|
|
|
|
|
|
|
|
ctx := context.WithValue(context.Background(), moduser.CtxKey{}, slogger)
|
|
|
|
if usr, err := moduser.QueryUser(ctx, client, username); err == nil && usr != nil {
|
|
|
|
c.Logger().Debug("got usr: ", usr.Username)
|
|
|
|
c.Logger().Debug("admin? ", usr.IsAdmin)
|
|
|
|
|
|
|
|
u.ID = usr.ID
|
|
|
|
u.Username = usr.Username
|
|
|
|
u.IsActive = usr.IsActive
|
|
|
|
u.IsLoggedIn = true
|
|
|
|
} else {
|
|
|
|
c.Logger().Error("failed to query usr", username)
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError)+" failed to query usr (make sure you've got cookies enabled)",
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-08-05 21:43:45 +02:00
|
|
|
csrf := c.Get("csrf").(string)
|
2023-08-15 18:33:48 +02:00
|
|
|
p := newPage()
|
|
|
|
|
|
|
|
p.Title = "Home"
|
|
|
|
p.Current = "home"
|
|
|
|
p.CSRF = csrf
|
|
|
|
p.Name = username
|
|
|
|
p.User = u
|
2023-08-05 21:43:45 +02:00
|
|
|
|
2023-05-11 05:07:53 +02:00
|
|
|
err := c.Render(http.StatusOK, "home.tmpl",
|
2023-08-15 18:33:48 +02:00
|
|
|
p,
|
2023-05-10 19:09:41 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
c.Logger().Errorf("error: %q", err)
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|