Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show menu section if user has access to at least one of its pages #8978

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
BryanMLima marked this conversation as resolved.
Show resolved Hide resolved
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) {
BryanMLima marked this conversation as resolved.
Show resolved Hide resolved
return hasAccessToSection(route)
}
return true
}
return false
Expand Down
Loading