/* xeslint-disable */ import _ from "../libs/lodash.min.js"; import store from "../store"; import rights from "./bizroles"; export default { ROLE_RIGHTS: rights, AUTHORIZATION_ROLES: { ///No role set NoRole: 0, ///BizAdminLimited BizAdminLimited: 1, ///BizAdminFull BizAdminFull: 2, ///DispatchLimited DispatchLimited: 4, ///DispatchFull DispatchFull: 8, ///InventoryLimited InventoryLimited: 16, ///InventoryFull InventoryFull: 32, ///AccountingFull AccountingFull: 64, //No limited role, not sure if there is a need ///TechLimited TechLimited: 128, ///TechFull TechFull: 256, ///SubContractorLimited SubContractorLimited: 512, ///SubContractorFull SubContractorFull: 1024, ///ClientLimited ClientLimited: 2048, ///ClientFull ClientFull: 4096, ///OpsAdminLimited OpsAdminLimited: 8192, ///OpsAdminFull OpsAdminFull: 16384 }, hasRole(desiredRole) { if (!store.state.roles || store.state.roles === 0) { return false; } return (store.state.roles & desiredRole) != 0; }, ///////////////////////////////// // // getRights(vm, oType) { //from bizroles.cs: //HOW THIS WORKS / WHATS EXPECTED //Change = CREATE, RETRIEVE, UPDATE, DELETE - Full rights // //ReadFullRecord = You can read *all* the fields of the record, but can't modify it. Change is automatically checked for so only add different roles from change //PICKLIST NOTE: this does not control getting a list of names for selection which is role independent because it's required for so much indirectly //DELETE = SAME AS CHANGE FOR NOW (There is no specific delete right for now though it's checked for by routes in Authorized.cs in case we want to add it in future as a separate right from create.) //NOTE: biz rules can supersede this, this is just for general rights purposes, if an object has restrictive business rules they will take precedence every time. var ret = { change: false, read: false, delete: false }; //Get the type name from the type enum value var typeName = _.findKey(vm.$gztype, function(o) { return o == oType; }); //Get the AyaNova stock role rights for that object var objectRoleRights = this.ROLE_RIGHTS[typeName]; //get the logged in user's role var userRole = vm.$store.state.roles; //calculate the effective rights //a non zero result of the bitwise calculation means true and zero means false so using !! to force it into a boolean value (contrary to some style guides that say !! is obscure but I say it saves a lot of typing) var canChange = !!(userRole & objectRoleRights.Change); var canReadFullRecord = !!(userRole & objectRoleRights.ReadFullRecord); ret.change = canChange; ret.delete = ret.change; //FOR NOW ret.read = canReadFullRecord; return ret; } }; /* USING BITWISE OPERATORS CHEAT SHEET //https://codeburst.io/using-javascript-bitwise-operators-in-real-life-f551a731ff5 // Test whether your bit number has a single attribute. '&' ensures // an intersection between them. if (myBitNumber & HAS_FOO1) { // False, in this example } if (myBitNumber & HAS_FOO2) { // True! } // Test whether your bit number has ANY of the specified attributes if (myBitNumber & (HAS_FOO1 | HAS_FOO2)) { // True! } if (myBitNumber & (HAS_FOO1 | HAS_FOO3)) { // False } // Test whether your bit number contains ONLY the specified attributes if (myBitNumber == (HAS_FOO2 | HAS_FOO4)) { // True } if (myBitNumber == (HAS_FOO2 | HAS_FOO3 | HAS_FOO4)) { // False } // Test whether your bit number contains ALL of the given // attributes. This is slightly tricky: the union of ATTRIBUTES // can't supersede `myBitNumber` alone, otherwise it contains a bit // that `myBitNumber` doesn't. if (myBitNumber == (myBitNumber | (HAS_FOO2 | HAS_FOO4))) { // True } if (myBitNumber == (myBitNumber | (HAS_FOO2 | HAS_FOO3 | HAS_FOO4))) { // False } */