custom: Add new footer template and context

This commit is contained in:
Tom Wiesing 2023-01-06 19:56:13 +01:00
parent 009d649ea6
commit bda763725e
No known key found for this signature in database
18 changed files with 197 additions and 33 deletions

View file

@ -6,6 +6,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
"github.com/julienschmidt/httprouter"
@ -14,7 +15,8 @@ import (
type UserPanel struct {
component.Base
Dependencies struct {
Auth *auth.Auth
Auth *auth.Auth
Custom *custom.Custom
}
}
@ -67,7 +69,9 @@ func (panel *UserPanel) HandleRoute(ctx context.Context, route string) (http.Han
}
type userFormContext struct {
custom.BaseContext
httpx.FormContext
User *models.User
}
@ -75,6 +79,7 @@ func (panel *UserPanel) UserFormContext(ctx httpx.FormContext, r *http.Request)
user, err := panel.Dependencies.Auth.UserOf(r)
uctx := userFormContext{FormContext: ctx}
panel.Dependencies.Custom.Update(&uctx)
if err == nil {
uctx.User = &user.User
}

View file

@ -25,6 +25,8 @@ var (
)
func (panel *UserPanel) routePassword(ctx context.Context) http.Handler {
passwordTemplate := panel.Dependencies.Custom.Template(passwordTemplate)
return &httpx.Form[struct{}]{
Fields: []httpx.Field{
{Name: "old", Type: httpx.PasswordField, EmptyOnError: true, Label: "Current Password"},

View file

@ -17,6 +17,8 @@ var totpEnableStr string
var totpEnableTemplate = static.AssetsUser.MustParseShared("totp_enable.html", totpEnableStr)
func (panel *UserPanel) routeTOTPEnable(ctx context.Context) http.Handler {
totpEnableTemplate := panel.Dependencies.Custom.Template(totpEnableTemplate)
return &httpx.Form[struct{}]{
Fields: []httpx.Field{
{Name: "password", Type: httpx.PasswordField, EmptyOnError: true, Label: "Current Password"},
@ -73,6 +75,8 @@ type totpEnrollContext struct {
}
func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
totpEnrollTemplate := panel.Dependencies.Custom.Template(totpEnrollTemplate)
return &httpx.Form[struct{}]{
Fields: []httpx.Field{
{Name: "password", Type: httpx.PasswordField, EmptyOnError: true, Label: "Current Password"},
@ -85,6 +89,8 @@ func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
return struct{}{}, err == nil && user != nil && user.IsTOTPEnabled()
},
RenderForm: func(context httpx.FormContext, w http.ResponseWriter, r *http.Request) {
// TODO: Do we want to reuse the same function here?
user, err := panel.Dependencies.Auth.UserOf(r)
ctx := totpEnrollContext{
@ -92,6 +98,7 @@ func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
FormContext: context,
},
}
panel.Dependencies.Custom.Update(&ctx.userFormContext)
if err == nil && user != nil {
ctx.userFormContext.User = &user.User
@ -142,6 +149,8 @@ var totpDisableStr string
var totpDisableTemplate = static.AssetsUser.MustParseShared("totp_disable.html", totpDisableStr)
func (panel *UserPanel) routeTOTPDisable(ctx context.Context) http.Handler {
totpDisableTemplate := panel.Dependencies.Custom.Template(totpDisableTemplate)
return &httpx.Form[struct{}]{
Fields: []httpx.Field{
{Name: "password", Type: httpx.PasswordField, EmptyOnError: true, Label: "Current Password"},

View file

@ -8,6 +8,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
)
@ -18,9 +19,19 @@ var userTemplate = static.AssetsUser.MustParseShared(
userHTMLStr,
)
type routeUserContext struct {
custom.BaseContext
*auth.AuthUser
}
func (panel *UserPanel) routeUser(ctx context.Context) http.Handler {
return &httpx.HTMLHandler[*auth.AuthUser]{
Handler: panel.Dependencies.Auth.UserOf,
userTemplate := panel.Dependencies.Custom.Template(userTemplate)
return &httpx.HTMLHandler[routeUserContext]{
Handler: func(r *http.Request) (ruc routeUserContext, err error) {
panel.Dependencies.Custom.Update(&ruc)
ruc.AuthUser, err = panel.Dependencies.Auth.UserOf(r)
return routeUserContext{}, err
},
Template: userTemplate,
}
}