templating: Move actions into template
This commit is contained in:
parent
313af2b9e3
commit
202599aaeb
23 changed files with 153 additions and 105 deletions
|
|
@ -84,8 +84,8 @@ type userFormContext struct {
|
|||
User *models.User
|
||||
}
|
||||
|
||||
func (panel *UserPanel) UserFormContext(last component.MenuItem) func(ctx httpx.FormContext, r *http.Request) any {
|
||||
crumbs := []component.MenuItem{
|
||||
func (panel *UserPanel) UserFormContext(last component.MenuItem, gaps custom.BaseContextGaps) func(ctx httpx.FormContext, r *http.Request) any {
|
||||
gaps.Crumbs = []component.MenuItem{
|
||||
{Title: "User", Path: "/user/"},
|
||||
last,
|
||||
}
|
||||
|
|
@ -93,7 +93,7 @@ func (panel *UserPanel) UserFormContext(last component.MenuItem) func(ctx httpx.
|
|||
user, err := panel.Dependencies.Auth.UserOf(r)
|
||||
|
||||
uctx := userFormContext{FormContext: ctx}
|
||||
panel.Dependencies.Custom.Update(&uctx, r, crumbs)
|
||||
panel.Dependencies.Custom.Update(&uctx, r, gaps)
|
||||
if err == nil {
|
||||
uctx.User = &user.User
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import (
|
|||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||
"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"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/httpx/field"
|
||||
)
|
||||
|
|
@ -38,7 +39,7 @@ 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/"}),
|
||||
RenderTemplateContext: panel.UserFormContext(component.MenuItem{Title: "Change Password", Path: "/user/password/"}, custom.BaseContextGaps{}),
|
||||
|
||||
Validate: func(r *http.Request, values map[string]string) (struct{}, error) {
|
||||
old, passcode, new, new2 := values["old"], values["otp"], values["new"], values["new2"]
|
||||
|
|
|
|||
|
|
@ -18,14 +18,6 @@
|
|||
{{ end }}
|
||||
</ul>
|
||||
</p>
|
||||
<div class="pure-button-group" role="group" role="Actions">
|
||||
<a class="pure-button" href="/user/password/">Change Password</a>
|
||||
{{ if .User.IsTOTPEnabled }}
|
||||
<a class="pure-button" href="/user/totp/disable/">Disable Passcode (TOTP)</a>
|
||||
{{ else }}
|
||||
<a class="pure-button" href="/user/totp/enable/">Enable Passcode (TOTP)</a>
|
||||
{{ end }}
|
||||
</div>
|
||||
<hr />
|
||||
</div>
|
||||
|
||||
|
|
@ -34,14 +26,11 @@
|
|||
{{ if (not .User.IsTOTPEnabled) }}
|
||||
<div>
|
||||
<p class="error-message">
|
||||
TOTP is required to access these.
|
||||
You are an administrator, but do not have TOTP enabled.
|
||||
Please turn it on to access the admin page.
|
||||
</p>
|
||||
</div>
|
||||
{{ end }}
|
||||
<div class="pure-button-group" role="group" role="Actions">
|
||||
<a class="pure-button" href="/admin/">Distillery Admin Page</a>
|
||||
</div>
|
||||
<hr />
|
||||
</div>
|
||||
{{ end }}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +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/control/static"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/control/static/custom"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/httpx"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/httpx/field"
|
||||
|
||||
|
|
@ -33,7 +34,7 @@ func (panel *UserPanel) routeTOTPEnable(ctx context.Context) http.Handler {
|
|||
},
|
||||
|
||||
RenderTemplate: totpEnableTemplate,
|
||||
RenderTemplateContext: panel.UserFormContext(component.MenuItem{Title: "Enable TOTP", Path: "/user/totp/enable/"}),
|
||||
RenderTemplateContext: panel.UserFormContext(component.MenuItem{Title: "Enable TOTP", Path: "/user/totp/enable/"}, custom.BaseContextGaps{}),
|
||||
|
||||
Validate: func(r *http.Request, values map[string]string) (struct{}, error) {
|
||||
password := values["password"]
|
||||
|
|
@ -78,9 +79,11 @@ type totpEnrollContext struct {
|
|||
|
||||
func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
|
||||
totpEnrollTemplate := panel.Dependencies.Custom.Template(totpEnrollTemplate)
|
||||
crumbs := []component.MenuItem{
|
||||
{Title: "User", Path: "/user/"},
|
||||
{Title: "Enable TOTP", Path: "/user/totp/enable/"},
|
||||
gaps := custom.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "User", Path: "/user/"},
|
||||
{Title: "Enable TOTP", Path: "/user/totp/enable/"},
|
||||
},
|
||||
}
|
||||
return &httpx.Form[struct{}]{
|
||||
Fields: []field.Field{
|
||||
|
|
@ -103,7 +106,7 @@ func (panel *UserPanel) routeTOTPEnroll(ctx context.Context) http.Handler {
|
|||
FormContext: context,
|
||||
},
|
||||
}
|
||||
panel.Dependencies.Custom.Update(&ctx.userFormContext, r, crumbs)
|
||||
panel.Dependencies.Custom.Update(&ctx.userFormContext, r, gaps)
|
||||
|
||||
if err == nil && user != nil {
|
||||
ctx.userFormContext.User = &user.User
|
||||
|
|
@ -168,7 +171,7 @@ func (panel *UserPanel) routeTOTPDisable(ctx context.Context) http.Handler {
|
|||
return struct{}{}, err == nil && user != nil && !user.IsTOTPEnabled()
|
||||
},
|
||||
RenderTemplate: totpDisableTemplate,
|
||||
RenderTemplateContext: panel.UserFormContext(component.MenuItem{Title: "Disable TOTP", Path: "/user/totp/disable/"}),
|
||||
RenderTemplateContext: panel.UserFormContext(component.MenuItem{Title: "Disable TOTP", Path: "/user/totp/disable/"}, custom.BaseContextGaps{}),
|
||||
|
||||
Validate: func(r *http.Request, values map[string]string) (struct{}, error) {
|
||||
password, otp := values["password"], values["otp"]
|
||||
|
|
|
|||
|
|
@ -36,13 +36,17 @@ type GrantWithURL struct {
|
|||
|
||||
func (panel *UserPanel) routeUser(ctx context.Context) http.Handler {
|
||||
userTemplate := panel.Dependencies.Custom.Template(userTemplate)
|
||||
crumbs := []component.MenuItem{
|
||||
{Title: "User", Path: "/user/"},
|
||||
gaps := custom.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "User", Path: "/user/"},
|
||||
},
|
||||
Actions: []component.MenuItem{
|
||||
{Title: "Change Password", Path: "/user/password"},
|
||||
},
|
||||
}
|
||||
|
||||
return &httpx.HTMLHandler[routeUserContext]{
|
||||
Handler: func(r *http.Request) (ruc routeUserContext, err error) {
|
||||
panel.Dependencies.Custom.Update(&ruc, r, crumbs)
|
||||
|
||||
// find the user
|
||||
ruc.AuthUser, err = panel.Dependencies.Auth.UserOf(r)
|
||||
|
|
@ -50,6 +54,21 @@ func (panel *UserPanel) routeUser(ctx context.Context) http.Handler {
|
|||
return ruc, err
|
||||
}
|
||||
|
||||
// build the gaps
|
||||
gaps := gaps.Clone()
|
||||
if ruc.AuthUser.IsTOTPEnabled() {
|
||||
gaps.Actions = append(gaps.Actions, component.MenuItem{
|
||||
Title: "Disable Passcode (TOTP)",
|
||||
Path: "/user/totp/disable/",
|
||||
})
|
||||
} else {
|
||||
gaps.Actions = append(gaps.Actions, 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)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue