Refactor: Menu

This commit is contained in:
Tom Wiesing 2023-02-03 15:46:53 +01:00
parent a7309d5268
commit 7f820224ec
No known key found for this signature in database
16 changed files with 103 additions and 92 deletions

View file

@ -53,6 +53,17 @@ func (panel *UserPanel) Menu(r *http.Request) []component.MenuItem {
}
}
var (
menuUser = component.MenuItem{Title: "User", Path: "/user/"}
menuChangePassword = component.MenuItem{Title: "Change Password", Path: "/user/password/"}
menuSSH = component.MenuItem{Title: "SSH Keys", Path: "/user/ssh/"}
menuSSHAdd = component.MenuItem{Title: "Add New Key", Path: "/user/ssh/add/"}
menuTOTPAction = component.DummyMenuItem()
menuTOTPDisable = component.MenuItem{Title: "Disable Passcode (TOTP)", Path: "/user/totp/disable/"}
menuTOTPEnable = component.MenuItem{Title: "Enable Passcode (TOTP)", Path: "/user/totp/enable/"}
)
func (panel *UserPanel) HandleRoute(ctx context.Context, route string) (http.Handler, error) {
router := httprouter.New()
@ -114,9 +125,10 @@ type userFormContext struct {
func (panel *UserPanel) UserFormContext(tpl *templating.Template[userFormContext], last component.MenuItem, funcs ...templating.FlagFunc) func(ctx httpx.FormContext, r *http.Request) any {
funcs = append(funcs, func(flags templating.Flags, r *http.Request) templating.Flags {
flags.Crumbs = append(flags.Crumbs, component.MenuItem{})
// append the last menu item, and prepend the menuUser one!
flags.Crumbs = append(flags.Crumbs, last, last)
copy(flags.Crumbs[1:], flags.Crumbs)
flags.Crumbs[0] = component.MenuItem{Title: "User", Path: "/user/"}
flags.Crumbs[0] = menuUser
return flags
})

View file

@ -7,7 +7,6 @@ import (
_ "embed"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
@ -45,7 +44,7 @@ func (panel *UserPanel) routePassword(ctx context.Context) http.Handler {
FieldTemplate: field.PureCSSFieldTemplate,
RenderTemplate: tpl.Template(),
RenderTemplateContext: panel.UserFormContext(tpl, component.MenuItem{Title: "Change Password", Path: "/user/password/"}),
RenderTemplateContext: panel.UserFormContext(tpl, menuChangePassword),
Validate: func(r *http.Request, values map[string]string) (struct{}, error) {
old, passcode, new, new2 := values["old"], values["otp"], values["new"], values["new2"]

View file

@ -5,7 +5,6 @@ import (
"errors"
"net/http"
"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/server/assets"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
@ -45,11 +44,11 @@ func (panel *UserPanel) sshRoute(ctx context.Context) http.Handler {
tpl := sshTemplate.Prepare(
panel.Dependencies.Templating,
templating.Crumbs(
component.MenuItem{Title: "User", Path: "/user/"},
component.MenuItem{Title: "SSH Keys", Path: "/user/ssh/"},
menuUser,
menuSSH,
),
templating.Actions(
component.MenuItem{Title: "Add New Key", Path: "/user/ssh/add/"},
menuSSHAdd,
),
)
@ -136,9 +135,9 @@ func (panel *UserPanel) sshAddRoute(ctx context.Context) http.Handler {
tpl := sshAddTemplate.Prepare(
panel.Dependencies.Templating,
templating.Crumbs(
component.MenuItem{Title: "User", Path: "/user/"},
component.MenuItem{Title: "SSH Keys", Path: "/user/ssh/"},
component.MenuItem{Title: "Add New Key", Path: "/user/ssh/add/"},
menuUser,
menuSSH,
menuSSHAdd,
),
)

View file

@ -5,7 +5,6 @@ import (
"html/template"
"net/http"
"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/server/assets"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
@ -39,7 +38,7 @@ func (panel *UserPanel) routeTOTPEnable(ctx context.Context) http.Handler {
},
RenderTemplate: tpl.Template(),
RenderTemplateContext: panel.UserFormContext(tpl, component.MenuItem{Title: "Enable TOTP", Path: "/user/totp/enable/"}),
RenderTemplateContext: panel.UserFormContext(tpl, menuTOTPEnable),
Validate: func(r *http.Request, values map[string]string) (struct{}, error) {
password := values["password"]
@ -93,8 +92,8 @@ func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
tpl := totpEnrollTemplate.Prepare(
panel.Dependencies.Templating,
templating.Crumbs(
component.MenuItem{Title: "User", Path: "/user/"},
component.MenuItem{Title: "Enable TOTP", Path: "/user/totp/enable/"},
menuUser,
menuTOTPEnable,
),
)
@ -189,7 +188,7 @@ func (panel *UserPanel) routeTOTPDisable(ctx context.Context) http.Handler {
return struct{}{}, err == nil && user != nil && !user.IsTOTPEnabled()
},
RenderTemplate: tpl.Template(),
RenderTemplateContext: panel.UserFormContext(tpl, component.MenuItem{Title: "Disable TOTP", Path: "/user/totp/disable/"}),
RenderTemplateContext: panel.UserFormContext(tpl, menuTOTPDisable),
Validate: func(r *http.Request, values map[string]string) (struct{}, error) {
password, otp := values["password"], values["otp"]

View file

@ -34,21 +34,17 @@ type GrantWithURL struct {
URL template.URL
}
var (
totpActionItem = component.DummyMenuItem()
)
func (panel *UserPanel) routeUser(ctx context.Context) http.Handler {
tpl := userTemplate.Prepare(
panel.Dependencies.Templating,
templating.Crumbs(
component.MenuItem{Title: "User", Path: "/user/"},
menuUser,
),
templating.Actions(
component.MenuItem{Title: "Change Password", Path: "/user/password/"},
totpActionItem,
component.MenuItem{Title: "SSH Keys", Path: "/user/ssh/"},
menuChangePassword,
menuTOTPAction,
menuSSH,
),
)
@ -62,18 +58,12 @@ func (panel *UserPanel) routeUser(ctx context.Context) http.Handler {
// replace the totp action in the menu
var totpAction component.MenuItem
if uc.AuthUser.IsTOTPEnabled() {
totpAction = component.MenuItem{
Title: "Disable Passcode (TOTP)",
Path: "/user/totp/disable/",
}
totpAction = menuTOTPDisable
} else {
totpAction = component.MenuItem{
Title: "Enable Passcode (TOTP)",
Path: "/user/totp/enable/",
}
totpAction = menuTOTPEnable
}
funcs = []templating.FlagFunc{
templating.ReplaceAction(totpActionItem, totpAction),
templating.ReplaceAction(menuTOTPAction, totpAction),
templating.Title(uc.AuthUser.User.User),
}