UNPKG

backendless

Version:

Backendless JavaScript SDK for Node.js and the browser

129 lines (125 loc) 4.14 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; 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 PermissionTypes = { GRANT: 'GRANT', DENY: 'DENY' }; var Permission = /*#__PURE__*/function () { function Permission(permission, app) { (0, _classCallCheck2["default"])(this, Permission); this.permission = permission; this.app = app; this.grantUser = this.backwardCompatibility('grantForUser', 'grantUser'); this.denyUser = this.backwardCompatibility('denyForUser', 'denyUser'); this.grantRole = this.backwardCompatibility('grantForRole', 'grantRole'); this.denyRole = this.backwardCompatibility('denyForRole', 'denyRole'); this.grant = this.backwardCompatibility('grantForAllUsers', 'grant'); this.deny = this.backwardCompatibility('denyForAllUsers', 'deny'); } (0, _createClass2["default"])(Permission, [{ key: "backwardCompatibility", value: function backwardCompatibility(methodName, oldMethodName) { //TODO: this method will be removed var context = this; var bcLabel = this.constructor.BACKWARD_COMPATIBILITY_LABEL; return function () { var mainMessage = "\"".concat(bcLabel, ".").concat(oldMethodName, "\" is deprecated and will be removed in the nearest release."); var helpMessage = "Please use \"".concat(bcLabel, ".").concat(methodName, "\" instead of."); // eslint-disable-next-line no-console console.warn("".concat(mainMessage, "\n").concat(helpMessage)); return context[methodName].apply(context, arguments); }; } }, { key: "grantForUser", value: function grantForUser(userId, object) { return this.sendRequest(PermissionTypes.GRANT, object, { userId: userId }); } }, { key: "denyForUser", value: function denyForUser(userId, object) { return this.sendRequest(PermissionTypes.DENY, object, { userId: userId }); } }, { key: "grantForRole", value: function grantForRole(roleName, object) { return this.sendRequest(PermissionTypes.GRANT, object, { roleName: roleName }); } }, { key: "denyForRole", value: function denyForRole(roleName, object) { return this.sendRequest(PermissionTypes.DENY, object, { roleName: roleName }); } }, { key: "grantForAllUsers", value: function grantForAllUsers(object) { return this.sendRequest(PermissionTypes.GRANT, object, { userId: '*' }); } }, { key: "denyForAllUsers", value: function denyForAllUsers(object) { return this.sendRequest(PermissionTypes.DENY, object, { userId: '*' }); } }, { key: "grantForAllRoles", value: function grantForAllRoles(object) { return this.sendRequest(PermissionTypes.GRANT, object, { roleName: '*' }); } }, { key: "denyForAllRoles", value: function denyForAllRoles(object) { return this.sendRequest(PermissionTypes.DENY, object, { roleName: '*' }); } }, { key: "sendRequest", value: function sendRequest(type, entity, _ref) { var userId = _ref.userId, roleName = _ref.roleName; var data = { permission: this.permission }; if (userId) { data.user = userId; } else if (roleName) { data.role = roleName; } return this.app.request.put({ url: this.getRequestURL(type, entity), data: data }); } /** * @abstract **/ }, { key: "getRequestURL", value: function getRequestURL( /** type, entity **/ ) {} }]); return Permission; }(); exports["default"] = Permission; (0, _defineProperty2["default"])(Permission, "BACKWARD_COMPATIBILITY_LABEL", null);