Files
raven-client/ayanova/src/api/authorizationroles.js
2019-05-22 00:13:30 +00:00

130 lines
4.2 KiB
JavaScript

/* eslint-disable */
import _ from "../libs/lodash.min.js";
import store from "../store";
import rights from "./bizroles";
export default {
ROLE_RIGHTS: rights,
AUTHORIZATION_ROLES: {
///<summary>No role set</summary>
NoRole: 0,
///<summary>BizAdminLimited</summary>
BizAdminLimited: 1,
///<summary>BizAdminFull</summary>
BizAdminFull: 2,
///<summary>DispatchLimited</summary>
DispatchLimited: 4,
///<summary>DispatchFull</summary>
DispatchFull: 8,
///<summary>InventoryLimited</summary>
InventoryLimited: 16,
///<summary>InventoryFull</summary>
InventoryFull: 32,
///<summary>AccountingFull</summary>
AccountingFull: 64, //No limited role, not sure if there is a need
///<summary>TechLimited</summary>
TechLimited: 128,
///<summary>TechFull</summary>
TechFull: 256,
///<summary>SubContractorLimited</summary>
SubContractorLimited: 512,
///<summary>SubContractorFull</summary>
SubContractorFull: 1024,
///<summary>ClientLimited</summary>
ClientLimited: 2048,
///<summary>ClientFull</summary>
ClientFull: 4096,
///<summary>OpsAdminLimited</summary>
OpsAdminLimited: 8192,
///<summary>OpsAdminFull</summary>
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
}
*/