From a31fe7735ee0d8d791c58e10dcb08e3307876582 Mon Sep 17 00:00:00 2001 From: Geriano Date: Sun, 17 Jul 2022 14:18:21 +0700 Subject: [PATCH] adding can method --- resources/js/app.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/resources/js/app.js b/resources/js/app.js index 77656a7..3a6b026 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -12,6 +12,26 @@ import { Inertia } from '@inertiajs/inertia'; const appName = window.document.getElementsByTagName('title')[0]?.innerText || 'Laravel'; +const can = (abilities) => { + const { $permissions } = usePage().props.value + + if (Array.isArray(abilities)) { + for (const ability of abilities) { + if (can(ability)) { + return true + } + } + } else if (typeof abilities === 'string') { + return $permissions.find(permission => permission.name === abilities) !== undefined + } else if (typeof abilities === 'number') { + return $permissions.find(permission => permission.id === abilities) !== undefined + } else { + return false + } +} + +window.can = can + createInertiaApp({ title: (title) => `${title} - ${appName}`, resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')), @@ -21,6 +41,7 @@ createInertiaApp({ .use(ZiggyVue, Ziggy) .mixin({ methods: { + can, themes: () => Themes, }, })