UNPKG

backendless

Version:

Backendless JavaScript SDK for Node.js and the browser

170 lines (169 loc) 6.86 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 _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); 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 _dataQueryBuilder = _interopRequireDefault(require("../data/data-query-builder")); var UsersRoles = /*#__PURE__*/function () { function UsersRoles(users) { (0, _classCallCheck2["default"])(this, UsersRoles); this.users = users; this.app = users.app; } (0, _createClass2["default"])(UsersRoles, [{ key: "findByRole", value: function () { var _findByRole = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(roleName, loadRoles, query) { var _this = this; var queryStringTokens; return _regenerator["default"].wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: if (!(!roleName || typeof roleName !== 'string')) { _context.next = 2; break; } throw new Error('Role Name must be a string and can not be empty'); case 2: if (!(loadRoles !== null && loadRoles !== undefined && typeof loadRoles !== 'boolean')) { _context.next = 4; break; } throw new Error('The second argument "loadRoles" can be a boolean only'); case 4: if (!(query !== null && query !== undefined && (Array.isArray(query) || (0, _typeof2["default"])(query) !== 'object'))) { _context.next = 6; break; } throw new Error('The third argument "query" can be an instance of DataQueryBuilder or a plain object only'); case 6: queryStringTokens = []; if (typeof loadRoles === 'boolean') { queryStringTokens.push("loadRoles=".concat(loadRoles)); } if (query) { queryStringTokens.push(_dataQueryBuilder["default"].toQueryString(query)); } return _context.abrupt("return", this.app.request.get({ url: this.app.urls.usersRole(roleName), queryString: queryStringTokens.join('&') }).then(function (result) { return _this.users.dataStore.parseResponse(result); })); case 10: case "end": return _context.stop(); } }, _callee, this); })); function findByRole(_x, _x2, _x3) { return _findByRole.apply(this, arguments); } return findByRole; }() }, { key: "getUserRoles", value: function () { var _getUserRoles = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(userId) { return _regenerator["default"].wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: return _context2.abrupt("return", this.app.request.get({ url: this.app.urls.userRoles(userId) })); case 1: case "end": return _context2.stop(); } }, _callee2, this); })); function getUserRoles(_x4) { return _getUserRoles.apply(this, arguments); } return getUserRoles; }() }, { key: "assignRole", value: function () { var _assignRole = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(identity, rolename) { return _regenerator["default"].wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: return _context3.abrupt("return", this.changeRole(identity, rolename, 'assignRole')); case 1: case "end": return _context3.stop(); } }, _callee3, this); })); function assignRole(_x5, _x6) { return _assignRole.apply(this, arguments); } return assignRole; }() }, { key: "unassignRole", value: function () { var _unassignRole = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(identity, rolename) { return _regenerator["default"].wrap(function _callee4$(_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: return _context4.abrupt("return", this.changeRole(identity, rolename, 'unassignRole')); case 1: case "end": return _context4.stop(); } }, _callee4, this); })); function unassignRole(_x7, _x8) { return _unassignRole.apply(this, arguments); } return unassignRole; }() }, { key: "changeRole", value: function () { var _changeRole = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(identity, roleName, operation) { return _regenerator["default"].wrap(function _callee5$(_context5) { while (1) switch (_context5.prev = _context5.next) { case 0: if (!(!identity || !(typeof identity === 'string' || typeof identity === 'number'))) { _context5.next = 2; break; } throw new Error('User identity must be a string or number and can not be empty.'); case 2: if (!(!roleName || typeof roleName !== 'string')) { _context5.next = 4; break; } throw new Error('Role Name must be a string and can not be empty.'); case 4: return _context5.abrupt("return", this.app.request.post({ url: this.app.urls.userRoleOperation(operation), data: { user: identity, roleName: roleName } })); case 5: case "end": return _context5.stop(); } }, _callee5, this); })); function changeRole(_x9, _x10, _x11) { return _changeRole.apply(this, arguments); } return changeRole; }() }]); return UsersRoles; }(); exports["default"] = UsersRoles;