@kurbar/access-control
Version:
Role, Attribute and Condition based Access Control for Node.js
57 lines (56 loc) • 2.29 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EqualsCondition = void 0;
var common_1 = require("./../utils/common");
var core_1 = require("../core");
var util_1 = require("./util");
/**
* Equals condition
*
* @author Dilip Kola <dilip@tensult.com>
*/
var EqualsCondition = /** @class */ (function () {
function EqualsCondition() {
this.defaultOptions = { allowUndefined: false };
}
EqualsCondition.prototype.evaluate = function (args, context, options) {
if (!args) {
return true;
}
if (!context) {
return false;
}
var opts = __assign(__assign({}, this.defaultOptions), (options || {}));
if (common_1.CommonUtil.type(args) !== 'object') {
throw new core_1.AccessControlError('EqualsCondition expects type of args to be object');
}
return Object.keys(args).every(function (key) {
return common_1.CommonUtil.matchesAnyElement(args[key], function (elm) {
var keyValue = key.startsWith('$.') ? util_1.ConditionUtil.getValueByPath(context, key) : context[key];
if (keyValue === undefined && !opts.allowUndefined) {
// throw new AccessControlError(`Value for key "${key}" evaluated to undefined and allowUndefined is set to false`);
return false;
}
var comparator = util_1.ConditionUtil.getValueByPath(context, elm);
if (comparator === undefined && !opts.allowUndefined) {
// throw new AccessControlError(`Value for key "${elm}" evaluated to undefined and allowUndefined is set to false`);
return false;
}
return comparator === keyValue;
});
});
};
return EqualsCondition;
}());
exports.EqualsCondition = EqualsCondition;