From 5bdc74e81818e51c528c2da33f63cac3597ce595 Mon Sep 17 00:00:00 2001 From: Fabricio Duarte Date: Thu, 18 Apr 2024 15:27:53 -0300 Subject: [PATCH] Show menu section if user has access to at least one of its pages --- ui/src/config/router.js | 5 ++--- ui/src/store/modules/permission.js | 31 ++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/ui/src/config/router.js b/ui/src/config/router.js index c3b69bde23a6..b4570dbcee83 100644 --- a/ui/src/config/router.js +++ b/ui/src/config/router.js @@ -51,14 +51,13 @@ function generateRouterMap (section) { icon: section.icon, docHelp: vueProps.$applyDocHelpMappings(section.docHelp), searchFilters: section.searchFilters, - related: section.related + related: section.related, + section: true }, component: shallowRef(RouteView) } if (section.children && section.children.length > 0) { - map.redirect = '/' + section.children[0].name - map.meta.permission = section.children[0].permission map.children = [] for (const child of section.children) { if ('show' in child && !child.show()) { diff --git a/ui/src/store/modules/permission.js b/ui/src/store/modules/permission.js index 3b080be5f636..ad11fe5a8d0f 100644 --- a/ui/src/store/modules/permission.js +++ b/ui/src/store/modules/permission.js @@ -17,24 +17,39 @@ import { asyncRouterMap, constantRouterMap } from '@/config/router' -function hasApi (apis, route) { - if (route.meta && route.meta.permission) { - for (const permission of route.meta.permission) { - if (!apis.includes(permission)) { - return false - } - } +function hasAccessToRoute (apis, route) { + if (!route.meta || !route.meta.permission) { return true } + for (const permission of route.meta.permission) { + if (!apis.includes(permission)) { + return false + } + } + return true +} + +function hasAccessToSection (route) { + const visibleChildren = route.children.filter(child => !child.hidden) + if (visibleChildren.length === 0) { + return false + } + const redirect = '/' + visibleChildren[0].meta.name + if (redirect !== route.path) { + route.redirect = redirect + } return true } function filterAsyncRouter (routerMap, apis) { const accessedRouters = routerMap.filter(route => { - if (hasApi(apis, route)) { + if (hasAccessToRoute(apis, route)) { if (route.children && route.children.length > 0) { route.children = filterAsyncRouter(route.children, apis) } + if (route.meta && route.meta.section) { + return hasAccessToSection(route) + } return true } return false