ff-admin/src/router/adminGuard.ts

38 lines
991 B
TypeScript
Raw Normal View History

2024-09-01 19:19:48 +02:00
import NProgress from "nprogress";
2024-09-17 16:44:02 +02:00
import { useAbilityStore } from "@/stores/ability";
import { useNavigationStore } from "@/stores/admin/navigation";
2024-09-01 19:19:48 +02:00
export async function abilityAndNavUpdate(to: any, from: any, next: any) {
NProgress.start();
const ability = useAbilityStore();
const navigation = useNavigationStore();
2025-02-15 11:16:52 +01:00
let admin = to.meta.admin || false;
2024-09-01 19:19:48 +02:00
let type = to.meta.type;
let section = to.meta.section;
let module = to.meta.module;
2025-02-15 11:16:52 +01:00
if ((admin && ability.isAdmin()) || ability.can(type, section, module)) {
2024-09-01 19:19:48 +02:00
NProgress.done();
2025-02-15 11:16:52 +01:00
navigation.activeNavigation = to.name.split("-")[1];
navigation.activeLink = to.name.split("-")[2];
2024-09-01 19:19:48 +02:00
next();
} else {
NProgress.done();
2024-12-23 14:00:32 +01:00
next({ name: "admin-default" });
2024-09-01 19:19:48 +02:00
}
}
2024-11-21 15:58:58 +01:00
export async function isOwner(to: any, from: any, next: any) {
NProgress.start();
const ability = useAbilityStore();
if (ability.isOwner) {
NProgress.done();
next();
} else {
NProgress.done();
next(false);
}
}