01 | const user = {
_ROLES_BIN: 30,
_ROLES_RAW: [
{ id: 4, name: 'broker', type: 'broker' },
{ id: 5, name: 'introducer', type: 'introducer' },
{ id: 7, name: 'super_broker', type: 'super_broker' },
{ id: 8, name: 'broker_ops', type: 'broker_ops' },
],
};
const ROLES = {
Client: 'client',
Introducer: 'introducer',
Broker: 'broker',
BrokerOps: 'broker_ops',
SuperBroker: 'super_broker',
};
const ROLES_BINARY_SHIFT = Object.values(ROLES).reduce((roles, role, index) => {
roles[role] = index;
return roles;
}, { });
const getBinRole = role => 0b0000_0001 << ROLES_BINARY_SHIFT[role];
const hasRole = (roleName) => {
const binRole = getBinRole(roleName);
return (binRole & user._ROLES_BIN) > 0;
};
hasRole('introducer');
| ready |
02 | const user = {
_ROLES_BIN: 30,
_ROLES_RAW: [
{ id: 4, name: 'broker', type: 'broker' },
{ id: 5, name: 'introducer', type: 'introducer' },
{ id: 7, name: 'super_broker', type: 'super_broker' },
{ id: 8, name: 'broker_ops', type: 'broker_ops' }
],
};
const ROLES = ['client', 'introducer', 'broker', 'broker_ops', 'super_broker'];
const getBinRole = roleName => {
const index = ROLES.indexOf(roleName);
return index !== -1 ? 0b0000_0001 << index : 0;
};
const hasRole = roleName => {
const binRole = getBinRole(roleName);
return (binRole & user._ROLES_BIN) > 0;
};
hasRole('introducer');
| ready |