55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
import ngModule from '../module';
|
|
import FormInput from '../components/form-input';
|
|
|
|
function vnUserAcl(aclService) {
|
|
let acls = [];
|
|
|
|
return {
|
|
restrict: 'A',
|
|
priority: -1,
|
|
link: function(_, $element, $attrs) {
|
|
acls = $attrs.vnUserAcl.split(',').map(i => i.trim());
|
|
if (acls[0] == '') return;
|
|
|
|
let action = $attrs.vnAclAction || 'disable';
|
|
|
|
// The acls always come formatted as "Model.property/accessType"
|
|
// Example: "Client.create/w"
|
|
|
|
let model = acls[0].split('.')[0];
|
|
let property = acls[0].split('.')[1].split('/')[0];
|
|
let accessType = acls[0].split('.')[1].split('/')[1];
|
|
|
|
// There can be 3 cases for the acessType: Write(w), Read(r) or All(*)
|
|
|
|
if (accessType === 'w') accessType = 'WRITE';
|
|
else if (accessType === 'r') accessType = 'READ';
|
|
|
|
const hasAny = aclService.hasAnyACL(model, property, accessType);
|
|
|
|
if (hasAny) return;
|
|
|
|
if (action === 'disable') {
|
|
let element = $element[0];
|
|
let elementToDisable = element.$ctrl;
|
|
|
|
if (!(elementToDisable instanceof FormInput)) {
|
|
let selector = 'input, textarea, button, submit';
|
|
|
|
if (!element.matches(selector))
|
|
element = element.querySelector(selector);
|
|
|
|
elementToDisable = element;
|
|
}
|
|
|
|
if (elementToDisable)
|
|
elementToDisable.disabled = true;
|
|
} else
|
|
$element.remove();
|
|
}
|
|
};
|
|
}
|
|
vnUserAcl.$inject = ['aclService'];
|
|
|
|
ngModule.directive('vnUserAcl', vnUserAcl);
|