重构用户权限
Some checks failed
Deploy / deploy (push) Has been cancelled

This commit is contained in:
2025-09-25 18:41:32 +08:00
parent 4429e2f018
commit efbca0e802
15 changed files with 619 additions and 187 deletions

View File

@@ -31,17 +31,68 @@ const router = createRouter({
routes: [ojs, admins],
})
router.beforeEach((to, from, next) => {
router.beforeEach(async (to, from, next) => {
// 检查是否需要认证
if (to.matched.some((record) => record.meta.requiresAuth)) {
if (!storage.get(STORAGE_KEY.AUTHED)) {
toggleLogin(true)
next("/")
} else {
next()
return
}
} else {
next()
}
// 检查管理员权限
if (to.matched.some((record) =>
record.meta.requiresAdmin ||
record.meta.requiresSuperAdmin ||
record.meta.requiresProblemPermission
)) {
if (!storage.get(STORAGE_KEY.AUTHED)) {
toggleLogin(true)
next("/")
return
}
// 动态导入用户store来检查权限
const { useUserStore } = await import("./shared/store/user")
const userStore = useUserStore()
// 确保用户信息已加载
if (!userStore.user) {
try {
await userStore.getMyProfile()
} catch (error) {
next("/")
return
}
}
// 检查super admin权限
if (to.matched.some((record) => record.meta.requiresSuperAdmin)) {
if (!userStore.isSuperAdmin) {
next("/admin")
return
}
}
// 检查题目权限
else if (to.matched.some((record) => record.meta.requiresProblemPermission)) {
if (!userStore.hasProblemPermission) {
next("/admin")
return
}
}
// 检查基本admin权限
else if (to.matched.some((record) => record.meta.requiresAdmin)) {
if (!userStore.isAdminRole) {
next("/")
return
}
}
}
next()
})
ChartJS.register(