ソースを参照

add a routing middleware

Olivier Massot 1 年間 前
コミット
ee3f824c4f
2 ファイル変更21 行追加0 行削除
  1. 20 0
      middleware/routing.global.ts
  2. 1 0
      pages/parameters.vue

+ 20 - 0
middleware/routing.global.ts

@@ -0,0 +1,20 @@
+import { useAbility } from '@casl/vue'
+
+export default defineNuxtRouteMiddleware((to, _) => {
+  const ability = useAbility()
+
+  const candidates = [to.name].concat(to.matched.map((route) => route.name))
+
+  const pages = 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)) {
+      console.error('No right to see this page')
+      navigateTo('/')
+    }
+  })
+})

+ 1 - 0
pages/parameters.vue

@@ -14,6 +14,7 @@
  */
 definePageMeta({
   layout: false,
+  name: 'parameters_page',
 })
 </script>