salix/modules/ticket/back/methods/sale/canEdit.js

91 lines
3.1 KiB
JavaScript
Raw Normal View History

2022-10-04 11:22:11 +00:00
const UserError = require('vn-loopback/util/user-error');
2022-10-19 06:22:36 +00:00
const loopBackCtx = require('vn-loopback/server/server');
2022-10-04 11:22:11 +00:00
module.exports = Self => {
Self.remoteMethodCtx('canEdit', {
description: 'Check if all the received sales are aditable',
accessType: 'READ',
accepts: [{
arg: 'sales',
type: ['object'],
required: true
}],
returns: {
type: 'boolean',
root: true
},
http: {
2022-10-04 13:02:00 +00:00
path: `/canEdit`,
verb: 'get'
}
});
Self.canEdit = async(ctx, sales, options) => {
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
2022-10-25 13:02:22 +00:00
console.log(ctx.req.accessToken);
const token = ctx.req.accessToken;
let canEditTracked = await models.ACL.checkAccessForToken(token, models.Sale, null, 'refund');
// const newCtx = ctx;
// newCtx.property = 'refund';
// newCtx.accessType = 'WRITE';
// newCtx.methodNames = ['refund'];
// newCtx.model = await models.Sale;
2022-10-25 13:02:22 +00:00
// let canEditTracked = await models.ACL.checkAccessForContext(newCtx);
console.log(canEditTracked);
2022-10-19 06:22:36 +00:00
2022-10-18 09:50:16 +00:00
// let canEditTracked2 = await models.ACL.checkPermission('USER', 'developer', 'Sale', 'editTracked', 'READ');
2022-10-25 13:02:22 +00:00
/* const array = ['editTracked'];
2022-10-19 06:22:36 +00:00
const AccessContext = loopBackCtx.AccessContext;
const toFind = {
2022-10-18 09:50:16 +00:00
principals: [{
type: 'ROLE',
id: 'employee'
}],
model: 'Sale',
property: 'editTracked',
2022-10-19 06:22:36 +00:00
methodNames: ['editTracked'],
accessType: 'WRITE'
};
const newContext = new AccessContext(toFind);
newContext.methodNames = ['editTracked'];
let canEditTracked3 = await models.ACL.checkAccessForContext(newContext);
let canEditTracked4 = await models.ACL.checkAccessForContext({
principals: [{
type: 'ROLE',
id: 'developer'
}],
model: 'Sale',
property: 'editTracked',
methodName: 'editTracked',
methodNames: ['editTracked'],
accessType: 'WRITE'
2022-10-18 09:50:16 +00:00
});
2022-10-19 06:22:36 +00:00
// console.log(canEditTracked);
2022-10-18 09:50:16 +00:00
// canEditTracked = await models.ACL.resolvePermission(canEditTracked);
// let canEditCloned = await models.ACL.checkPermission('ROLE', 'employee', 'Sale', 'editCloned', '*');
// let canEditWeekly = await models.ACL.checkPermission('ROLE', 'employee', 'Ticket', 'editWeekly', '*');
2022-10-18 09:50:16 +00:00
// console.log(canEditTracked, canEditTracked2);
2022-10-19 06:22:36 +00:00
console.log('DENY: ', canEditTracked3.permission);
console.log('ALLOW: ', canEditTracked4.permission);
const shouldEditTracked = canEditTracked || !hasSaleTracking;
const shouldEditCloned = canEditCloned || !hasSaleCloned;
const shouldEditWeekly = canEditWeekly || !isTicketWeekly;
const canEdit = shouldEditTracked && shouldEditCloned && shouldEditWeekly;
2022-10-18 09:50:16 +00:00
if (canEdit)
return true;
2022-10-25 13:02:22 +00:00
return false;*/
};
};