custom: Improve templating of assets
This commit is contained in:
parent
7d0fb60d67
commit
b6bf0a8900
19 changed files with 516 additions and 432 deletions
|
|
@ -112,20 +112,24 @@ type userFormContext struct {
|
|||
User *models.User
|
||||
}
|
||||
|
||||
func (panel *UserPanel) UserFormContext(last component.MenuItem, gaps custom.BaseContextGaps) func(ctx httpx.FormContext, r *http.Request) any {
|
||||
gaps.Crumbs = []component.MenuItem{
|
||||
func (panel *UserPanel) UserFormContext2(tpl *custom.Template[userFormContext], last component.MenuItem, gaps ...custom.BaseContextGaps) func(ctx httpx.FormContext, r *http.Request) any {
|
||||
var g custom.BaseContextGaps
|
||||
if len(gaps) > 1 {
|
||||
panic("UserFormContext2: gaps must be of length 0 or 1")
|
||||
}
|
||||
if len(gaps) == 1 {
|
||||
g = gaps[0]
|
||||
}
|
||||
g.Crumbs = []component.MenuItem{
|
||||
{Title: "User", Path: "/user/"},
|
||||
last,
|
||||
}
|
||||
return func(ctx httpx.FormContext, r *http.Request) any {
|
||||
user, err := panel.Dependencies.Auth.UserOf(r)
|
||||
|
||||
return custom.MappedHandler(tpl, func(ctx httpx.FormContext, r *http.Request) (userFormContext, custom.BaseContextGaps) {
|
||||
uctx := userFormContext{FormContext: ctx}
|
||||
panel.Dependencies.Custom.Update(&uctx, r, gaps)
|
||||
if err == nil {
|
||||
if user, err := panel.Dependencies.Auth.UserOf(r); err == nil {
|
||||
uctx.User = &user.User
|
||||
}
|
||||
return uctx
|
||||
}
|
||||
|
||||
return uctx, g
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,8 +15,8 @@ import (
|
|||
)
|
||||
|
||||
//go:embed "templates/password.html"
|
||||
var passwordHTMLString string
|
||||
var passwordTemplate = static.AssetsUser.MustParseShared("password.html", passwordHTMLString)
|
||||
var passwordHTML []byte
|
||||
var passwordTemplate = custom.Parse[userFormContext]("password.html", passwordHTML, static.AssetsUser)
|
||||
|
||||
var (
|
||||
errPasswordsNotIdentical = errors.New("passwords are not identical")
|
||||
|
|
@ -28,7 +28,7 @@ var (
|
|||
)
|
||||
|
||||
func (panel *UserPanel) routePassword(ctx context.Context) http.Handler {
|
||||
passwordTemplate := panel.Dependencies.Custom.Template(passwordTemplate)
|
||||
tpl := passwordTemplate.Prepare(panel.Dependencies.Custom)
|
||||
|
||||
return &httpx.Form[struct{}]{
|
||||
Fields: []field.Field{
|
||||
|
|
@ -39,8 +39,8 @@ func (panel *UserPanel) routePassword(ctx context.Context) http.Handler {
|
|||
},
|
||||
FieldTemplate: field.PureCSSFieldTemplate,
|
||||
|
||||
RenderTemplate: passwordTemplate,
|
||||
RenderTemplateContext: panel.UserFormContext(component.MenuItem{Title: "Change Password", Path: "/user/password/"}, custom.BaseContextGaps{}),
|
||||
RenderTemplate: tpl.Template(),
|
||||
RenderTemplateContext: panel.UserFormContext2(tpl, component.MenuItem{Title: "Change Password", Path: "/user/password/"}),
|
||||
|
||||
Validate: func(r *http.Request, values map[string]string) (struct{}, error) {
|
||||
old, passcode, new, new2 := values["old"], values["otp"], values["new"], values["new2"]
|
||||
|
|
|
|||
|
|
@ -21,8 +21,8 @@ import (
|
|||
)
|
||||
|
||||
//go:embed "templates/ssh.html"
|
||||
var sshTemplateStr string
|
||||
var sshTemplate = static.AssetsUser.MustParseShared("ssh.html", sshTemplateStr)
|
||||
var sshHTML []byte
|
||||
var sshTemplate = custom.Parse[SSHTemplateContext]("ssh.html", sshHTML, static.AssetsUser)
|
||||
|
||||
type SSHTemplateContext struct {
|
||||
custom.BaseContext
|
||||
|
|
@ -37,8 +37,7 @@ type SSHTemplateContext struct {
|
|||
}
|
||||
|
||||
func (panel *UserPanel) sshRoute(ctx context.Context) http.Handler {
|
||||
sshTemplate := panel.Dependencies.Custom.Template(sshTemplate)
|
||||
gaps := custom.BaseContextGaps{
|
||||
tpl := sshTemplate.Prepare(panel.Dependencies.Custom, custom.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "User", Path: "/user/"},
|
||||
{Title: "SSH Keys", Path: "/user/ssh/"},
|
||||
|
|
@ -46,48 +45,33 @@ func (panel *UserPanel) sshRoute(ctx context.Context) http.Handler {
|
|||
Actions: []component.MenuItem{
|
||||
{Title: "Add New Key", Path: "/user/ssh/add/"},
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
return httpx.HTMLHandler[SSHTemplateContext]{
|
||||
Handler: func(r *http.Request) (sc SSHTemplateContext, err error) {
|
||||
panel.Dependencies.Custom.Update(&sc, r, gaps)
|
||||
return tpl.HTMLHandler(func(r *http.Request) (sc SSHTemplateContext, err error) {
|
||||
user, err := panel.Dependencies.Auth.UserOf(r)
|
||||
if err != nil {
|
||||
return sc, err
|
||||
}
|
||||
|
||||
user, err := panel.Dependencies.Auth.UserOf(r)
|
||||
if err != nil {
|
||||
return sc, err
|
||||
}
|
||||
sc.Domain = panel.Config.DefaultDomain
|
||||
sc.Port = panel.Config.PublicSSHPort
|
||||
|
||||
sc.Domain = panel.Config.DefaultDomain
|
||||
sc.Port = panel.Config.PublicSSHPort
|
||||
// pick the first domain that the user has access to as an example
|
||||
grants, err := panel.Dependencies.Policy.User(r.Context(), user.User.User)
|
||||
if err != nil && len(grants) > 0 {
|
||||
sc.Slug = grants[0].Slug
|
||||
} else {
|
||||
sc.Slug = "example"
|
||||
}
|
||||
sc.Hostname = panel.Config.HostFromSlug(sc.Slug)
|
||||
|
||||
// pick the first domain that the user has access to as an example
|
||||
grants, err := panel.Dependencies.Policy.User(r.Context(), user.User.User)
|
||||
if err != nil && len(grants) > 0 {
|
||||
sc.Slug = grants[0].Slug
|
||||
} else {
|
||||
sc.Slug = "example"
|
||||
}
|
||||
sc.Hostname = panel.Config.HostFromSlug(sc.Slug)
|
||||
sc.Keys, err = panel.Dependencies.Keys.Keys(r.Context(), user.User.User)
|
||||
if err != nil {
|
||||
return sc, err
|
||||
}
|
||||
|
||||
sc.Keys, err = panel.Dependencies.Keys.Keys(r.Context(), user.User.User)
|
||||
if err != nil {
|
||||
return sc, err
|
||||
}
|
||||
|
||||
return sc, nil
|
||||
},
|
||||
Template: sshTemplate,
|
||||
}
|
||||
}
|
||||
|
||||
//go:embed "templates/ssh_add.html"
|
||||
var sshAddTemplateStr string
|
||||
var sshAddTemplate = static.AssetsUser.MustParseShared("ssh_add.html", sshAddTemplateStr)
|
||||
|
||||
type addKeyResult struct {
|
||||
User *auth.AuthUser
|
||||
Comment string
|
||||
Key ssh.PublicKey
|
||||
return sc, nil
|
||||
})
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
@ -128,15 +112,24 @@ func (panel *UserPanel) sshDeleteRoute(ctx context.Context) http.Handler {
|
|||
})
|
||||
}
|
||||
|
||||
//go:embed "templates/ssh_add.html"
|
||||
var sshAddHTML []byte
|
||||
var sshAddTemplate = custom.ParseForm("ssh_add.html", sshAddHTML, static.AssetsUser)
|
||||
|
||||
type addKeyResult struct {
|
||||
User *auth.AuthUser
|
||||
Comment string
|
||||
Key ssh.PublicKey
|
||||
}
|
||||
|
||||
func (panel *UserPanel) sshAddRoute(ctx context.Context) http.Handler {
|
||||
sshAddTemplate := panel.Dependencies.Custom.Template(sshAddTemplate)
|
||||
gaps := custom.BaseContextGaps{
|
||||
tpl := sshAddTemplate.Prepare(panel.Dependencies.Custom, custom.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "User", Path: "/user/"},
|
||||
{Title: "SSH Keys", Path: "/user/ssh/"},
|
||||
{Title: "Add New Key", Path: "/user/ssh/add/"},
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
return &httpx.Form[addKeyResult]{
|
||||
Fields: []field.Field{
|
||||
|
|
@ -145,10 +138,8 @@ func (panel *UserPanel) sshAddRoute(ctx context.Context) http.Handler {
|
|||
},
|
||||
FieldTemplate: field.PureCSSFieldTemplate,
|
||||
|
||||
RenderTemplate: sshAddTemplate,
|
||||
RenderTemplateContext: func(ctx httpx.FormContext, r *http.Request) any {
|
||||
return panel.Dependencies.Custom.NewForm(ctx, r, gaps)
|
||||
},
|
||||
RenderTemplate: tpl.Template(),
|
||||
RenderTemplateContext: custom.FormTemplateContext(tpl),
|
||||
|
||||
Validate: func(r *http.Request, values map[string]string) (ak addKeyResult, err error) {
|
||||
ak.User, err = panel.Dependencies.Auth.UserOf(r)
|
||||
|
|
|
|||
|
|
@ -16,11 +16,11 @@ import (
|
|||
)
|
||||
|
||||
//go:embed "templates/totp_enable.html"
|
||||
var totpEnableStr string
|
||||
var totpEnableTemplate = static.AssetsUser.MustParseShared("totp_enable.html", totpEnableStr)
|
||||
var totpEnableHTML []byte
|
||||
var totpEnable = custom.Parse[userFormContext]("totp_enable.html", totpEnableHTML, static.AssetsUser)
|
||||
|
||||
func (panel *UserPanel) routeTOTPEnable(ctx context.Context) http.Handler {
|
||||
totpEnableTemplate := panel.Dependencies.Custom.Template(totpEnableTemplate)
|
||||
tpl := totpEnable.Prepare(panel.Dependencies.Custom)
|
||||
|
||||
return &httpx.Form[struct{}]{
|
||||
Fields: []field.Field{
|
||||
|
|
@ -33,8 +33,8 @@ func (panel *UserPanel) routeTOTPEnable(ctx context.Context) http.Handler {
|
|||
return struct{}{}, err == nil && user != nil && user.IsTOTPEnabled()
|
||||
},
|
||||
|
||||
RenderTemplate: totpEnableTemplate,
|
||||
RenderTemplateContext: panel.UserFormContext(component.MenuItem{Title: "Enable TOTP", Path: "/user/totp/enable/"}, custom.BaseContextGaps{}),
|
||||
RenderTemplate: tpl.Template(),
|
||||
RenderTemplateContext: panel.UserFormContext2(tpl, component.MenuItem{Title: "Enable TOTP", Path: "/user/totp/enable/"}),
|
||||
|
||||
Validate: func(r *http.Request, values map[string]string) (struct{}, error) {
|
||||
password := values["password"]
|
||||
|
|
@ -68,8 +68,8 @@ func (panel *UserPanel) routeTOTPEnable(ctx context.Context) http.Handler {
|
|||
}
|
||||
|
||||
//go:embed "templates/totp_enroll.html"
|
||||
var totpEnrollStr string
|
||||
var totpEnrollTemplate = static.AssetsUser.MustParseShared("totp_enroll.html", totpEnrollStr)
|
||||
var totpEnrollHTML []byte
|
||||
var totpEnrollTemplate = custom.Parse[totpEnrollContext]("totp_enroll.html", totpEnrollHTML, static.AssetsUser)
|
||||
|
||||
type totpEnrollContext struct {
|
||||
userFormContext
|
||||
|
|
@ -80,13 +80,13 @@ type totpEnrollContext struct {
|
|||
}
|
||||
|
||||
func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
|
||||
totpEnrollTemplate := panel.Dependencies.Custom.Template(totpEnrollTemplate)
|
||||
gaps := custom.BaseContextGaps{
|
||||
tpl := totpEnrollTemplate.Prepare(panel.Dependencies.Custom, custom.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "User", Path: "/user/"},
|
||||
{Title: "Enable TOTP", Path: "/user/totp/enable/"},
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
return &httpx.Form[struct{}]{
|
||||
Fields: []field.Field{
|
||||
{Name: "password", Type: field.Password, Autocomplete: field.CurrentPassword, EmptyOnError: true, Label: "Current Password"},
|
||||
|
|
@ -108,7 +108,6 @@ func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
|
|||
FormContext: context,
|
||||
},
|
||||
}
|
||||
panel.Dependencies.Custom.Update(&ctx.userFormContext, r, gaps)
|
||||
|
||||
if err == nil && user != nil {
|
||||
ctx.userFormContext.User = &user.User
|
||||
|
|
@ -121,7 +120,7 @@ func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
|
|||
ctx.TOTPURL = template.URL(secret.URL())
|
||||
}
|
||||
}
|
||||
httpx.WriteHTML(ctx, nil, totpEnrollTemplate, "", w, r)
|
||||
tpl.Execute(w, r, ctx)
|
||||
},
|
||||
|
||||
Validate: func(r *http.Request, values map[string]string) (struct{}, error) {
|
||||
|
|
@ -156,11 +155,11 @@ func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
|
|||
}
|
||||
|
||||
//go:embed "templates/totp_disable.html"
|
||||
var totpDisableStr string
|
||||
var totpDisableTemplate = static.AssetsUser.MustParseShared("totp_disable.html", totpDisableStr)
|
||||
var totpDisableHTML []byte
|
||||
var totpDisableTemplate = custom.Parse[userFormContext]("totp_disable.html", totpDisableHTML, static.AssetsUser)
|
||||
|
||||
func (panel *UserPanel) routeTOTPDisable(ctx context.Context) http.Handler {
|
||||
totpDisableTemplate := panel.Dependencies.Custom.Template(totpDisableTemplate)
|
||||
tpl := totpDisableTemplate.Prepare(panel.Dependencies.Custom)
|
||||
|
||||
return &httpx.Form[struct{}]{
|
||||
Fields: []field.Field{
|
||||
|
|
@ -173,8 +172,8 @@ func (panel *UserPanel) routeTOTPDisable(ctx context.Context) http.Handler {
|
|||
user, err := panel.Dependencies.Auth.UserOf(r)
|
||||
return struct{}{}, err == nil && user != nil && !user.IsTOTPEnabled()
|
||||
},
|
||||
RenderTemplate: totpDisableTemplate,
|
||||
RenderTemplateContext: panel.UserFormContext(component.MenuItem{Title: "Disable TOTP", Path: "/user/totp/disable/"}, custom.BaseContextGaps{}),
|
||||
RenderTemplate: tpl.Template(),
|
||||
RenderTemplateContext: panel.UserFormContext2(tpl, component.MenuItem{Title: "Disable TOTP", Path: "/user/totp/disable/"}),
|
||||
|
||||
Validate: func(r *http.Request, values map[string]string) (struct{}, error) {
|
||||
password, otp := values["password"], values["otp"]
|
||||
|
|
|
|||
|
|
@ -12,17 +12,13 @@ import (
|
|||
"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/internal/models"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
|
||||
)
|
||||
|
||||
//go:embed "templates/user.html"
|
||||
var userHTMLStr string
|
||||
var userTemplate = static.AssetsUser.MustParseShared(
|
||||
"user.html",
|
||||
userHTMLStr,
|
||||
)
|
||||
var userHTML []byte
|
||||
var userTemplate = custom.Parse[userContext]("user.html", userHTML, static.AssetsUser)
|
||||
|
||||
type routeUserContext struct {
|
||||
type userContext struct {
|
||||
custom.BaseContext
|
||||
*auth.AuthUser
|
||||
|
||||
|
|
@ -35,8 +31,7 @@ type GrantWithURL struct {
|
|||
}
|
||||
|
||||
func (panel *UserPanel) routeUser(ctx context.Context) http.Handler {
|
||||
userTemplate := panel.Dependencies.Custom.Template(userTemplate)
|
||||
gaps := custom.BaseContextGaps{
|
||||
tpl := userTemplate.Prepare(panel.Dependencies.Custom, custom.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "User", Path: "/user/"},
|
||||
},
|
||||
|
|
@ -45,50 +40,45 @@ func (panel *UserPanel) routeUser(ctx context.Context) http.Handler {
|
|||
{Title: "*to be replaced*", Path: ""},
|
||||
{Title: "SSH Keys", Path: "/user/ssh/"},
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
return &httpx.HTMLHandler[routeUserContext]{
|
||||
Handler: func(r *http.Request) (ruc routeUserContext, err error) {
|
||||
// find the user
|
||||
ruc.AuthUser, err = panel.Dependencies.Auth.UserOf(r)
|
||||
if err != nil || ruc.AuthUser == nil {
|
||||
return ruc, err
|
||||
return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *custom.BaseContextGaps) (uc userContext, err error) {
|
||||
// find the user
|
||||
uc.AuthUser, err = panel.Dependencies.Auth.UserOf(r)
|
||||
if err != nil || uc.AuthUser == nil {
|
||||
return uc, err
|
||||
}
|
||||
|
||||
// build the gaps
|
||||
if uc.AuthUser.IsTOTPEnabled() {
|
||||
gaps.Actions[1] = component.MenuItem{
|
||||
Title: "Disable Passcode (TOTP)",
|
||||
Path: "/user/totp/disable/",
|
||||
}
|
||||
|
||||
// build the gaps
|
||||
gaps := gaps.Clone()
|
||||
if ruc.AuthUser.IsTOTPEnabled() {
|
||||
gaps.Actions[1] = component.MenuItem{
|
||||
Title: "Disable Passcode (TOTP)",
|
||||
Path: "/user/totp/disable/",
|
||||
}
|
||||
} else {
|
||||
gaps.Actions[1] = component.MenuItem{
|
||||
Title: "Enable Passcode (TOTP)",
|
||||
Path: "/user/totp/enable/",
|
||||
}
|
||||
} else {
|
||||
gaps.Actions[1] = component.MenuItem{
|
||||
Title: "Enable Passcode (TOTP)",
|
||||
Path: "/user/totp/enable/",
|
||||
}
|
||||
panel.Dependencies.Custom.Update(&ruc, r, gaps)
|
||||
}
|
||||
|
||||
// find the grants
|
||||
grants, err := panel.Dependencies.Policy.User(r.Context(), ruc.AuthUser.User.User)
|
||||
// find the grants
|
||||
grants, err := panel.Dependencies.Policy.User(r.Context(), uc.AuthUser.User.User)
|
||||
if err != nil {
|
||||
return uc, err
|
||||
}
|
||||
|
||||
uc.Grants = make([]GrantWithURL, len(grants))
|
||||
for i, grant := range grants {
|
||||
uc.Grants[i].Grant = grant
|
||||
|
||||
url, err := panel.Dependencies.Next.Next(r.Context(), grant.Slug, "/")
|
||||
if err != nil {
|
||||
return ruc, err
|
||||
return uc, err
|
||||
}
|
||||
uc.Grants[i].URL = template.URL(url)
|
||||
}
|
||||
|
||||
ruc.Grants = make([]GrantWithURL, len(grants))
|
||||
for i, grant := range grants {
|
||||
ruc.Grants[i].Grant = grant
|
||||
|
||||
url, err := panel.Dependencies.Next.Next(r.Context(), grant.Slug, "/")
|
||||
if err != nil {
|
||||
return ruc, err
|
||||
}
|
||||
ruc.Grants[i].URL = template.URL(url)
|
||||
}
|
||||
|
||||
return ruc, err
|
||||
},
|
||||
Template: userTemplate,
|
||||
}
|
||||
return uc, err
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue