Skip to content

Commit 00ee5fd

Browse files
authored
Show menu section if user has access to at least one of its pages (#8978)
1 parent 380385d commit 00ee5fd

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

ui/src/config/router.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,13 @@ function generateRouterMap (section) {
5151
icon: section.icon,
5252
docHelp: vueProps.$applyDocHelpMappings(section.docHelp),
5353
searchFilters: section.searchFilters,
54-
related: section.related
54+
related: section.related,
55+
section: true
5556
},
5657
component: shallowRef(RouteView)
5758
}
5859

5960
if (section.children && section.children.length > 0) {
60-
map.redirect = '/' + section.children[0].name
61-
map.meta.permission = section.children[0].permission
6261
map.children = []
6362
for (const child of section.children) {
6463
if ('show' in child && !child.show()) {

ui/src/store/modules/permission.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,39 @@
1717

1818
import { asyncRouterMap, constantRouterMap } from '@/config/router'
1919

20-
function hasApi (apis, route) {
21-
if (route.meta && route.meta.permission) {
22-
for (const permission of route.meta.permission) {
23-
if (!apis.includes(permission)) {
24-
return false
25-
}
26-
}
20+
function hasAccessToRoute (apis, route) {
21+
if (!route.meta || !route.meta.permission) {
2722
return true
2823
}
24+
for (const permission of route.meta.permission) {
25+
if (!apis.includes(permission)) {
26+
return false
27+
}
28+
}
29+
return true
30+
}
31+
32+
function hasAccessToSection (route) {
33+
const visibleChildren = route.children.filter(child => !child.hidden)
34+
if (visibleChildren.length === 0) {
35+
return false
36+
}
37+
const redirect = '/' + visibleChildren[0].meta.name
38+
if (redirect !== route.path) {
39+
route.redirect = redirect
40+
}
2941
return true
3042
}
3143

3244
function filterAsyncRouter (routerMap, apis) {
3345
const accessedRouters = routerMap.filter(route => {
34-
if (hasApi(apis, route)) {
46+
if (hasAccessToRoute(apis, route)) {
3547
if (route.children && route.children.length > 0) {
3648
route.children = filterAsyncRouter(route.children, apis)
3749
}
50+
if (route.meta && route.meta.section) {
51+
return hasAccessToSection(route)
52+
}
3853
return true
3954
}
4055
return false

0 commit comments

Comments
 (0)