forked from verdnatura/salix-front
35 lines
899 B
JavaScript
35 lines
899 B
JavaScript
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, property, accessType) {
|
|
const modelAcls = state.getAcls().value[model];
|
|
if (!modelAcls) return false;
|
|
|
|
return ['*', property].some(prop => {
|
|
const acl = modelAcls[prop];
|
|
return acl && (acl['*'] || acl[accessType]);
|
|
});
|
|
}
|
|
|
|
return {
|
|
fetch,
|
|
hasAny,
|
|
state,
|
|
};
|
|
}
|