75 lines
2.4 KiB
TypeScript
75 lines
2.4 KiB
TypeScript
![]() |
import { useCommonStore } from "~/stores/commonStore";
|
||
|
import { useAuthStore } from "~/stores/authStore";
|
||
|
|
||
|
// plugins/my-directive.ts
|
||
|
export default defineNuxtPlugin((nuxtApp) => {
|
||
|
nuxtApp.vueApp.directive("can", {
|
||
|
// called before bound element's attributes
|
||
|
// or event listeners are applied
|
||
|
created(el, binding, vnode) {
|
||
|
// see below for details on arguments
|
||
|
|
||
|
if (vnode.elm && !isSuperAdmin()) {
|
||
|
if (binding.value != "" && !hasPermission(binding.value)) {
|
||
|
if (binding.modifiers.checkbox || binding.modifiers.radio) {
|
||
|
vnode.elm.classList.add("disabled");
|
||
|
vnode.elm.disabled = true;
|
||
|
} else {
|
||
|
vnode.elm.remove();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
// called right before the element is inserted into the DOM.
|
||
|
beforeMount(el, binding, vnode) {},
|
||
|
// called when the bound element's parent component
|
||
|
// and all its children are mounted.
|
||
|
mounted(el, binding, vnode) {},
|
||
|
// called before the parent component is updated
|
||
|
beforeUpdate(el, binding, vnode, prevVnode) {},
|
||
|
// called after the parent component and
|
||
|
// all of its children have updated
|
||
|
updated(el, binding, vnode, prevVnode) {
|
||
|
if (vnode.elm && !isSuperAdmin()) {
|
||
|
if (binding.value != "" && !hasPermission(binding.value)) {
|
||
|
if (binding.modifiers.checkbox || binding.modifiers.radio) {
|
||
|
vnode.elm.classList.add("disabled");
|
||
|
vnode.elm.disabled = true;
|
||
|
} else {
|
||
|
vnode.elm.remove();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
// called before the parent component is unmounted
|
||
|
beforeUnmount(el, binding, vnode) {},
|
||
|
// called when the parent component is unmounted
|
||
|
unmounted(el, binding, vnode) {},
|
||
|
|
||
|
getSSRProps(binding, vnode) {
|
||
|
// You can provide SSR-specific props here
|
||
|
return {};
|
||
|
},
|
||
|
});
|
||
|
});
|
||
|
|
||
|
function hasPermission(permission) {
|
||
|
const CommonStore = useCommonStore();
|
||
|
|
||
|
if (CommonStore.userPermisionGetter && CommonStore.userPermisionGetter.length)
|
||
|
return CommonStore.userPermisionGetter.includes(permission);
|
||
|
|
||
|
// if (CommonStore.getters.userPermisionGetter.includes(binding.value)) {
|
||
|
// vnode.elm.style.display = 'none!important';
|
||
|
// vnode.elm.classList.add('disabled');
|
||
|
// vnode.elm.disabled = true;
|
||
|
// vnode.elm.title = 'عدم دسترسی';
|
||
|
}
|
||
|
|
||
|
function isSuperAdmin() {
|
||
|
const CommonStore = useAuthStore();
|
||
|
|
||
|
// user with id = 2 is super admin
|
||
|
return CommonStore?.currentUser?.user_level > 1;
|
||
|
}
|