fix(17): WR-06 apply the admin save rules in menu:import-winter
The importer copied URLs, targets and cross-menu parents as exported, so a javascript: URL or a _parent target broke the first admin save of the row, and an entry under a parent in another menu vanished from the public tree. Kept entries now get a trimmed or cleared URL, a cleared target and a top-level place in their own menu, each with a report line.
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.golem15.com/golem15/sm-menu-plugin/classes"
|
||||
"git.golem15.com/golem15/sm-menu-plugin/models"
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/bonfire"
|
||||
@@ -59,7 +60,12 @@ type WinterReport struct {
|
||||
// all-journal-categories, --map old=new on top); a page entry, an entry of a
|
||||
// type that is neither built-in nor registered, and a registry entry whose
|
||||
// reference is not among the type's current references are dropped with
|
||||
// their subtree, and every dropped or remapped entry is reported. Tables
|
||||
// their subtree, and every dropped or remapped entry is reported. The rules
|
||||
// an admin save enforces are applied to the copies with a report line each:
|
||||
// a URL that classes.AllowedURL refuses is trimmed when only surrounding
|
||||
// whitespace is wrong and cleared otherwise, a target other than _self or
|
||||
// _blank is cleared, and an entry whose parent is in another menu is copied
|
||||
// as a top-level entry of its own menu. Tables
|
||||
// that already hold rows are refused without --force, which replaces them in
|
||||
// the same transaction. Both id sequences are moved past the copied ids.
|
||||
func ImportWinter(app *backpack.App) bonfire.Command {
|
||||
@@ -233,7 +239,8 @@ func mergedMaps(maps map[string]string) map[string]string {
|
||||
// export, when its parent is missing or dropped (with the whole subtree),
|
||||
// for a page type, for a type mapped to nothing, for a type that is neither
|
||||
// built-in nor registered, and for a registry type that takes references
|
||||
// when its reference is not among them.
|
||||
// when its reference is not among them. A kept entry's URL, target and
|
||||
// parent are made to pass the admin save rules (see ImportWinter).
|
||||
func mapEntries(ctx context.Context, reg *signpost.Registry, entries []models.MenuEntry, menuIDs map[uint]bool, maps map[string]string, report *WinterReport) ([]models.MenuEntry, error) {
|
||||
byID := make(map[uint]*models.MenuEntry, len(entries))
|
||||
for i := range entries {
|
||||
@@ -319,11 +326,43 @@ func mapEntries(ctx context.Context, reg *signpost.Registry, entries []models.Me
|
||||
report.Dropped++
|
||||
continue
|
||||
}
|
||||
report.Lines = append(report.Lines, adminRules(e, byID, oldType[e.ID])...)
|
||||
kept = append(kept, *e)
|
||||
}
|
||||
return kept, nil
|
||||
}
|
||||
|
||||
// adminRules makes a kept entry pass the rules an admin save enforces and
|
||||
// returns a report line per change: a URL classes.AllowedURL refuses is
|
||||
// trimmed when that is enough and cleared otherwise, a target
|
||||
// classes.AllowedTarget refuses is cleared, and a parent in another menu is
|
||||
// dropped so the entry is a top-level entry of its own menu (the public tree
|
||||
// would leave it out, the reorder page would show it at the top).
|
||||
func adminRules(e *models.MenuEntry, byID map[uint]*models.MenuEntry, typ string) []string {
|
||||
var lines []string
|
||||
label := describe(e, typ)
|
||||
if url := models.Text(e.URL); !classes.AllowedURL(url) {
|
||||
if trimmed := strings.TrimSpace(url); trimmed != url && classes.AllowedURL(trimmed) {
|
||||
e.URL = &trimmed
|
||||
lines = append(lines, fmt.Sprintf("%s: url '%s' trimmed to '%s'", label, url, trimmed))
|
||||
} else {
|
||||
e.URL = nil
|
||||
lines = append(lines, fmt.Sprintf("%s: url %q cleared (only http, https, mailto, tel, / and # links are served)", label, url))
|
||||
}
|
||||
}
|
||||
if target := models.Text(e.Target); !classes.AllowedTarget(target) {
|
||||
e.Target = nil
|
||||
lines = append(lines, fmt.Sprintf("%s: target '%s' cleared (only _self and _blank are allowed)", label, target))
|
||||
}
|
||||
if e.ParentID != nil {
|
||||
if parent, ok := byID[*e.ParentID]; ok && parent.MenuID != e.MenuID {
|
||||
lines = append(lines, fmt.Sprintf("%s: copied as a top-level entry (its parent %d is in menu %d)", label, parent.ID, parent.MenuID))
|
||||
e.ParentID = nil
|
||||
}
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
// describe names an entry in a report line by id, title, menu, exported
|
||||
// type and reference.
|
||||
func describe(e *models.MenuEntry, typ string) string {
|
||||
|
||||
Reference in New Issue
Block a user