Last active
September 4, 2021 04:59
-
-
Save eliran-arkabi/0001f6cd75c32ee7bdfb452408d2fd08 to your computer and use it in GitHub Desktop.
nuxtjs plugin check permissions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Vue from 'vue' | |
| export default ({store, $axios}) => { | |
| //add directives | |
| //v-can - check permissions | |
| Vue.directive('can', { | |
| inserted (el, binding, vnode) { | |
| let can = false | |
| if (typeof binding.value === 'undefined') { | |
| Vue.util.warn('vCan directive require array or string of permissions as parameter') | |
| }else{ | |
| if(typeof binding.value === 'string'){ | |
| binding.value = binding.value.split() | |
| } | |
| binding.value.forEach((permission) => { | |
| if(store.getters['auth/permissions'].find((p) => p === permission)){ | |
| can = true | |
| } | |
| }) | |
| } | |
| if (!can) { | |
| vnode.elm.parentElement.removeChild(vnode.elm) | |
| } | |
| } | |
| }) | |
| //v-role - check permissions | |
| Vue.directive('role',{ | |
| inserted (el, binding, vnode){ | |
| let has = false | |
| if (typeof binding.value === 'undefined') { | |
| Vue.util.warn('vRole directive require array or string of permissions as parameter') | |
| }else{ | |
| if(typeof binding.value === 'string'){ | |
| binding.value = binding.value.split() | |
| } | |
| binding.value.forEach((role) => { | |
| if(store.getters['auth/roles'].find((r) => r === role)){ | |
| has = true | |
| } | |
| }) | |
| } | |
| if (!has) { | |
| vnode.elm.parentElement.removeChild(vnode.elm) | |
| } | |
| } | |
| }) | |
| //add prototypes to Vue | |
| Vue.prototype.$permissions = { | |
| can(...permissions){ | |
| permissions = [].concat.apply([], permissions) | |
| let can = false | |
| permissions.forEach((permission) => { | |
| if(store.getters['auth/permissions'].find((p) => p === permission)){ | |
| can = true | |
| } | |
| }) | |
| return can | |
| }, | |
| role(...roles){ | |
| roles = [].concat.apply([], roles); | |
| let has = false | |
| roles.forEach((role) => { | |
| if(store.getters['auth/roles'].find((r) => r === role)){ | |
| has = true | |
| } | |
| }) | |
| return has | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment