deep-security
Version:
DEEP Security Library
64 lines (51 loc) • 1.93 kB
JavaScript
/**
* Created by CCristi on 7/15/16.
*/
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.BaseVoter = undefined;
var _deepCore = require('deep-core');
var _deepCore2 = _interopRequireDefault(_deepCore);
var _VoterInterface = require('./VoterInterface');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
class BaseVoter extends _VoterInterface.VoterInterface {
/**
* @param {String|RegExp|Function|VoterInterface} validateExpression
*/
constructor(validateExpression) {
super();
this._validateExpression = validateExpression;
}
/**
* @param {String} context
* @returns {*}
*/
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 _VoterInterface.VoterInterface) {
return this._validateExpression.vote(context);
}
throw new _deepCore2.default.Exception.InvalidArgumentException(this._validateExpression, 'String, Number, Function, BaseVoter');
}
/**
* @param {String} actionIdentifier
* @returns {BaseVoter}
*/
static 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);
}
}
exports.BaseVoter = BaseVoter;