I am working on a Role Based permissions, I have following array,
let Roles = {
[
{ model: 'user', property: 'find', permission: 'allow' },
{ model: 'user', property: 'create', permission: 'allow' },
{ model: 'user', property: 'update', permission: 'deny' },
{ model: 'user', property: 'delete', permission: 'deny' }
],
[
{ model: 'registration', property: 'find', permission: 'allow' },
{ model: 'registration', property: 'create', permission: 'deny' },
{ model: 'registration', property: 'update', permission: 'deny' },
{ model: 'registration', property: 'delete', permission: 'deny' }
]
[
{ model: 'schedule', property: 'find', permission: 'allow' },
{ model: 'schedule', property: 'create', permission: 'allow' },
{ model: 'schedule', property: 'update', permission: 'allow' }
{ model: 'schedule', property: 'delete', permission: 'deny' }
]
}
and I am trying to get the following output
let userPermissions = [{
'menu_name': 'user',
'canFetchData': true,
'canCreateData': true,
'canUpdateData': false,
'canDeleteData': false,
}]
let registrationPermissions = [{
'menu_name': 'registration',
'canFetchData': true,
'canCreateData': false,
'canUpdateData': false,
'canDeleteData': false,
}]
let schedulePermissions = [{
'menu_name': 'schedule',
'canFetchData': true,
'canCreateData': true,
'canUpdateData': true,
'canDeleteData': false,
}]
the condition to generate the result would be , for a given model and property , if permission is allow , it should be true, else false.
I have tried by writing if condition and assign the value true , if condition matches . But for the second array the values are overriding
From the Roles array , Iam passing each array to the following function and returning the result
private canFetchData;
private canCreateData;
private canUpdateData;
private canDeleteData;
filterAndApplyPermission(data) {
for (let i = 0; i < data.length; i++) {
if (data[i].property === 'find' && data[i].permission === 'ALLOW') {
this.canFetchData = true;
} else if (data[i].property === 'create' && data[i].permission === 'ALLOW') {
this.canCreateData = true;
} else if (data[i].property === 'update' && data[i].permission === 'ALLOW') {
this.canUpdateData = true;
} else if (data[i].property === 'delete' && data[i].permission === 'ALLOW') {
this.canDeleteData = true;
}
}
const grouped_permission = {
'menu': data[0].model,
'canFetchData': this.canFetchData,
'canCreateData': this.canCreateData,
'canUpdateData': this.canUpdateData,
'canDeleteData': this.canDeleteData,
};
return grouped_permission;
}
thisinthis.canFetchData = true;refers to the same object...still, we need some more codethisin your code ?Rolesseems to be wrong