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; const action = $attrs.vnAclAction || 'disable'; // The acls always come formatted as "Model.property/accessType" // Example: "Client.create/w" const splitAcl = acls[0].split('.'); const splitSlash = splitAcl[1].split('/'); const model = splitAcl[0]; const property = splitSlash[0]; let accessType = splitSlash[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)) { const 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);