feat(quick-261016-s8x): pact.NavigationItem.Visible hides navigation entries at request time
- pact.NavigationItem gains Visible func(ctx) bool (json:"-"); nil keeps the entry always listed - cabana.Registry.Metadata runs it after the permission check, drops a main item (with its side menu) or a side item that answers false, and relinks a parent whose controller is a hidden child's to its first visible openable child - TestNavigationHidesInvisibleItemsAndRepointsTarget covers the rules - pact and cabana READMEs, users-and-permissions and admin-controllers docs describe the hook
This commit is contained in:
@@ -35,7 +35,11 @@ type SettingsEntry struct {
|
||||
}
|
||||
|
||||
// Metadata returns only entries the backend principal may open. Denied
|
||||
// entries are removed before any response value is constructed.
|
||||
// entries are removed before any response value is constructed. A navigation
|
||||
// entry with a pact.NavigationItem.Visible func is also left out when that
|
||||
// func answers false for ctx (a main item with its whole side menu, or one
|
||||
// side item); Visible runs only after the permission check has passed, so a
|
||||
// denied principal never runs plugin code.
|
||||
func (r *Registry) Metadata(ctx context.Context, principal *bouncer.Principal, tr *phrasebook.Translator) ([]NavigationEntry, []SettingsEntry) {
|
||||
navigation := make([]NavigationEntry, 0)
|
||||
settings := make([]SettingsEntry, 0)
|
||||
@@ -49,15 +53,29 @@ func (r *Registry) Metadata(ctx context.Context, principal *bouncer.Principal, t
|
||||
if !Allows(principal, item.Permissions) {
|
||||
continue
|
||||
}
|
||||
if item.Visible != nil && !item.Visible(ctx) {
|
||||
continue
|
||||
}
|
||||
children := make([]NavigationEntry, 0)
|
||||
// hidden collects the controllers of side items a Visible func hid
|
||||
// for this request, so the parent never links to one of them.
|
||||
// Permission-denied children are not recorded: the parent keeps its
|
||||
// own controller whenever the principal can open it.
|
||||
hidden := map[string]bool{}
|
||||
for _, child := range item.SideMenu {
|
||||
if !Allows(principal, child.Permissions) {
|
||||
continue
|
||||
}
|
||||
if child.Visible != nil && !child.Visible(ctx) {
|
||||
if child.Controller != "" {
|
||||
hidden[child.Controller] = true
|
||||
}
|
||||
continue
|
||||
}
|
||||
children = append(children, navigationView(ctx, tr, child, nil))
|
||||
}
|
||||
view := navigationView(ctx, tr, item, children)
|
||||
view.Controller = r.openableTarget(principal, item, children)
|
||||
view.Controller = r.openableTarget(principal, item, children, hidden)
|
||||
navigation = append(navigation, view)
|
||||
}
|
||||
sort.SliceStable(navigation, func(i, j int) bool {
|
||||
@@ -95,11 +113,13 @@ func (r *Registry) Metadata(ctx context.Context, principal *bouncer.Principal, t
|
||||
}
|
||||
|
||||
// openableTarget returns the controller a main item should link to. It is the
|
||||
// item's own controller unless the principal cannot open it, in which case it
|
||||
// is the first side-menu entry the principal can, so the menu never links to a
|
||||
// page that answers 403. It is empty when nothing is openable.
|
||||
func (r *Registry) openableTarget(principal *bouncer.Principal, item pact.NavigationItem, children []NavigationEntry) string {
|
||||
if item.Controller == "" || r.canOpen(principal, item.Controller) {
|
||||
// item's own controller unless the principal cannot open it or it is the
|
||||
// controller of a side item hidden for this request (in hidden), in which
|
||||
// case it is the first of the listed children (already visible and permitted)
|
||||
// the principal can open, so the menu never links to a page that answers 403
|
||||
// or to a screen whose feature is off. It is empty when nothing is openable.
|
||||
func (r *Registry) openableTarget(principal *bouncer.Principal, item pact.NavigationItem, children []NavigationEntry, hidden map[string]bool) string {
|
||||
if item.Controller == "" || (!hidden[item.Controller] && r.canOpen(principal, item.Controller)) {
|
||||
return item.Controller
|
||||
}
|
||||
for _, child := range children {
|
||||
|
||||
Reference in New Issue
Block a user