Refactor html templates

This commit entirely refactors the use of html templates. Instead of
inheriting from a shared template, we insert the results into a base
template.
This commit is contained in:
Tom Wiesing 2023-01-20 14:42:37 +01:00
parent 6ede99d7c6
commit d235ee4e5c
No known key found for this signature in database
59 changed files with 869 additions and 777 deletions

View file

@ -5,7 +5,7 @@ import (
"net/http"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/sql"
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
"github.com/gorilla/sessions"
@ -17,7 +17,7 @@ type Auth struct {
Dependencies struct {
SQL *sql.SQL
UserDeleteHooks []component.UserDeleteHook
Templating *templates.Templating
Templating *templating.Templating
}
store lazy.Lazy[sessions.Store]

View file

@ -1,5 +1,4 @@
{{ template "_form.html" . }}
{{ define "form/title" }}Login Required{{ end }}
{{ template "form.html" . }}
{{ define "form/button" }}Login{{ end }}
{{ define "form/inside" }}
<div class="pure-form-group">

View file

@ -9,7 +9,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/next"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth/policy"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/ssh2/sshkeys"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
@ -20,7 +20,7 @@ type UserPanel struct {
component.Base
Dependencies struct {
Auth *auth.Auth
Templating *templates.Templating
Templating *templating.Templating
Policy *policy.Policy
Instances *instances.Instances
Next *next.Next
@ -106,30 +106,25 @@ func (panel *UserPanel) HandleRoute(ctx context.Context, route string) (http.Han
}
type userFormContext struct {
templates.BaseContext
templating.RuntimeFlags
httpx.FormContext
User *models.User
}
func (panel *UserPanel) UserFormContext2(tpl *templates.Template[userFormContext], last component.MenuItem, gaps ...templates.BaseContextGaps) func(ctx httpx.FormContext, r *http.Request) any {
var g templates.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,
}
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{})
copy(flags.Crumbs[1:], flags.Crumbs)
flags.Crumbs[0] = component.MenuItem{Title: "User", Path: "/user/"}
return flags
})
return templates.MappedHandler(tpl, func(ctx httpx.FormContext, r *http.Request) (userFormContext, templates.BaseContextGaps) {
return func(ctx httpx.FormContext, r *http.Request) any {
uctx := userFormContext{FormContext: ctx}
if user, err := panel.Dependencies.Auth.UserOf(r); err == nil {
uctx.User = &user.User
}
return uctx, g
})
return tpl.Context(r, uctx, funcs...)
}
}

View file

@ -9,14 +9,19 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets"
templating "github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx/field"
)
//go:embed "templates/password.html"
var passwordHTML []byte
var passwordTemplate = templating.Parse[userFormContext]("password.html", passwordHTML, assets.AssetsUser)
var passwordTemplate = templating.Parse[userFormContext](
"password.html", passwordHTML, httpx.FormTemplate,
templating.Title("Change Password"),
templating.Assets(assets.AssetsUser),
)
var (
errPasswordsNotIdentical = errors.New("passwords are not identical")
@ -40,7 +45,7 @@ func (panel *UserPanel) routePassword(ctx context.Context) http.Handler {
FieldTemplate: field.PureCSSFieldTemplate,
RenderTemplate: tpl.Template(),
RenderTemplateContext: panel.UserFormContext2(tpl, component.MenuItem{Title: "Change Password", Path: "/user/password/"}),
RenderTemplateContext: panel.UserFormContext(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"]

View file

@ -8,7 +8,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/server/assets"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx/field"
@ -22,10 +22,15 @@ import (
//go:embed "templates/ssh.html"
var sshHTML []byte
var sshTemplate = templates.Parse[SSHTemplateContext]("ssh.html", sshHTML, assets.AssetsUser)
var sshTemplate = templating.Parse[SSHTemplateContext](
"ssh.html", sshHTML, nil,
templating.Title("SSH Keys"),
templating.Assets(assets.AssetsUser),
)
type SSHTemplateContext struct {
templates.BaseContext
templating.RuntimeFlags
Keys []models.Keys
@ -37,15 +42,16 @@ type SSHTemplateContext struct {
}
func (panel *UserPanel) sshRoute(ctx context.Context) http.Handler {
tpl := sshTemplate.Prepare(panel.Dependencies.Templating, templates.BaseContextGaps{
Crumbs: []component.MenuItem{
{Title: "User", Path: "/user/"},
{Title: "SSH Keys", Path: "/user/ssh/"},
},
Actions: []component.MenuItem{
{Title: "Add New Key", Path: "/user/ssh/add/"},
},
})
tpl := sshTemplate.Prepare(
panel.Dependencies.Templating,
templating.Crumbs(
component.MenuItem{Title: "User", Path: "/user/"},
component.MenuItem{Title: "SSH Keys", Path: "/user/ssh/"},
),
templating.Actions(
component.MenuItem{Title: "Add New Key", Path: "/user/ssh/add/"},
),
)
return tpl.HTMLHandler(func(r *http.Request) (sc SSHTemplateContext, err error) {
user, err := panel.Dependencies.Auth.UserOf(r)
@ -114,7 +120,11 @@ func (panel *UserPanel) sshDeleteRoute(ctx context.Context) http.Handler {
//go:embed "templates/ssh_add.html"
var sshAddHTML []byte
var sshAddTemplate = templates.ParseForm("ssh_add.html", sshAddHTML, assets.AssetsUser)
var sshAddTemplate = templating.ParseForm(
"ssh_add.html", sshAddHTML, httpx.FormTemplate,
templating.Title("Add SSH Key"),
templating.Assets(assets.AssetsUser),
)
type addKeyResult struct {
User *auth.AuthUser
@ -123,13 +133,14 @@ type addKeyResult struct {
}
func (panel *UserPanel) sshAddRoute(ctx context.Context) http.Handler {
tpl := sshAddTemplate.Prepare(panel.Dependencies.Templating, templates.BaseContextGaps{
Crumbs: []component.MenuItem{
{Title: "User", Path: "/user/"},
{Title: "SSH Keys", Path: "/user/ssh/"},
{Title: "Add New Key", Path: "/user/ssh/add/"},
},
})
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/"},
),
)
return &httpx.Form[addKeyResult]{
Fields: []field.Field{
@ -139,7 +150,7 @@ func (panel *UserPanel) sshAddRoute(ctx context.Context) http.Handler {
FieldTemplate: field.PureCSSFieldTemplate,
RenderTemplate: tpl.Template(),
RenderTemplateContext: templates.FormTemplateContext(tpl),
RenderTemplateContext: templating.FormTemplateContext(tpl),
Validate: func(r *http.Request, values map[string]string) (ak addKeyResult, err error) {
ak.User, err = panel.Dependencies.Auth.UserOf(r)

View file

@ -1,3 +1,2 @@
{{ template "_form.html" . }}
{{ define "form/title" }}Change Password{{ end }}
{{ template "form.html" . }}
{{ define "form/button" }}Update{{ end }}

View file

@ -1,8 +1,3 @@
{{ template "_base.html" . }}
{{ define "title" }}SSH Keys{{ end }}
{{ define "content" }}
<div class="pure-u-1">
<p>
This page allows you to add, view and remove ssh keys to and from your distillery account.
@ -101,4 +96,3 @@ Host {{ .Domain }}.proxy
</code>
</div>
</div>
{{ end }}

View file

@ -1,7 +1,4 @@
{{ template "_form.html" . }}
{{ define "form/title" }}Add SSH Key{{ end }}
{{ define "form/button" }}Add{{ end }}
{{ define "form/inside" }}
<div>
<p>

View file

@ -1,5 +1,5 @@
{{ template "_form.html" . }}
{{ define "form/title" }}Disable TOTP{{ end }}
{{ template "form.html" . }}
{{ define "form/button" }}Disable{{ end }}
{{ define "form/inside" }}

View file

@ -1,5 +1,4 @@
{{ template "_form.html" . }}
{{ define "form/title" }}Enable TOTP{{ end }}
{{ template "form.html" . }}
{{ define "form/button" }}Enable{{ end }}
{{ define "form/inside" }}
<div>

View file

@ -1,5 +1,3 @@
{{ template "_form.html" . }}
{{ define "form/title" }}Enable TOTP{{ end }}
{{ define "form/button" }}Enable{{ end }}
{{ define "form/inside" }}
<div>

View file

@ -1,7 +1,3 @@
{{ template "_base.html" . }}
{{ define "title" }}{{ .User.User }}{{ end }}
{{ define "content" }}
<div class="pure-u-1">
<p>
<ul>
@ -79,6 +75,4 @@
</table>
</div>
</div>
</div>
{{ end }}
</div>

View file

@ -8,7 +8,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/server/assets"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx/field"
@ -17,7 +17,12 @@ import (
//go:embed "templates/totp_enable.html"
var totpEnableHTML []byte
var totpEnable = templates.Parse[userFormContext]("totp_enable.html", totpEnableHTML, assets.AssetsUser)
var totpEnable = templating.Parse[userFormContext](
"totp_enable.html", totpEnableHTML, httpx.FormTemplate,
templating.Title("Enable TOTP"),
templating.Assets(assets.AssetsUser),
)
func (panel *UserPanel) routeTOTPEnable(ctx context.Context) http.Handler {
tpl := totpEnable.Prepare(panel.Dependencies.Templating)
@ -34,7 +39,7 @@ func (panel *UserPanel) routeTOTPEnable(ctx context.Context) http.Handler {
},
RenderTemplate: tpl.Template(),
RenderTemplateContext: panel.UserFormContext2(tpl, component.MenuItem{Title: "Enable TOTP", Path: "/user/totp/enable/"}),
RenderTemplateContext: panel.UserFormContext(tpl, component.MenuItem{Title: "Enable TOTP", Path: "/user/totp/enable/"}),
Validate: func(r *http.Request, values map[string]string) (struct{}, error) {
password := values["password"]
@ -69,7 +74,12 @@ func (panel *UserPanel) routeTOTPEnable(ctx context.Context) http.Handler {
//go:embed "templates/totp_enroll.html"
var totpEnrollHTML []byte
var totpEnrollTemplate = templates.Parse[totpEnrollContext]("totp_enroll.html", totpEnrollHTML, assets.AssetsUser)
var totpEnrollTemplate = templating.Parse[totpEnrollContext](
"totp_enroll.html", totpEnrollHTML, httpx.FormTemplate,
templating.Title("Enable TOTP"),
templating.Assets(assets.AssetsUser),
)
type totpEnrollContext struct {
userFormContext
@ -80,12 +90,13 @@ type totpEnrollContext struct {
}
func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
tpl := totpEnrollTemplate.Prepare(panel.Dependencies.Templating, templates.BaseContextGaps{
Crumbs: []component.MenuItem{
{Title: "User", Path: "/user/"},
{Title: "Enable TOTP", Path: "/user/totp/enable/"},
},
})
tpl := totpEnrollTemplate.Prepare(
panel.Dependencies.Templating,
templating.Crumbs(
component.MenuItem{Title: "User", Path: "/user/"},
component.MenuItem{Title: "Enable TOTP", Path: "/user/totp/enable/"},
),
)
return &httpx.Form[struct{}]{
Fields: []field.Field{
@ -98,9 +109,7 @@ func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
user, err := panel.Dependencies.Auth.UserOf(r)
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?
RenderTemplateContext: func(context httpx.FormContext, r *http.Request) any {
user, err := panel.Dependencies.Auth.UserOf(r)
ctx := totpEnrollContext{
@ -120,8 +129,10 @@ func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
ctx.TOTPURL = template.URL(secret.URL())
}
}
tpl.Execute(w, r, ctx)
return tpl.Context(r, ctx)
},
RenderTemplate: tpl.Template(),
Validate: func(r *http.Request, values map[string]string) (struct{}, error) {
password, otp := values["password"], values["otp"]
@ -156,7 +167,12 @@ func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
//go:embed "templates/totp_disable.html"
var totpDisableHTML []byte
var totpDisableTemplate = templates.Parse[userFormContext]("totp_disable.html", totpDisableHTML, assets.AssetsUser)
var totpDisableTemplate = templating.Parse[userFormContext](
"totp_disable.html", totpDisableHTML, httpx.FormTemplate,
templating.Title("Disable TOTP"),
templating.Assets(assets.AssetsUser),
)
func (panel *UserPanel) routeTOTPDisable(ctx context.Context) http.Handler {
tpl := totpDisableTemplate.Prepare(panel.Dependencies.Templating)
@ -173,7 +189,7 @@ func (panel *UserPanel) routeTOTPDisable(ctx context.Context) http.Handler {
return struct{}{}, err == nil && user != nil && !user.IsTOTPEnabled()
},
RenderTemplate: tpl.Template(),
RenderTemplateContext: panel.UserFormContext2(tpl, component.MenuItem{Title: "Disable TOTP", Path: "/user/totp/disable/"}),
RenderTemplateContext: panel.UserFormContext(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"]

View file

@ -10,16 +10,20 @@ 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/server/assets"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/FAU-CDI/wisski-distillery/internal/models"
)
//go:embed "templates/user.html"
var userHTML []byte
var userTemplate = templates.Parse[userContext]("user.html", userHTML, assets.AssetsUser)
var userTemplate = templating.Parse[userContext](
"user.html", userHTML, nil,
templating.Assets(assets.AssetsUser),
)
type userContext struct {
templates.BaseContext
templating.RuntimeFlags
*auth.AuthUser
Grants []GrantWithURL
@ -31,41 +35,47 @@ type GrantWithURL struct {
}
func (panel *UserPanel) routeUser(ctx context.Context) http.Handler {
tpl := userTemplate.Prepare(panel.Dependencies.Templating, templates.BaseContextGaps{
Crumbs: []component.MenuItem{
{Title: "User", Path: "/user/"},
},
Actions: []component.MenuItem{
{Title: "Change Password", Path: "/user/password/"},
{Title: "*to be replaced*", Path: ""},
{Title: "SSH Keys", Path: "/user/ssh/"},
},
})
tpl := userTemplate.Prepare(
panel.Dependencies.Templating,
templating.Crumbs(
component.MenuItem{Title: "User", Path: "/user/"},
),
templating.Actions(
component.MenuItem{Title: "Change Password", Path: "/user/password/"},
component.DummyMenuItem,
component.MenuItem{Title: "SSH Keys", Path: "/user/ssh/"},
),
)
return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *templates.BaseContextGaps) (uc userContext, err error) {
return tpl.HTMLHandlerWithFlags(func(r *http.Request) (uc userContext, funcs []templating.FlagFunc, err error) {
// find the user
uc.AuthUser, err = panel.Dependencies.Auth.UserOf(r)
if err != nil || uc.AuthUser == nil {
return uc, err
return uc, nil, err
}
// build the gaps
// replace the totp action in the menu
var totpAction component.MenuItem
if uc.AuthUser.IsTOTPEnabled() {
gaps.Actions[1] = component.MenuItem{
totpAction = component.MenuItem{
Title: "Disable Passcode (TOTP)",
Path: "/user/totp/disable/",
}
} else {
gaps.Actions[1] = component.MenuItem{
totpAction = component.MenuItem{
Title: "Enable Passcode (TOTP)",
Path: "/user/totp/enable/",
}
}
funcs = []templating.FlagFunc{
templating.ReplaceAction(1, totpAction),
templating.Title(uc.AuthUser.User.User),
}
// find the grants
grants, err := panel.Dependencies.Policy.User(r.Context(), uc.AuthUser.User.User)
if err != nil {
return uc, err
return uc, nil, err
}
uc.Grants = make([]GrantWithURL, len(grants))
@ -74,11 +84,11 @@ func (panel *UserPanel) routeUser(ctx context.Context) http.Handler {
url, err := panel.Dependencies.Next.Next(r.Context(), grant.Slug, "/")
if err != nil {
return uc, err
return uc, nil, err
}
uc.Grants[i].URL = template.URL(url)
}
return uc, err
return uc, funcs, err
})
}

View file

@ -9,7 +9,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templates"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
"github.com/FAU-CDI/wisski-distillery/pkg/httpx/field"
"github.com/gorilla/sessions"
@ -120,7 +120,12 @@ func (auth *Auth) Logout(w http.ResponseWriter, r *http.Request) error {
//go:embed "login.html"
var loginHTML []byte
var loginTemplate = templates.ParseForm("login.html", loginHTML, assets.AssetsUser)
var loginTemplate = templating.ParseForm(
"login.html", loginHTML, httpx.FormTemplate,
templating.Title("Login Required"),
templating.Assets(assets.AssetsUser),
)
var loginResponse = httpx.Response{
ContentType: "text/plain",
@ -131,7 +136,15 @@ var errLoginFailed = errors.New("Login failed")
// authLogin implements a view to login a user
func (auth *Auth) authLogin(ctx context.Context) http.Handler {
tpl := loginTemplate.Prepare(auth.Dependencies.Templating)
tpl := loginTemplate.Prepare(
auth.Dependencies.Templating,
func(flags templating.Flags, r *http.Request) templating.Flags {
flags.Crumbs = []component.MenuItem{
{Title: "Login", Path: template.URL(r.URL.RequestURI())},
}
return flags
},
)
return &httpx.Form[*AuthUser]{
Fields: []field.Field{
@ -141,16 +154,13 @@ func (auth *Auth) authLogin(ctx context.Context) http.Handler {
},
FieldTemplate: field.PureCSSFieldTemplate,
RenderForm: func(context httpx.FormContext, w http.ResponseWriter, r *http.Request) {
if context.Err != nil {
context.Err = errLoginFailed
RenderTemplateContext: func(ctx httpx.FormContext, r *http.Request) any {
if ctx.Err != nil {
ctx.Err = errLoginFailed
}
tpl.Execute(w, r, templates.BaseFormContext{FormContext: context}, templates.BaseContextGaps{
Crumbs: []component.MenuItem{
{Title: "Login", Path: template.URL(r.URL.RequestURI())},
},
})
return tpl.Context(r, templating.NewFormContext(ctx))
},
RenderTemplate: tpl.Template(),
Validate: func(r *http.Request, values map[string]string) (*AuthUser, error) {
username, password, passcode := values["username"], values["password"], values["otp"]