|
|
@@ -1,20 +1,25 @@
|
|
|
import { useAbility } from '@casl/vue'
|
|
|
+import { useRedirect } from '~/composables/utils/useRedirect'
|
|
|
|
|
|
export default defineNuxtRouteMiddleware((to, _) => {
|
|
|
const ability = useAbility()
|
|
|
-
|
|
|
const candidates = to.matched.map((route) => route.name)
|
|
|
|
|
|
- const pages = ability.rules
|
|
|
+ const restrictedPages = ability.rules
|
|
|
.filter((rule) => rule.action.toString() === 'display')
|
|
|
.map((rule) => rule.subject.toString())
|
|
|
|
|
|
candidates.forEach((routeName) => {
|
|
|
const name: string = routeName?.toString() ?? ''
|
|
|
|
|
|
- if (name && pages.includes(name) && !ability.can('display', name)) {
|
|
|
+ if (
|
|
|
+ name &&
|
|
|
+ restrictedPages.includes(name) &&
|
|
|
+ !ability.can('display', name)
|
|
|
+ ) {
|
|
|
console.error('No right to see this page')
|
|
|
- navigateTo('/')
|
|
|
+ const { redirectToHome } = useRedirect()
|
|
|
+ redirectToHome()
|
|
|
}
|
|
|
})
|
|
|
})
|