diff --git a/src/composables/useAcl.js b/src/composables/useAcl.js new file mode 100644 index 000000000..46aaa3c25 --- /dev/null +++ b/src/composables/useAcl.js @@ -0,0 +1,33 @@ +import axios from 'axios'; +import { useState } from './useState'; + +export function useAcl() { + const state = useState(); + + async function fetch() { + const { data } = await axios.get('VnUsers/acls'); + const acls = {}; + data.forEach((acl) => { + acls[acl.model] = acls[acl.model] || {}; + acls[acl.model][acl.property] = acls[acl.model][acl.property] || {}; + acls[acl.model][acl.property][acl.accessType] = true; + }); + + state.setAcls(acls); + } + + function hasAny(model, prop, accessType) { + const acls = state.getAcls().value[model]; + if (acls) + return ['*', prop].some((key) => { + const acl = acls[key]; + return acl && (acl['*'] || acl[accessType]); + }); + } + + return { + fetch, + hasAny, + state, + }; +} diff --git a/src/composables/useSession.js b/src/composables/useSession.js index 56bce0279..ca2abef00 100644 --- a/src/composables/useSession.js +++ b/src/composables/useSession.js @@ -1,5 +1,6 @@ import { useState } from './useState'; import { useRole } from './useRole'; +import { useAcl } from './useAcl'; import { useUserConfig } from './useUserConfig'; import axios from 'axios'; import useNotify from './useNotify'; @@ -88,6 +89,7 @@ export function useSession() { setSession(data); await useRole().fetch(); + await useAcl().fetch(); await useUserConfig().fetch(); await useTokenConfig().fetch(); diff --git a/src/composables/useState.js b/src/composables/useState.js index f20209494..c2ac1740c 100644 --- a/src/composables/useState.js +++ b/src/composables/useState.js @@ -15,6 +15,7 @@ if (sessionStorage.getItem('user')) user.value = JSON.parse(sessionStorage.getItem('user')); const roles = ref([]); +const acls = ref([]); const tokenConfig = ref({}); const drawer = ref(true); const headerMounted = ref(false); @@ -42,6 +43,14 @@ export function useState() { function setRoles(data) { roles.value = data; } + + function getAcls() { + return computed(() => acls.value); + } + + function setAcls(data) { + acls.value = data; + } function getTokenConfig() { return computed(() => { return tokenConfig.value; @@ -69,6 +78,8 @@ export function useState() { setUser, getRoles, setRoles, + getAcls, + setAcls, getTokenConfig, setTokenConfig, set, diff --git a/src/pages/Worker/WorkerCreate.vue b/src/pages/Worker/WorkerCreate.vue index ebde15b72..5e81a3070 100644 --- a/src/pages/Worker/WorkerCreate.vue +++ b/src/pages/Worker/WorkerCreate.vue @@ -86,7 +86,7 @@ onBeforeMount(async () => { url-create="Workers/new" model="worker" :form-initial-data="formData" - @on-data-saved="({ id }) => $router.push({ path: `/worker/${id}` })" + @on-data-saved="(__, { id }) => $router.push({ path: `/worker/${id}` })" >