0
0
Fork 0

refactor: refs #4074 useAcl in navigationStore & router

This commit is contained in:
Jorge Penadés 2024-09-09 11:35:49 +02:00
parent bef9b05b45
commit c978cca8c5
5 changed files with 25 additions and 27 deletions

View File

@ -31,12 +31,8 @@ const acl = useAcl();
const showForm = ref(false); const showForm = ref(false);
const isAllowedToCreate = computed(() => { const isAllowedToCreate = computed(() => {
const hasMissingAcl = $props.acls.some( if ($props.acls.length) return acl.hasAny($props.acls);
(x) => !acl.hasAny(x.model, x.props, x.accessType) return role.hasAny($props.rolesAllowedToCreate);
);
const hasRequiredRole = role.hasAny($props.rolesAllowedToCreate);
if ($props.acls.length) return !hasMissingAcl;
return hasRequiredRole;
}); });
</script> </script>

View File

@ -16,15 +16,20 @@ export function useAcl() {
state.setAcls(acls); state.setAcls(acls);
} }
function hasAny(model, props, accessType) { function hasAny(acls) {
const acls = state.getAcls().value[model]; if (!Array.isArray(acls)) acls = [acls];
for (const acl of acls) {
let { model, props, accessType } = acl;
const modelAcls = state.getAcls().value[model];
Array.isArray(props) || (props = [props]); Array.isArray(props) || (props = [props]);
if (acls) if (modelAcls)
return ['*', ...props].some((key) => { return ['*', ...props].some((key) => {
const acl = acls[key]; const acl = modelAcls[key];
return acl && (acl['*'] || acl[accessType]); return acl && (acl['*'] || acl[accessType]);
}); });
} }
return false;
}
return { return {
fetch, fetch,

View File

@ -60,15 +60,12 @@ export default route(function (/* { store, ssrContext } */) {
await useTokenConfig().fetch(); await useTokenConfig().fetch();
} }
const matches = to.matched; const matches = to.matched;
const hasRequiredRoles = matches.every((route) => { const hasRequiredAcls = matches.every((route) => {
const meta = route.meta; const meta = route.meta;
if (meta && meta.roles) return useRole().hasAny(meta.roles); if (!meta?.acls) return true;
return true; return useAcl().hasAny(meta.acls);
}); });
if (!hasRequiredAcls) return next({ path: '/' });
if (!hasRequiredRoles) {
return next({ path: '/' });
}
} }
next(); next();

View File

@ -79,7 +79,7 @@ export default {
meta: { meta: {
title: 'accounts', title: 'accounts',
icon: 'accessibility', icon: 'accessibility',
roles: ['itManagement'], acls: [{ model: 'Account', props: '*', accessType: '*' }],
}, },
component: () => import('src/pages/Account/AccountAccounts.vue'), component: () => import('src/pages/Account/AccountAccounts.vue'),
}, },
@ -89,7 +89,7 @@ export default {
meta: { meta: {
title: 'ldap', title: 'ldap',
icon: 'account_tree', icon: 'account_tree',
roles: ['itManagement'], acls: [{ model: 'LdapConfig', props: '*', accessType: '*' }],
}, },
component: () => import('src/pages/Account/AccountLdap.vue'), component: () => import('src/pages/Account/AccountLdap.vue'),
}, },
@ -99,7 +99,7 @@ export default {
meta: { meta: {
title: 'samba', title: 'samba',
icon: 'preview', icon: 'preview',
roles: ['itManagement'], acls: [{ model: 'SambaConfig', props: '*', accessType: '*' }],
}, },
component: () => import('src/pages/Account/AccountSamba.vue'), component: () => import('src/pages/Account/AccountSamba.vue'),
}, },

View File

@ -2,7 +2,7 @@ import axios from 'axios';
import { ref } from 'vue'; import { ref } from 'vue';
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { toLowerCamel } from 'src/filters'; import { toLowerCamel } from 'src/filters';
import { useRole } from 'src/composables/useRole'; import { useAcl } from 'src/composables/useAcl';
import routes from 'src/router/modules'; import routes from 'src/router/modules';
export const useNavigationStore = defineStore('navigationStore', () => { export const useNavigationStore = defineStore('navigationStore', () => {
@ -26,7 +26,7 @@ export const useNavigationStore = defineStore('navigationStore', () => {
'zone', 'zone',
]; ];
const pinnedModules = ref([]); const pinnedModules = ref([]);
const role = useRole(); const acl = useAcl();
function getModules() { function getModules() {
const modulesRoutes = ref([]); const modulesRoutes = ref([]);
@ -63,7 +63,7 @@ export const useNavigationStore = defineStore('navigationStore', () => {
title: `globals.pageTitles.${title}`, title: `globals.pageTitles.${title}`,
})); }));
if (meta && meta.roles && role.hasAny(meta.roles) === false) return; if (meta && meta.acls && acl.hasAny(meta.acls) === false) return;
const item = { const item = {
name: route.name, name: route.name,