diff --git a/front/core/directives/acl-common.js b/front/core/directives/acl-common.js new file mode 100644 index 000000000..81edeb675 --- /dev/null +++ b/front/core/directives/acl-common.js @@ -0,0 +1,43 @@ +import FormInput from '../components/form-input'; + +export default function getAcls(acls, aclService, $element, $attrs, user) { + const attrsVnAcl = user ? $attrs.vnUserAcl : $attrs.vnAcl; + acls = attrsVnAcl.split(',').map(i => i.trim()); + if (acls[0] == '') return; + if (user) { + const splitAcl = acls[0].split('.'); + const splitSlash = splitAcl[1].split('/'); + + const model = splitAcl[0]; + const property = splitSlash[0]; + let accessType = splitSlash[1]; + + if (accessType === 'w') accessType = 'WRITE'; + else if (accessType === 'r') accessType = 'READ'; + if (aclService.hasAnyACL(model, property, accessType)) return; + } else if (aclService.hasAny(acls)) return; + + disableElement($attrs, $element); +} + +function disableElement($attrs, $element) { + const action = $attrs.vnAclAction || 'disable'; + 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(); +} + diff --git a/front/core/directives/acl.js b/front/core/directives/acl.js index 9b7328524..2267c71ad 100644 --- a/front/core/directives/acl.js +++ b/front/core/directives/acl.js @@ -1,6 +1,5 @@ import ngModule from '../module'; -import FormInput from '../components/form-input'; - +import getAcls from './acl-common'; function vnAcl(aclService) { let acls = []; @@ -8,30 +7,7 @@ function vnAcl(aclService) { restrict: 'A', priority: -1, link: function(_, $element, $attrs) { - acls = $attrs.vnAcl.split(',').map(i => i.trim()); - if (acls[0] == '') return; - - let action = $attrs.vnAclAction || 'disable'; - - if (aclService.hasAny(acls)) 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(); + getAcls(acls, aclService, $element, $attrs, false); } }; } diff --git a/front/core/directives/index.js b/front/core/directives/index.js index 701baf133..d58832096 100644 --- a/front/core/directives/index.js +++ b/front/core/directives/index.js @@ -5,6 +5,7 @@ import './popover'; import './click-stop'; import './rule'; import './acl'; +import './acl-common'; import './user-acl'; import './on-error-src'; import './zoom-image'; diff --git a/front/core/directives/user-acl.js b/front/core/directives/user-acl.js index e0c7af460..7dcfef6ee 100644 --- a/front/core/directives/user-acl.js +++ b/front/core/directives/user-acl.js @@ -1,5 +1,5 @@ import ngModule from '../module'; -import FormInput from '../components/form-input'; +import getAcls from './acl-common'; function vnUserAcl(aclService) { let acls = []; @@ -8,47 +8,7 @@ function vnUserAcl(aclService) { 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(); + getAcls(acls, aclService, $element, $attrs, true); } }; }