feat(projects): add auth example

This commit is contained in:
Soybean
2024-03-24 15:39:41 +08:00
parent 41e8bc44f8
commit c11d56da29
20 changed files with 269 additions and 22 deletions

View File

@ -10,6 +10,7 @@ import { useSvgIcon } from '@/hooks/common/icon';
* @param roles Roles
*/
export function filterAuthRoutesByRoles(routes: ElegantConstRoute[], roles: string[]) {
// in static mode of auth route, the super admin role is defined in front-end
const SUPER_ROLE = 'R_SUPER';
// if the user is super admin, then it is allowed to access all routes
@ -30,9 +31,7 @@ function filterAuthRouteByRoles(route: ElegantConstRoute, roles: string[]) {
const routeRoles = (route.meta && route.meta.roles) || [];
// if the route's "roles" is empty, then it is allowed to access
if (!routeRoles.length) {
return [route];
}
const isEmptyRoles = !routeRoles.length;
// if the user's role is included in the route's "roles", then it is allowed to access
const hasPermission = routeRoles.some(role => roles.includes(role));
@ -43,7 +42,7 @@ function filterAuthRouteByRoles(route: ElegantConstRoute, roles: string[]) {
filterRoute.children = filterRoute.children.flatMap(item => filterAuthRouteByRoles(item, roles));
}
return hasPermission ? [filterRoute] : [];
return hasPermission || isEmptyRoles ? [filterRoute] : [];
}
/**