UNPKG

@aspectus/permissions

Version:

Basement for permissions checking functionality.

225 lines (184 loc) 5.67 kB
/*! * permissions v0.10.20 * (c) 2020 Alex Tkachenko * Released under the MIT License. */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); /* eslint-disable import/prefer-default-export, prefer-spread */ /** * Multiple permissions checker. * * @export * @param {any} permissions - List of permissions. * @returns {Promise} - It will be resolved only if all permissions will * be successful. */ function hasAccess(permissions) { for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } return Promise.all(permissions.map(function (x) { return x.onHasAccess.apply(x, args); })); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } /* eslint-disable class-methods-use-this, no-unused-vars, prefer-spread, prefer-rest-params */ var Permission = /*#__PURE__*/ function () { function Permission() { _classCallCheck(this, Permission); } _createClass(Permission, [{ key: "hasAccess", /** * Permission checker function. */ value: function hasAccess() { return true; } /** * Promise resolvable checker. * * Same as `hasAccess` method, but returns a promise instead of bare * boolean. This done, because user will be available only after * fetching it from api. Or there might be some other asynchronous checker. */ }, { key: "onHasAccess", value: function onHasAccess() { var _this = this; var args = arguments; return new Promise(function (resolve, reject) { return _this.hasAccess.apply(_this, args) ? resolve() : reject(); }); } }]); return Permission; }(); var AllowAny = /*#__PURE__*/ function (_Permission) { _inherits(AllowAny, _Permission); function AllowAny() { _classCallCheck(this, AllowAny); return _possibleConstructorReturn(this, _getPrototypeOf(AllowAny).apply(this, arguments)); } return AllowAny; }(Permission); var PermissionsCollection = /*#__PURE__*/ function (_Permission2) { _inherits(PermissionsCollection, _Permission2); function PermissionsCollection() { var _this; _classCallCheck(this, PermissionsCollection); _this = _possibleConstructorReturn(this, _getPrototypeOf(PermissionsCollection).call(this)); _this.collection = Array.from(arguments); return _this; } _createClass(PermissionsCollection, [{ key: "executeCollection", value: function executeCollection(args) { return this.collection.map(function (permission) { return permission.onHasAccess.apply(permission, args); }); } }]); return PermissionsCollection; }(Permission); var Or = /*#__PURE__*/ function (_PermissionsCollectio) { _inherits(Or, _PermissionsCollectio); function Or() { _classCallCheck(this, Or); return _possibleConstructorReturn(this, _getPrototypeOf(Or).apply(this, arguments)); } _createClass(Or, [{ key: "onHasAccess", value: function onHasAccess() { return Promise.any(this.executeCollection(arguments)); } }]); return Or; }(PermissionsCollection); var And = /*#__PURE__*/ function (_PermissionsCollectio2) { _inherits(And, _PermissionsCollectio2); function And() { _classCallCheck(this, And); return _possibleConstructorReturn(this, _getPrototypeOf(And).apply(this, arguments)); } _createClass(And, [{ key: "onHasAccess", value: function onHasAccess() { return Promise.all(this.executeCollection(arguments)); } }]); return And; }(PermissionsCollection); exports.AllowAny = AllowAny; exports.And = And; exports.Or = Or; exports.Permission = Permission; exports.PermissionsCollection = PermissionsCollection; exports.hasAccess = hasAccess;