39 lines
1.0 KiB
JavaScript
39 lines
1.0 KiB
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(acls) {
|
|
for (const acl of acls) {
|
|
let { model, props, accessType } = acl;
|
|
const modelAcls = state.getAcls().value[model];
|
|
Array.isArray(props) || (props = [props]);
|
|
if (modelAcls)
|
|
return ['*', ...props].some((key) => {
|
|
const acl = modelAcls[key];
|
|
return acl && (acl['*'] || acl[accessType]);
|
|
});
|
|
}
|
|
return false;
|
|
}
|
|
|
|
return {
|
|
fetch,
|
|
hasAny,
|
|
state,
|
|
};
|
|
}
|