common/helpers/privileges-utils.js

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PrivilegeTypes = exports.PrivilegeType = exports.PRIVILEGE_PROPERTIES = exports.AdminActionType = void 0;
exports.checkPrivilege = checkPrivilege;
exports.getBadgeVariantFromBit = getBadgeVariantFromBit;
exports.getPrivilegeBitsArray = getPrivilegeBitsArray;
exports.getPrivilegeShieldIcon = getPrivilegeShieldIcon;
exports.getPrivilegeTitleFromBit = getPrivilegeTitleFromBit;
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.object.values.js");
require("core-js/modules/es.object.keys.js");
/* eslint-disable sort-keys */
var PRIVILEGE_PROPERTIES = {
  0: {
    title: 'Entity Editor',
    badgeVariant: 'secondary'
  },
  1: {
    title: 'Identifier Type Editor',
    badgeVariant: 'warning'
  },
  2: {
    title: 'Relationship Type Editor',
    badgeVariant: 'info'
  },
  3: {
    title: 'Reindex Search Engine',
    badgeVariant: 'success'
  },
  4: {
    title: 'Administrator',
    badgeVariant: 'danger'
  }
};
exports.PRIVILEGE_PROPERTIES = PRIVILEGE_PROPERTIES;
var PrivilegeType;
exports.PrivilegeType = PrivilegeType;
(function (PrivilegeType) {
  PrivilegeType[PrivilegeType["ADMIN"] = 16] = "ADMIN";
  PrivilegeType[PrivilegeType["REINDEX_SEARCH_SERVER"] = 8] = "REINDEX_SEARCH_SERVER";
  PrivilegeType[PrivilegeType["RELATIONSHIP_TYPE_EDITOR"] = 4] = "RELATIONSHIP_TYPE_EDITOR";
  PrivilegeType[PrivilegeType["IDENTIFIER_TYPE_EDITOR"] = 2] = "IDENTIFIER_TYPE_EDITOR";
  PrivilegeType[PrivilegeType["ENTITY_EDITOR"] = 1] = "ENTITY_EDITOR";
})(PrivilegeType || (exports.PrivilegeType = PrivilegeType = {}));
var AdminActionType;
exports.AdminActionType = AdminActionType;
(function (AdminActionType) {
  AdminActionType["CHANGE_PRIV"] = "Change Privileges";
})(AdminActionType || (exports.AdminActionType = AdminActionType = {}));
var PrivilegeTypes = {
  ADMIN_PRIV: {
    bit: 4,
    value: PrivilegeType.ADMIN
  },
  REINDEX_SEARCH_SERVER_PRIV: {
    bit: 3,
    value: PrivilegeType.REINDEX_SEARCH_SERVER
  },
  RELATIONSHIP_TYPE_EDITOR_PRIV: {
    bit: 2,
    value: PrivilegeType.RELATIONSHIP_TYPE_EDITOR
  },
  IDENTIFIER_TYPE_EDITOR_PRIV: {
    bit: 1,
    value: PrivilegeType.IDENTIFIER_TYPE_EDITOR
  },
  ENTITY_EDITOR_PRIV: {
    bit: 0,
    value: PrivilegeType.ENTITY_EDITOR
  }
};

/* eslint-disable no-bitwise */
/**
 * Retrieves the icon for the shield depending on the privileges that the user has
 *
 * @param {number} privs - the privileges of the user
 * @returns {string} - returns a string which contains the location of the PrivilegeShield Icon
 */
exports.PrivilegeTypes = PrivilegeTypes;
function getPrivilegeShieldIcon(privs) {
  // if the user has admin privilege
  if (privs & PrivilegeTypes.ADMIN_PRIV.value) {
    return '/images/icons/shield-check-orange-filled.svg';
  }
  // if the user has some special privileges, but not the admin privilege
  else if (privs > 1) {
    return '/images/icons/shield-orange-center.svg';
  }
  // if the user has no privileges
  else if (privs === 0) {
    return '/images/icons/shield-white-center.svg';
  }
  // if the user has only the Entity-editor privilege
  return '/images/icons/shield-grey-center.svg';
}

/**
 * Returns whether the user has a particular Privilege or not
 *
 * @param {number} privs - the privileges of the user
 * @param {PrivilegeType} privType - the privilege type being checked
 * @returns {boolean} - whether the user has the privilege type or not
 */
function checkPrivilege(privs, privType) {
  return Boolean(privs & privType);
}
function getPrivilegeTitleFromBit(bit) {
  return PRIVILEGE_PROPERTIES[bit].title;
}
function getBadgeVariantFromBit(bit) {
  return PRIVILEGE_PROPERTIES[bit].badgeVariant;
}

/**
 * Retrieves the bits of all the privileges set in the privs variable
 *
 * @param {number} privs - the privileges of the user
 * @throws {Error} Throws a custom error if there is some unsupported privilege type
 * @returns {Array} - returns an array of containing the bits of all the privileges the user has set
 */
function getPrivilegeBitsArray(privs) {
  var PrivBits = Object.values(PrivilegeTypes).filter(function (priv) {
    return privs & 1 << priv.bit;
  }).map(function (priv) {
    return priv.bit;
  });
  var maxBits = Object.keys(PrivilegeTypes).length;
  if (privs >= 1 << maxBits) {
    throw new Error("Unsupported set of Privileges: '".concat(privs, "'"));
  }
  return PrivBits;
}
//# sourceMappingURL=privileges-utils.js.map