2022-07-15 08:26:27 +00:00
|
|
|
import { useRoleStore } from "@/stores/role";
|
|
|
|
import { hasPermission } from "@/utils/permission";
|
|
|
|
import type { Router } from "vue-router";
|
|
|
|
|
|
|
|
export function setupPermissionGuard(router: Router) {
|
2024-09-28 10:19:41 +00:00
|
|
|
router.beforeEach((to, _, next) => {
|
2022-07-15 08:26:27 +00:00
|
|
|
const roleStore = useRoleStore();
|
|
|
|
const { uiPermissions } = roleStore.permissions;
|
|
|
|
const { meta } = to;
|
|
|
|
if (meta && meta.permissions) {
|
|
|
|
const flag = hasPermission(
|
|
|
|
Array.from(uiPermissions),
|
|
|
|
meta.permissions as string[],
|
2022-07-19 06:07:28 +00:00
|
|
|
true
|
2022-07-15 08:26:27 +00:00
|
|
|
);
|
|
|
|
if (!flag) {
|
|
|
|
next({ name: "Forbidden" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|