deep-security
Version:
DEEP Security Library
86 lines (63 loc) • 3.81 kB
JavaScript
/**
* Created by CCristi on 7/15/16.
*/
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.BaseVoter = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _deepCore = require('deep-core');
var _deepCore2 = _interopRequireDefault(_deepCore);
var _VoterInterface2 = require('./VoterInterface');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
let BaseVoter = exports.BaseVoter = function (_VoterInterface) {
_inherits(BaseVoter, _VoterInterface);
/**
* @param {String|RegExp|Function|VoterInterface} validateExpression
*/
function BaseVoter(validateExpression) {
_classCallCheck(this, BaseVoter);
var _this = _possibleConstructorReturn(this, (BaseVoter.__proto__ || Object.getPrototypeOf(BaseVoter)).call(this));
_this._validateExpression = validateExpression;
return _this;
}
/**
* @param {String} context
* @returns {*}
*/
_createClass(BaseVoter, [{
key: 'vote',
value: function vote(context) {
if (typeof this._validateExpression === 'string') {
return this._validateExpression === context;
} else if (this._validateExpression instanceof RegExp) {
return this._validateExpression.test(context);
} else if (this._validateExpression instanceof Function) {
return this._validateExpression(context);
} else if (this._validateExpression instanceof _VoterInterface2.VoterInterface) {
return this._validateExpression.vote(context);
}
throw new _deepCore2.default.Exception.InvalidArgumentException(this._validateExpression, 'String, Number, Function, BaseVoter');
}
/**
* @param {String} actionIdentifier
* @returns {BaseVoter}
*/
}], [{
key: 'createFromAction',
value: function createFromAction(actionIdentifier) {
let actionsParts = actionIdentifier.split(':');
let escapePart = part => part && part !== '*' ? part.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') : '[a-zA-Z\\d+\\-_\\.]+';
let microservice = escapePart(actionsParts[0]);
let resource = escapePart(actionsParts[1]);
let action = escapePart(actionsParts[2]);
let regExp = new RegExp(`^\\s*${microservice}:${resource}:${action}\\s*$`);
return new BaseVoter(regExp);
}
}]);
return BaseVoter;
}(_VoterInterface2.VoterInterface);