UNPKG

kindergarten

Version:

Kindergarten is a JavaScript library which helps programmers to achieve modular security using sandbox pattern

106 lines (75 loc) 3.21 kB
'use strict'; exports.__esModule = true; exports.default = 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 _isArray = require('lodash/isArray'); var _isArray2 = _interopRequireDefault(_isArray); var _isString = require('lodash/isString'); var _isString2 = _interopRequireDefault(_isString); var _AllowedMethodsService = require('../utils/AllowedMethodsService'); var _AllowedMethodsService2 = _interopRequireDefault(_AllowedMethodsService); var _errors = require('../errors'); 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"); } } // The basic RegEx for validating that rule string is correct. The type of the // rule is later on validated using AllowedMethodsService as well. var TYPE_REGEX = /^can(not)? ([a-z_$][a-zA-Z0-9_$]*)$/; /** * Implementation of the Rule Type class. * Type is responsible for the rule string e.g. `can watch` validation and * for extracting all relevant infos out of that string. */ var Type = function () { function Type(rule, str) { _classCallCheck(this, Type); var match = (0, _isString2.default)(str) && str.match(TYPE_REGEX); // Extract type of the rule. // e.g. "cannot watch" => "watch" this.type = function () { return (0, _isArray2.default)(match) ? match[2] : undefined; }(); /** * Just store the given string. Used internally by Type class. */ this.raw = str; /** * Reference to a original rule */ this.rule = rule; this.validate(); // 'can' rules are positive 'cannot' rules are NOT positive. this._isPositive = !match[1]; } /** * Throws an error if the type of the rule is not applicable. */ _createClass(Type, [{ key: 'validate', value: function validate() { var allowedMethodsService = new _AllowedMethodsService2.default(); var type = this.type; if (!(0, _isString2.default)(type) || allowedMethodsService.isRestricted(type)) { throw new _errors.WrongRuleDefinition('Cannot create a rule ' + this.raw + '. The type of the rule cannot be parsed.'); } return true; } /** * `can` rules are positive `cannot` rules are negative. */ }, { key: 'isPositive', value: function isPositive() { return !!this._isPositive; } /** * Opposite of `isPositive()` method. */ }, { key: 'isNegative', value: function isNegative() { return !this.isPositive(); } }]); return Type; }(); exports.default = Type;