custom: Improve templating of assets
This commit is contained in:
parent
7d0fb60d67
commit
b6bf0a8900
19 changed files with 516 additions and 432 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"html/template"
|
||||
|
|
@ -20,11 +21,8 @@ import (
|
|||
)
|
||||
|
||||
//go:embed "html/grants.html"
|
||||
var grantsStr string
|
||||
var grantsTemplate = static.AssetsAdmin.MustParseShared(
|
||||
"grants.html",
|
||||
grantsStr,
|
||||
)
|
||||
var grantsHTML []byte
|
||||
var grantsTemplate = custom.Parse[grantsContext]("grants.html", grantsHTML, static.AssetsAdmin)
|
||||
|
||||
type grantsContext struct {
|
||||
custom.BaseContext
|
||||
|
|
@ -39,15 +37,88 @@ type grantsContext struct {
|
|||
Drupals []string // unusued drupal usernames
|
||||
}
|
||||
|
||||
func (gc *grantsContext) use(r *http.Request, slug string, admin *Admin) (err error) {
|
||||
admin.Dependencies.Custom.Update(gc, r, custom.BaseContextGaps{
|
||||
func (admin *Admin) grants(ctx context.Context) http.Handler {
|
||||
tpl := grantsTemplate.Prepare(admin.Dependencies.Custom, custom.BaseContextGaps{
|
||||
Crumbs: []component.MenuItem{
|
||||
{Title: "Admin", Path: "/admin/"},
|
||||
{Title: "Instance", Path: template.URL("/admin/instance/" + slug)},
|
||||
{Title: "Grants", Path: template.URL("/admin/instance/" + slug + "/grants/")},
|
||||
{Title: "Instance", Path: "*to be updated*"},
|
||||
{Title: "Grants", Path: "*to be updated*"},
|
||||
},
|
||||
})
|
||||
|
||||
return tpl.HTMLHandlerWithGaps(func(r *http.Request, gaps *custom.BaseContextGaps) (grantsContext, error) {
|
||||
if r.Method == http.MethodGet {
|
||||
return admin.getGrants(r, gaps)
|
||||
} else {
|
||||
return admin.postGrants(r, gaps)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (admin *Admin) getGrants(r *http.Request, gaps *custom.BaseContextGaps) (gc grantsContext, err error) {
|
||||
slug := httprouter.ParamsFromContext(r.Context()).ByName("slug")
|
||||
if err := gc.use(r, gaps, slug, admin); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
|
||||
if err := gc.useGrants(r, admin); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
|
||||
return gc, nil
|
||||
}
|
||||
|
||||
func (admin *Admin) postGrants(r *http.Request, gaps *custom.BaseContextGaps) (gc grantsContext, err error) {
|
||||
// parse the form
|
||||
if err := r.ParseForm(); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
|
||||
// read out the form values
|
||||
var (
|
||||
slug = r.PostFormValue("slug")
|
||||
delete = r.PostFormValue("action") == "delete"
|
||||
distilleryUser = r.PostFormValue("distillery-user")
|
||||
drupalUser = r.PostFormValue("drupal-user")
|
||||
adminRole = r.PostFormValue("admin") == field.CheckboxChecked
|
||||
)
|
||||
|
||||
// set the common fields
|
||||
if err := gc.use(r, gaps, slug, admin); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
|
||||
if delete {
|
||||
// delete the user grant
|
||||
err := admin.Dependencies.Policy.Remove(r.Context(), distilleryUser, slug)
|
||||
if err != nil {
|
||||
return gc, err
|
||||
}
|
||||
} else {
|
||||
// update the grant
|
||||
err := admin.Dependencies.Policy.Set(r.Context(), models.Grant{
|
||||
User: distilleryUser,
|
||||
Slug: slug,
|
||||
|
||||
DrupalUsername: drupalUser,
|
||||
DrupalAdminRole: adminRole,
|
||||
})
|
||||
if err != nil {
|
||||
gc.Error = fmt.Sprintf("Unable to update grant for user %s: %s", distilleryUser, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// fetch the grants for the instance
|
||||
if err := gc.useGrants(r, admin); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
return gc, nil
|
||||
}
|
||||
|
||||
func (gc *grantsContext) use(r *http.Request, gaps *custom.BaseContextGaps, slug string, admin *Admin) (err error) {
|
||||
gaps.Crumbs[1] = component.MenuItem{Title: "Instance", Path: template.URL("/admin/instance/" + slug)}
|
||||
gaps.Crumbs[2] = component.MenuItem{Title: "Grants", Path: template.URL("/admin/instance/" + slug + "/grants/")}
|
||||
|
||||
// find the instance itself
|
||||
gc.instance, err = admin.Dependencies.Instances.WissKI(r.Context(), slug)
|
||||
if err == instances.ErrWissKINotFound {
|
||||
|
|
@ -100,63 +171,3 @@ func (gc *grantsContext) useGrants(r *http.Request, admin *Admin) (err error) {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (admin *Admin) getGrants(r *http.Request) (gc grantsContext, err error) {
|
||||
slug := httprouter.ParamsFromContext(r.Context()).ByName("slug")
|
||||
if err := gc.use(r, slug, admin); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
|
||||
if err := gc.useGrants(r, admin); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
|
||||
return gc, nil
|
||||
}
|
||||
|
||||
func (admin *Admin) postGrants(r *http.Request) (gc grantsContext, err error) {
|
||||
// parse the form
|
||||
if err := r.ParseForm(); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
|
||||
// read out the form values
|
||||
var (
|
||||
slug = r.PostFormValue("slug")
|
||||
delete = r.PostFormValue("action") == "delete"
|
||||
distilleryUser = r.PostFormValue("distillery-user")
|
||||
drupalUser = r.PostFormValue("drupal-user")
|
||||
adminRole = r.PostFormValue("admin") == field.CheckboxChecked
|
||||
)
|
||||
|
||||
// set the common fields
|
||||
if err := gc.use(r, slug, admin); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
|
||||
if delete {
|
||||
// delete the user grant
|
||||
err := admin.Dependencies.Policy.Remove(r.Context(), distilleryUser, slug)
|
||||
if err != nil {
|
||||
return gc, err
|
||||
}
|
||||
} else {
|
||||
// update the grant
|
||||
err := admin.Dependencies.Policy.Set(r.Context(), models.Grant{
|
||||
User: distilleryUser,
|
||||
Slug: slug,
|
||||
|
||||
DrupalUsername: drupalUser,
|
||||
DrupalAdminRole: adminRole,
|
||||
})
|
||||
if err != nil {
|
||||
gc.Error = fmt.Sprintf("Unable to update grant for user %s: %s", distilleryUser, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// fetch the grants for the instance
|
||||
if err := gc.useGrants(r, admin); err != nil {
|
||||
return gc, err
|
||||
}
|
||||
return gc, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue