halo/ui/console-src/router/guards/permission.ts

23 lines
606 B
TypeScript
Raw Normal View History

import { useRoleStore } from "@/stores/role";
import { hasPermission } from "@/utils/permission";
import type { Router } from "vue-router";
export function setupPermissionGuard(router: Router) {
router.beforeEach((to, _, next) => {
const roleStore = useRoleStore();
const { uiPermissions } = roleStore.permissions;
const { meta } = to;
if (meta && meta.permissions) {
const flag = hasPermission(
Array.from(uiPermissions),
meta.permissions as string[],
true
);
if (!flag) {
next({ name: "Forbidden" });
}
}
next();
});
}