Skip to content

Commit

Permalink
Show menu section if user has access to at least one of its pages (#8978
Browse files Browse the repository at this point in the history
)
  • Loading branch information
winterhazel authored Apr 26, 2024
1 parent 380385d commit 00ee5fd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
5 changes: 2 additions & 3 deletions ui/src/config/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
31 changes: 23 additions & 8 deletions ui/src/store/modules/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 00ee5fd

Please sign in to comment.