UNPKG

forest-express

Version:

Official package for all Forest Express Lianas

255 lines (254 loc) 10.6 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _forestadminClient = require("@forestadmin/forestadmin-client"); var _badRequestError = _interopRequireDefault(require("../../utils/errors/bad-request-error")); var _forbiddenError = _interopRequireDefault(require("../../utils/errors/forbidden-error")); var AuthorizationService = /*#__PURE__*/function () { function AuthorizationService(_ref) { var forestAdminClient = _ref.forestAdminClient; (0, _classCallCheck2["default"])(this, AuthorizationService); (0, _defineProperty2["default"])(this, "forestAdminClient", void 0); this.forestAdminClient = forestAdminClient; } (0, _createClass2["default"])(AuthorizationService, [{ key: "assertCanBrowse", value: function () { var _assertCanBrowse = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(user, collectionName, segmentQuery) { var canBrowse, canExecuteSegmentQuery; return _regenerator["default"].wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: _context.next = 2; return this.forestAdminClient.permissionService.canOnCollection({ userId: user.id, collectionName: collectionName, event: _forestadminClient.CollectionActionEvent.Browse }); case 2: canBrowse = _context.sent; _context.t0 = !segmentQuery; if (_context.t0) { _context.next = 8; break; } _context.next = 7; return this.forestAdminClient.permissionService.canExecuteSegmentQuery({ userId: user.id, collectionName: collectionName, renderingId: user.renderingId, segmentQuery: segmentQuery }); case 7: _context.t0 = _context.sent; case 8: canExecuteSegmentQuery = _context.t0; if (!(!canBrowse || !canExecuteSegmentQuery)) { _context.next = 11; break; } throw new _forbiddenError["default"]("User ".concat(user.email, " is not authorized to browse on collection ").concat(collectionName)); case 11: case "end": return _context.stop(); } }, _callee, this); })); function assertCanBrowse(_x, _x2, _x3) { return _assertCanBrowse.apply(this, arguments); } return assertCanBrowse; }() }, { key: "assertCanRead", value: function () { var _assertCanRead = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(user, collectionName) { return _regenerator["default"].wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return this.canOnCollection(user, _forestadminClient.CollectionActionEvent.Read, collectionName); case 2: case "end": return _context2.stop(); } }, _callee2, this); })); function assertCanRead(_x4, _x5) { return _assertCanRead.apply(this, arguments); } return assertCanRead; }() }, { key: "assertCanAdd", value: function () { var _assertCanAdd = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(user, collectionName) { return _regenerator["default"].wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return this.canOnCollection(user, _forestadminClient.CollectionActionEvent.Add, collectionName); case 2: case "end": return _context3.stop(); } }, _callee3, this); })); function assertCanAdd(_x6, _x7) { return _assertCanAdd.apply(this, arguments); } return assertCanAdd; }() }, { key: "assertCanEdit", value: function () { var _assertCanEdit = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(user, collectionName) { return _regenerator["default"].wrap(function _callee4$(_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: _context4.next = 2; return this.canOnCollection(user, _forestadminClient.CollectionActionEvent.Edit, collectionName); case 2: case "end": return _context4.stop(); } }, _callee4, this); })); function assertCanEdit(_x8, _x9) { return _assertCanEdit.apply(this, arguments); } return assertCanEdit; }() }, { key: "assertCanDelete", value: function () { var _assertCanDelete = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(user, collectionName) { return _regenerator["default"].wrap(function _callee5$(_context5) { while (1) switch (_context5.prev = _context5.next) { case 0: _context5.next = 2; return this.canOnCollection(user, _forestadminClient.CollectionActionEvent.Delete, collectionName); case 2: case "end": return _context5.stop(); } }, _callee5, this); })); function assertCanDelete(_x10, _x11) { return _assertCanDelete.apply(this, arguments); } return assertCanDelete; }() }, { key: "assertCanExport", value: function () { var _assertCanExport = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(user, collectionName) { return _regenerator["default"].wrap(function _callee6$(_context6) { while (1) switch (_context6.prev = _context6.next) { case 0: _context6.next = 2; return this.canOnCollection(user, _forestadminClient.CollectionActionEvent.Export, collectionName); case 2: case "end": return _context6.stop(); } }, _callee6, this); })); function assertCanExport(_x12, _x13) { return _assertCanExport.apply(this, arguments); } return assertCanExport; }() }, { key: "canOnCollection", value: function () { var _canOnCollection = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7(user, event, collectionName) { var userId, email, canOnCollection; return _regenerator["default"].wrap(function _callee7$(_context7) { while (1) switch (_context7.prev = _context7.next) { case 0: userId = user.id, email = user.email; _context7.next = 3; return this.forestAdminClient.permissionService.canOnCollection({ userId: userId, event: event, collectionName: collectionName }); case 3: canOnCollection = _context7.sent; if (canOnCollection) { _context7.next = 6; break; } throw new _forbiddenError["default"]("User ".concat(email, " is not authorized to ").concat(event, " on collection ").concat(collectionName)); case 6: case "end": return _context7.stop(); } }, _callee7, this); })); function canOnCollection(_x14, _x15, _x16) { return _canOnCollection.apply(this, arguments); } return canOnCollection; }() }, { key: "assertCanRetrieveChart", value: function () { var _assertCanRetrieveChart = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8(_ref2) { var user, chartRequest, renderingId, userId, canRetrieveChart; return _regenerator["default"].wrap(function _callee8$(_context8) { while (1) switch (_context8.prev = _context8.next) { case 0: user = _ref2.user, chartRequest = _ref2.chartRequest; renderingId = user.renderingId, userId = user.id; _context8.prev = 2; _context8.next = 5; return this.forestAdminClient.permissionService.canExecuteChart({ renderingId: renderingId, userId: userId, chartRequest: chartRequest }); case 5: canRetrieveChart = _context8.sent; if (canRetrieveChart) { _context8.next = 8; break; } throw new _forbiddenError["default"]('User is not authorized to view this chart'); case 8: _context8.next = 15; break; case 10: _context8.prev = 10; _context8.t0 = _context8["catch"](2); if (!(_context8.t0 instanceof _forestadminClient.EmptySQLQueryError || _context8.t0 instanceof _forestadminClient.NonSelectSQLQueryError || _context8.t0 instanceof _forestadminClient.ChainedSQLQueryError)) { _context8.next = 14; break; } throw new _badRequestError["default"](_context8.t0.message); case 14: throw _context8.t0; case 15: case "end": return _context8.stop(); } }, _callee8, this, [[2, 10]]); })); function assertCanRetrieveChart(_x17) { return _assertCanRetrieveChart.apply(this, arguments); } return assertCanRetrieveChart; }() }]); return AuthorizationService; }(); exports["default"] = AuthorizationService;