36 lines
905 B
JavaScript
36 lines
905 B
JavaScript
|
import { useState } from './useState';
|
||
|
import axios from 'axios';
|
||
|
|
||
|
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 acls = acls[model];
|
||
|
if (acls) {
|
||
|
for (const prop of ['*', property]) {
|
||
|
const acl = acls[prop];
|
||
|
if (acl && (acl['*'] || acl[accessType])) return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
fetch,
|
||
|
hasAny,
|
||
|
state,
|
||
|
};
|
||
|
}
|