123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- "use strict";
- var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
- var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
- _Object$defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/keys"));
- var _typeof2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/typeof"));
- var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
- var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
- var _ParseRole = _interopRequireDefault(require("./ParseRole"));
- var _ParseUser = _interopRequireDefault(require("./ParseUser"));
- var PUBLIC_KEY = '*';
- var ParseACL = function () {
-
- function ParseACL(arg1 /*: ParseUser | ByIdMap*/) {
- (0, _classCallCheck2.default)(this, ParseACL);
- (0, _defineProperty2.default)(this, "permissionsById", void 0);
- this.permissionsById = {};
- if (arg1 && (0, _typeof2.default)(arg1) === 'object') {
- if (arg1 instanceof _ParseUser.default) {
- this.setReadAccess(arg1, true);
- this.setWriteAccess(arg1, true);
- } else {
- for (var _userId in arg1) {
- var accessList = arg1[_userId];
- this.permissionsById[_userId] = {};
- for (var _permission in accessList) {
- var allowed = accessList[_permission];
- if (_permission !== 'read' && _permission !== 'write') {
- throw new TypeError('Tried to create an ACL with an invalid permission type.');
- }
- if (typeof allowed !== 'boolean') {
- throw new TypeError('Tried to create an ACL with an invalid permission value.');
- }
- this.permissionsById[_userId][_permission] = allowed;
- }
- }
- }
- } else if (typeof arg1 === 'function') {
- throw new TypeError('ParseACL constructed with a function. Did you forget ()?');
- }
- }
-
- (0, _createClass2.default)(ParseACL, [{
- key: "toJSON",
- value: function () /*: ByIdMap*/{
- var permissions = {};
- for (var p in this.permissionsById) {
- permissions[p] = this.permissionsById[p];
- }
- return permissions;
- }
-
- }, {
- key: "equals",
- value: function (other /*: ParseACL*/) /*: boolean*/{
- if (!(other instanceof ParseACL)) {
- return false;
- }
- var users = (0, _keys.default)(this.permissionsById);
- var otherUsers = (0, _keys.default)(other.permissionsById);
- if (users.length !== otherUsers.length) {
- return false;
- }
- for (var u in this.permissionsById) {
- if (!other.permissionsById[u]) {
- return false;
- }
- if (this.permissionsById[u].read !== other.permissionsById[u].read) {
- return false;
- }
- if (this.permissionsById[u].write !== other.permissionsById[u].write) {
- return false;
- }
- }
- return true;
- }
- }, {
- key: "_setAccess",
- value: function (accessType /*: string*/, userId /*: ParseUser | ParseRole | string*/, allowed /*: boolean*/) {
- if (userId instanceof _ParseUser.default) {
- userId = userId.id;
- } else if (userId instanceof _ParseRole.default) {
- var name = userId.getName();
- if (!name) {
- throw new TypeError('Role must have a name');
- }
- userId = 'role:' + name;
- }
- if (typeof userId !== 'string') {
- throw new TypeError('userId must be a string.');
- }
- if (typeof allowed !== 'boolean') {
- throw new TypeError('allowed must be either true or false.');
- }
- var permissions = this.permissionsById[userId];
- if (!permissions) {
- if (!allowed) {
-
- return;
- } else {
- permissions = {};
- this.permissionsById[userId] = permissions;
- }
- }
- if (allowed) {
- this.permissionsById[userId][accessType] = true;
- } else {
- delete permissions[accessType];
- if ((0, _keys.default)(permissions).length === 0) {
- delete this.permissionsById[userId];
- }
- }
- }
- }, {
- key: "_getAccess",
- value: function (accessType /*: string*/, userId /*: ParseUser | ParseRole | string*/) /*: boolean*/{
- if (userId instanceof _ParseUser.default) {
- userId = userId.id;
- if (!userId) {
- throw new Error('Cannot get access for a ParseUser without an ID');
- }
- } else if (userId instanceof _ParseRole.default) {
- var name = userId.getName();
- if (!name) {
- throw new TypeError('Role must have a name');
- }
- userId = 'role:' + name;
- }
- var permissions = this.permissionsById[userId];
- if (!permissions) {
- return false;
- }
- return !!permissions[accessType];
- }
-
- }, {
- key: "setReadAccess",
- value: function (userId /*: ParseUser | ParseRole | string*/, allowed /*: boolean*/) {
- this._setAccess('read', userId, allowed);
- }
-
- }, {
- key: "getReadAccess",
- value: function (userId /*: ParseUser | ParseRole | string*/) /*: boolean*/{
- return this._getAccess('read', userId);
- }
-
- }, {
- key: "setWriteAccess",
- value: function (userId /*: ParseUser | ParseRole | string*/, allowed /*: boolean*/) {
- this._setAccess('write', userId, allowed);
- }
-
- }, {
- key: "getWriteAccess",
- value: function (userId /*: ParseUser | ParseRole | string*/) /*: boolean*/{
- return this._getAccess('write', userId);
- }
-
- }, {
- key: "setPublicReadAccess",
- value: function (allowed /*: boolean*/) {
- this.setReadAccess(PUBLIC_KEY, allowed);
- }
-
- }, {
- key: "getPublicReadAccess",
- value: function () /*: boolean*/{
- return this.getReadAccess(PUBLIC_KEY);
- }
-
- }, {
- key: "setPublicWriteAccess",
- value: function (allowed /*: boolean*/) {
- this.setWriteAccess(PUBLIC_KEY, allowed);
- }
-
- }, {
- key: "getPublicWriteAccess",
- value: function () /*: boolean*/{
- return this.getWriteAccess(PUBLIC_KEY);
- }
-
- }, {
- key: "getRoleReadAccess",
- value: function (role /*: ParseRole | string*/) /*: boolean*/{
- if (role instanceof _ParseRole.default) {
-
- role = role.getName();
- }
- if (typeof role !== 'string') {
- throw new TypeError('role must be a ParseRole or a String');
- }
- return this.getReadAccess('role:' + role);
- }
-
- }, {
- key: "getRoleWriteAccess",
- value: function (role /*: ParseRole | string*/) /*: boolean*/{
- if (role instanceof _ParseRole.default) {
-
- role = role.getName();
- }
- if (typeof role !== 'string') {
- throw new TypeError('role must be a ParseRole or a String');
- }
- return this.getWriteAccess('role:' + role);
- }
-
- }, {
- key: "setRoleReadAccess",
- value: function (role /*: ParseRole | string*/, allowed /*: boolean*/) {
- if (role instanceof _ParseRole.default) {
-
- role = role.getName();
- }
- if (typeof role !== 'string') {
- throw new TypeError('role must be a ParseRole or a String');
- }
- this.setReadAccess('role:' + role, allowed);
- }
-
- }, {
- key: "setRoleWriteAccess",
- value: function (role /*: ParseRole | string*/, allowed /*: boolean*/) {
- if (role instanceof _ParseRole.default) {
-
- role = role.getName();
- }
- if (typeof role !== 'string') {
- throw new TypeError('role must be a ParseRole or a String');
- }
- this.setWriteAccess('role:' + role, allowed);
- }
- }]);
- return ParseACL;
- }();
- var _default = ParseACL;
- exports.default = _default;
|