bug 103 falcar cliente, acl condicionales

This commit is contained in:
Daniel Herrero 2018-03-01 10:54:02 +01:00
parent 319cab6223
commit 465d210b51
4 changed files with 123 additions and 35 deletions

View File

@ -15,19 +15,25 @@
vn-focus
label="Social name"
field="$ctrl.client.socialName"
vn-acl="administrative">
vn-acl="administrative, salesPerson"
acl-conditional-to-salesPerson="{{!$ctrl.client.isTaxDataChecked}}"
>
</vn-textfield>
<vn-textfield
vn-one
label="Tax number"
field="$ctrl.client.fi"
vn-acl="administrative">
vn-acl="administrative, salesPerson"
acl-conditional-to-salesPerson="{{!$ctrl.client.isTaxDataChecked}}"
>
</vn-textfield>
<vn-check
vn-one
label="Is equalizated"
field="$ctrl.client.isEqualizated"
vn-acl="administrative">
vn-acl="administrative, salesPerson"
acl-conditional-to-salesPerson="{{!$ctrl.client.isTaxDataChecked}}"
>
</vn-check>
</vn-horizontal>
<vn-horizontal>
@ -35,13 +41,17 @@
vn-two
label="Street"
field="$ctrl.client.street"
vn-acl="administrative">
vn-acl="administrative, salesPerson"
acl-conditional-to-salesPerson="{{!$ctrl.client.isTaxDataChecked}}"
>
</vn-textfield>
<vn-textfield
vn-one
label="City"
field="$ctrl.client.city"
vn-acl="administrative">
vn-acl="administrative, salesPerson"
acl-conditional-to-salesPerson="{{!$ctrl.client.isTaxDataChecked}}"
>
</vn-textfield>
</vn-horizontal>
<vn-horizontal>
@ -49,7 +59,9 @@
vn-one
label="Postcode"
field="$ctrl.client.postcode"
vn-acl="administrative">
vn-acl="administrative, salesPerson"
acl-conditional-to-salesPerson="{{!$ctrl.client.isTaxDataChecked}}"
>
</vn-textfield>
<vn-autocomplete
vn-one
@ -59,7 +71,9 @@
show-field="name"
value-field="id"
label="Province"
vn-acl="administrative">
vn-acl="administrative, salesPerson"
acl-conditional-to-salesPerson="{{!$ctrl.client.isTaxDataChecked}}"
>
</vn-autocomplete>
<vn-autocomplete
vn-one
@ -69,7 +83,9 @@
show-field="country"
value-field="id"
label="Country"
vn-acl="administrative">
vn-acl="administrative, salesPerson"
acl-conditional-to-salesPerson="{{!$ctrl.client.isTaxDataChecked}}"
>
</vn-autocomplete>
</vn-horizontal>
<vn-horizontal margin-small-bottom>
@ -77,21 +93,26 @@
<vn-check
label="Active"
field="$ctrl.client.isActive"
vn-acl="administrative">
vn-acl="administrative, salesPerson"
acl-conditional-to-salesPerson="{{!$ctrl.client.isTaxDataChecked}}"
>
</vn-check>
</vn-one>
<vn-one>
<vn-check
label="Invoice by address"
field="$ctrl.client.hasToInvoiceByAddress"
vn-acl="administrative">
vn-acl="administrative, salesPerson"
acl-conditional-to-salesPerson="{{!$ctrl.client.isTaxDataChecked}}"
>
</vn-check>
</vn-one>
<vn-one>
<vn-check
label="Verified data"
field="$ctrl.client.isTaxDataChecked"
vn-acl="administrative">
vn-acl="administrative"
>
</vn-check>
</vn-one>
</vn-horizontal>
@ -100,28 +121,34 @@
<vn-check
label="Has to invoice"
field="$ctrl.client.hasToInvoice"
vn-acl="administrative">
vn-acl="administrative, salesPerson"
acl-conditional-to-salesPerson="{{!$ctrl.client.isTaxDataChecked}}"
>
</vn-check>
</vn-one>
<vn-one>
<vn-check
label="Invoice by mail"
field="$ctrl.client.isToBeMailed"
vn-acl="administrative">
vn-acl="administrative, salesPerson"
acl-conditional-to-salesPerson="{{!$ctrl.client.isTaxDataChecked}}"
>
</vn-check>
</vn-one>
<vn-one>
<vn-check
label="Vies"
field="$ctrl.client.isVies"
vn-acl="administrative">
vn-acl="administrative, salesPerson"
acl-conditional-to-salesPerson="{{!$ctrl.client.isTaxDataChecked}}"
>
</vn-check>
</vn-one>
</vn-horizontal>
</vn-vertical>
</vn-card>
<vn-button-bar>
<vn-submit label="Save" vn-acl="administrative"></vn-submit>
<vn-submit label="Save" vn-acl="administrative, salesPerson" acl-conditional-to-salesPerson="{{!$ctrl.client.isTaxDataChecked}}"></vn-submit>
</vn-button-bar>
</form>
<vn-confirm

View File

@ -20,6 +20,11 @@ export default class ClientFiscalData {
}
}
buyerHaspermissions() {
if (!this.client) return true;
return !this.client.isTaxDataChecked;
}
submit() {
return this.$.watcher.submit().then(
() => this.checkEtChanges());

View File

@ -1,6 +1,8 @@
import ngModule from '../module';
function vnAcl(aclService, $timeout) {
let acls = [];
function getMaterialType(className) {
let type = '';
if (className) {
@ -20,32 +22,78 @@ function vnAcl(aclService, $timeout) {
}
}
}
function getDynamicConditions($attrs) {
let atributes = $attrs.$attr;
let conditions = {};
Object.keys(atributes).forEach(atribute => {
if (atribute.startsWith('aclConditionalTo')) {
let role = atributes[atribute].split('-').slice(-1)[0];
conditions[atribute] = {
role: role
};
}
});
return conditions;
}
function permissionElement($element, action) {
if (!aclService.aclPermission(acls)) {
if (action === 'disabled') {
let input = $element[0];
let selector = 'input, textarea, button, submit';
if (!input.matches(selector))
input = input.querySelector(selector);
if (input) {
$timeout(() => {
input.setAttribute("disabled", "true");
updateMaterial(input);
});
$element[0].querySelectorAll('i, vn-drop-down').forEach(element => {
element.parentNode.removeChild(element);
});
}
} else {
$element.remove();
}
}
}
function updateAcls(role, toAdd) {
let position = acls.indexOf(role);
if (!toAdd && position > -1) {
acls.splice(position, 1);
} // todo: add acl and enabled element if previusly was disabled
}
return {
restrict: 'A',
priority: -1,
link: function($scope, $element, $attrs) {
let acls = $attrs.vnAcl.split(',');
acls = $attrs.vnAcl.split(',').map(element => element.trim().toLowerCase());
let action = $attrs.vnAclAction || 'disabled';
if (!aclService.aclPermission(acls)) {
if (action === 'disabled') {
let input = $element[0];
let selector = 'input, textarea, button, submit';
let conditions = getDynamicConditions($attrs);
permissionElement($element, action);
if (!input.matches(selector))
input = input.querySelector(selector);
if (Object.keys(conditions).length) {
let watchConditions = $scope.$watch(() => {
Object.keys(conditions).forEach(attrName => {
let hasPermission = $scope.$eval($attrs[attrName]);
if (!hasPermission) {
updateAcls(conditions[attrName].role, hasPermission);
permissionElement($element, action);
delete conditions[attrName];
}
});
if (input) {
$timeout(() => {
input.setAttribute("disabled", "true");
updateMaterial(input);
});
$element[0].querySelectorAll('i, vn-drop-down').forEach(element => {
element.parentNode.removeChild(element);
});
if (Object.keys(conditions).length === 0) {
// unWacth
watchConditions();
}
} else {
$element.remove();
}
});
}
}
};

View File

@ -5,7 +5,14 @@ ngModule.constant('aclConstant', acl);
aclService.$inject = ['aclConstant'];
function aclService(aclConstant) {
this.roles = aclConstant.roles || undefined;
if (aclConstant.roles) {
this.roles = {};
Object.keys(aclConstant.roles).forEach(role => {
this.roles[role.toLowerCase()] = aclConstant.roles[role];
});
} else {
this.roles = undefined;
}
this.routeHasPermission = function(route) {
let hasPermission;
@ -23,7 +30,8 @@ function aclService(aclConstant) {
let hasPermission = false;
let total = aclCollection.length;
for (let i = 0; i < total; i++) {
if (this.roles[aclCollection[i]]) {
let role = aclCollection[i].trim().toLowerCase();
if (this.roles[role]) {
hasPermission = true;
break;
}