client/helpers/adminLogs.js

"use strict";

require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/es.weak-map.js");
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.constructAdminLogStatement = constructAdminLogStatement;
require("core-js/modules/es.object.keys.js");
require("core-js/modules/es.array.slice.js");
require("core-js/modules/es.array.join.js");
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.function.name.js");
var React = _interopRequireWildcard(require("react"));
var _privilegesUtils = require("../../common/helpers/privileges-utils");
var _isomorphicDompurify = require("isomorphic-dompurify");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/*
 * Copyright (C) 2023 Shivam Awasthi
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/* eslint-disable no-bitwise */
function getPrivsChanged(newPrivs, oldPrivs) {
  var privsRemoved = [];
  var privsAdded = [];
  var totalBits = Object.keys(_privilegesUtils.PrivilegeTypes).length;
  for (var i = 0; i < totalBits; i++) {
    if (!(newPrivs & 1 << i) && oldPrivs & 1 << i) {
      privsRemoved.push((0, _privilegesUtils.getPrivilegeTitleFromBit)(i));
    }
    if (!(oldPrivs & 1 << i) && newPrivs & 1 << i) {
      privsAdded.push((0, _privilegesUtils.getPrivilegeTitleFromBit)(i));
    }
  }
  return {
    privsAdded: privsAdded,
    privsRemoved: privsRemoved
  };
}
function constructPrivsChangeStatement(logData) {
  var newPrivs = logData.newPrivs,
    oldPrivs = logData.oldPrivs,
    targetUserId = logData.targetUserId,
    targetUser = logData.targetUser,
    adminId = logData.adminId,
    admin = logData.admin;
  var _getPrivsChanged = getPrivsChanged(newPrivs, oldPrivs),
    privsAdded = _getPrivsChanged.privsAdded,
    privsRemoved = _getPrivsChanged.privsRemoved;
  var grantStatement = '';
  if (privsAdded.length) {
    grantStatement = ' granted ';
    var lastItem = privsAdded.at(-1);
    var otherPrivs = privsAdded.slice(0, -1);
    if (otherPrivs.length) {
      grantStatement += otherPrivs.map(function (priv) {
        return "<strong>".concat(priv, "</strong>");
      }).join(', ');
      grantStatement += ' and ';
    }
    grantStatement += "<strong>".concat(lastItem, "</strong>");
    grantStatement += ' privilege';
    if (privsAdded.length > 1) {
      grantStatement += 's';
    }
  }
  var andStatement = privsAdded.length && privsRemoved.length ? ' and' : '';
  var removedStatement = '';
  if (privsRemoved.length) {
    removedStatement = ' removed ';
    var _lastItem = privsRemoved.at(-1);
    var _otherPrivs = privsRemoved.slice(0, -1);
    if (_otherPrivs.length) {
      removedStatement += _otherPrivs.map(function (priv) {
        return "<strong>".concat(priv, "</strong>");
      }).join(', ');
      removedStatement += ' and ';
    }
    removedStatement += "<strong>".concat(_lastItem, "</strong>");
    removedStatement += ' privilege';
    if (privsRemoved.length > 1) {
      removedStatement += 's';
    }
  }
  var preposition = privsRemoved.length ? ' from ' : ' to ';
  var finalStatement = grantStatement + andStatement + removedStatement + preposition;
  /* eslint-disable react/no-danger */
  // We are disabling this rule because we are already sanitizing the html here
  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("a", {
    href: "/editor/".concat(adminId)
  }, admin.name), /*#__PURE__*/React.createElement("span", {
    dangerouslySetInnerHTML: {
      __html: (0, _isomorphicDompurify.sanitize)(finalStatement)
    }
  }), /*#__PURE__*/React.createElement("a", {
    href: "/editor/".concat(targetUserId)
  }, targetUser.name), ".");
}

/**
 * Constructs a log statement for each administrative action for the Admin Logs Page
 * @function constructAdminLogStatement
 * @param {AdminLogDataT} logData - the data for the admin log action
 * @returns {string} A statement of the log depending upon the AdminActionType
 */
function constructAdminLogStatement(logData) {
  if (logData.actionType === _privilegesUtils.AdminActionType.CHANGE_PRIV) {
    return constructPrivsChangeStatement(logData);
  }
  return '';
}
//# sourceMappingURL=adminLogs.js.map