ParseCLP.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  3. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  4. var _Object$defineProperties = require("@babel/runtime-corejs3/core-js-stable/object/define-properties");
  5. var _Object$getOwnPropertyDescriptors = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors");
  6. var _forEachInstanceProperty = require("@babel/runtime-corejs3/core-js-stable/instance/for-each");
  7. var _Object$getOwnPropertyDescriptor = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor");
  8. var _filterInstanceProperty = require("@babel/runtime-corejs3/core-js-stable/instance/filter");
  9. var _Object$getOwnPropertySymbols = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols");
  10. var _Object$keys2 = require("@babel/runtime-corejs3/core-js-stable/object/keys");
  11. var _Array$isArray2 = require("@babel/runtime-corejs3/core-js-stable/array/is-array");
  12. var _getIteratorMethod = require("@babel/runtime-corejs3/core-js/get-iterator-method");
  13. var _Symbol = require("@babel/runtime-corejs3/core-js-stable/symbol");
  14. var _Array$from = require("@babel/runtime-corejs3/core-js-stable/array/from");
  15. var _sliceInstanceProperty2 = require("@babel/runtime-corejs3/core-js-stable/instance/slice");
  16. _Object$defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.default = void 0;
  20. var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array"));
  21. var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/keys"));
  22. var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice"));
  23. var _assign = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/assign"));
  24. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/slicedToArray"));
  25. var _entries = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/entries"));
  26. var _every = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/every"));
  27. var _includes = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/includes"));
  28. var _typeof2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/typeof"));
  29. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
  30. var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
  31. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
  32. var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/map"));
  33. var _ParseRole = _interopRequireDefault(require("./ParseRole"));
  34. var _ParseUser = _interopRequireDefault(require("./ParseUser"));
  35. function ownKeys(object, enumerableOnly) {
  36. var keys = _Object$keys2(object);
  37. if (_Object$getOwnPropertySymbols) {
  38. var symbols = _Object$getOwnPropertySymbols(object);
  39. enumerableOnly && (symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) {
  40. return _Object$getOwnPropertyDescriptor(object, sym).enumerable;
  41. })), keys.push.apply(keys, symbols);
  42. }
  43. return keys;
  44. }
  45. function _objectSpread(target) {
  46. for (var i = 1; i < arguments.length; i++) {
  47. var _context3, _context4;
  48. var source = null != arguments[i] ? arguments[i] : {};
  49. i % 2 ? _forEachInstanceProperty(_context3 = ownKeys(Object(source), !0)).call(_context3, function (key) {
  50. (0, _defineProperty2.default)(target, key, source[key]);
  51. }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)) : _forEachInstanceProperty(_context4 = ownKeys(Object(source))).call(_context4, function (key) {
  52. _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key));
  53. });
  54. }
  55. return target;
  56. }
  57. function _createForOfIteratorHelper(o, allowArrayLike) {
  58. var it = typeof _Symbol !== "undefined" && _getIteratorMethod(o) || o["@@iterator"];
  59. if (!it) {
  60. if (_Array$isArray2(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
  61. if (it) o = it;
  62. var i = 0;
  63. var F = function () {};
  64. return {
  65. s: F,
  66. n: function () {
  67. if (i >= o.length) return {
  68. done: true
  69. };
  70. return {
  71. done: false,
  72. value: o[i++]
  73. };
  74. },
  75. e: function (_e) {
  76. throw _e;
  77. },
  78. f: F
  79. };
  80. }
  81. throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  82. }
  83. var normalCompletion = true,
  84. didErr = false,
  85. err;
  86. return {
  87. s: function () {
  88. it = it.call(o);
  89. },
  90. n: function () {
  91. var step = it.next();
  92. normalCompletion = step.done;
  93. return step;
  94. },
  95. e: function (_e2) {
  96. didErr = true;
  97. err = _e2;
  98. },
  99. f: function () {
  100. try {
  101. if (!normalCompletion && it.return != null) it.return();
  102. } finally {
  103. if (didErr) throw err;
  104. }
  105. }
  106. };
  107. }
  108. function _unsupportedIterableToArray(o, minLen) {
  109. var _context2;
  110. if (!o) return;
  111. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  112. var n = _sliceInstanceProperty2(_context2 = Object.prototype.toString.call(o)).call(_context2, 8, -1);
  113. if (n === "Object" && o.constructor) n = o.constructor.name;
  114. if (n === "Map" || n === "Set") return _Array$from(o);
  115. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  116. }
  117. function _arrayLikeToArray(arr, len) {
  118. if (len == null || len > arr.length) len = arr.length;
  119. for (var i = 0, arr2 = new Array(len); i < len; i++) {
  120. arr2[i] = arr[i];
  121. }
  122. return arr2;
  123. }
  124. var PUBLIC_KEY = '*';
  125. var VALID_PERMISSIONS
  126. /*: Map<string, UsersMap>*/
  127. = new _map.default();
  128. VALID_PERMISSIONS.set('get', {});
  129. VALID_PERMISSIONS.set('find', {});
  130. VALID_PERMISSIONS.set('count', {});
  131. VALID_PERMISSIONS.set('create', {});
  132. VALID_PERMISSIONS.set('update', {});
  133. VALID_PERMISSIONS.set('delete', {});
  134. VALID_PERMISSIONS.set('addField', {});
  135. var VALID_PERMISSIONS_EXTENDED
  136. /*: Map<string, UsersMap>*/
  137. = new _map.default();
  138. VALID_PERMISSIONS_EXTENDED.set('protectedFields', {});
  139. /**
  140. * Creates a new CLP.
  141. * If no argument is given, the CLP has no permissions for anyone.
  142. * If the argument is a Parse.User or Parse.Role, the CLP will have read and write
  143. * permission for only that user or role.
  144. * If the argument is any other JSON object, that object will be interpretted
  145. * as a serialized CLP created with toJSON().
  146. *
  147. * <p>A CLP, or Class Level Permissions can be added to any
  148. * <code>Parse.Schema</code> to restrict access to only a subset of users
  149. * of your application.</p>
  150. *
  151. * <p>
  152. * For get/count/find/create/update/delete/addField using the following functions:
  153. *
  154. * Entity is type Parse.User or Parse.Role or string
  155. * Role is type Parse.Role or Name of Parse.Role
  156. *
  157. * getGetRequiresAuthentication()
  158. * setGetRequiresAuthentication(allowed: boolean)
  159. * getGetPointerFields()
  160. * setGetPointerFields(pointerFields: string[])
  161. * getGetAccess(entity: Entity)
  162. * setGetAccess(entity: Entity, allowed: boolean)
  163. * getPublicGetAccess()
  164. * setPublicGetAccess(allowed: boolean)
  165. * getRoleGetAccess(role: Role)
  166. * setRoleGetAccess(role: Role, allowed: boolean)
  167. * getFindRequiresAuthentication()
  168. * setFindRequiresAuthentication(allowed: boolean)
  169. * getFindPointerFields()
  170. * setFindPointerFields(pointerFields: string[])
  171. * getFindAccess(entity: Entity)
  172. * setFindAccess(entity: Entity, allowed: boolean)
  173. * getPublicFindAccess()
  174. * setPublicFindAccess(allowed: boolean)
  175. * getRoleFindAccess(role: Role)
  176. * setRoleFindAccess(role: Role, allowed: boolean)
  177. * getCountRequiresAuthentication()
  178. * setCountRequiresAuthentication(allowed: boolean)
  179. * getCountPointerFields()
  180. * setCountPointerFields(pointerFields: string[])
  181. * getCountAccess(entity: Entity)
  182. * setCountAccess(entity: Entity, allowed: boolean)
  183. * getPublicCountAccess()
  184. * setPublicCountAccess(allowed: boolean)
  185. * getRoleCountAccess(role: Role)
  186. * setRoleCountAccess(role: Role, allowed: boolean)
  187. * getCreateRequiresAuthentication()
  188. * setCreateRequiresAuthentication(allowed: boolean)
  189. * getCreatePointerFields()
  190. * setCreatePointerFields(pointerFields: string[])
  191. * getCreateAccess(entity: Entity)
  192. * setCreateAccess(entity: Entity, allowed: boolean)
  193. * getPublicCreateAccess()
  194. * setPublicCreateAccess(allowed: Boolean)
  195. * getRoleCreateAccess(role: Role)
  196. * setRoleCreateAccess(role: Role, allowed: boolean)
  197. * getUpdateRequiresAuthentication()
  198. * setUpdateRequiresAuthentication(allowed: boolean)
  199. * getUpdatePointerFields()
  200. * setUpdatePointerFields(pointerFields: string[])
  201. * getUpdateAccess(entity: Entity)
  202. * setUpdateAccess(entity: Entity, allowed: boolean)
  203. * getPublicUpdateAccess()
  204. * setPublicUpdateAccess(allowed: boolean)
  205. * getRoleUpdateAccess(role: Role)
  206. * setRoleUpdateAccess(role: Role, allowed: boolean)
  207. * getDeleteRequiresAuthentication()
  208. * setDeleteRequiresAuthentication(allowed: boolean)
  209. * getDeletePointerFields()
  210. * setDeletePointerFields(pointerFields: string[])
  211. * getDeleteAccess(entity: Entity)
  212. * setDeleteAccess(entity: Entity, allowed: boolean)
  213. * getPublicDeleteAccess()
  214. * setPublicDeleteAccess(allowed: boolean)
  215. * getRoleDeleteAccess(role: Role)
  216. * setRoleDeleteAccess(role: Role, allowed: boolean)
  217. * getAddFieldRequiresAuthentication()
  218. * setAddFieldRequiresAuthentication(allowed: boolean)
  219. * getAddFieldPointerFields()
  220. * setAddFieldPointerFields(pointerFields: string[])
  221. * getAddFieldAccess(entity: Entity)
  222. * setAddFieldAccess(entity: Entity, allowed: boolean)
  223. * getPublicAddFieldAccess()
  224. * setPublicAddFieldAccess(allowed: boolean)
  225. * getRoleAddFieldAccess(role: Role)
  226. * setRoleAddFieldAccess(role: Role, allowed: boolean)
  227. * </p>
  228. *
  229. * @alias Parse.CLP
  230. */
  231. var ParseCLP = /*#__PURE__*/function () {
  232. /**
  233. * @param {(Parse.User | Parse.Role | object)} userId The user to initialize the CLP for
  234. */
  235. function ParseCLP(userId
  236. /*: ParseUser | ParseRole | PermissionsMap*/
  237. ) {
  238. var _this = this;
  239. (0, _classCallCheck2.default)(this, ParseCLP);
  240. (0, _defineProperty2.default)(this, "permissionsMap", void 0);
  241. this.permissionsMap = {}; // Initialize permissions Map with default permissions
  242. var _iterator = _createForOfIteratorHelper((0, _entries.default)(VALID_PERMISSIONS).call(VALID_PERMISSIONS)),
  243. _step;
  244. try {
  245. var _loop = function _loop() {
  246. var _step$value = (0, _slicedToArray2.default)(_step.value, 2),
  247. operation = _step$value[0],
  248. group = _step$value[1];
  249. _this.permissionsMap[operation] = (0, _assign.default)({}, group);
  250. var action = operation.charAt(0).toUpperCase() + (0, _slice.default)(operation).call(operation, 1);
  251. _this["get".concat(action, "RequiresAuthentication")] = function () {
  252. return this._getAccess(operation, 'requiresAuthentication');
  253. };
  254. _this["set".concat(action, "RequiresAuthentication")] = function (allowed) {
  255. this._setAccess(operation, 'requiresAuthentication', allowed);
  256. };
  257. _this["get".concat(action, "PointerFields")] = function () {
  258. return this._getAccess(operation, 'pointerFields', false);
  259. };
  260. _this["set".concat(action, "PointerFields")] = function (pointerFields) {
  261. this._setArrayAccess(operation, 'pointerFields', pointerFields);
  262. };
  263. _this["get".concat(action, "Access")] = function (entity) {
  264. return this._getAccess(operation, entity);
  265. };
  266. _this["set".concat(action, "Access")] = function (entity, allowed) {
  267. this._setAccess(operation, entity, allowed);
  268. };
  269. _this["getPublic".concat(action, "Access")] = function () {
  270. return this["get".concat(action, "Access")](PUBLIC_KEY);
  271. };
  272. _this["setPublic".concat(action, "Access")] = function (allowed) {
  273. this["set".concat(action, "Access")](PUBLIC_KEY, allowed);
  274. };
  275. _this["getRole".concat(action, "Access")] = function (role) {
  276. return this["get".concat(action, "Access")](this._getRoleName(role));
  277. };
  278. _this["setRole".concat(action, "Access")] = function (role, allowed) {
  279. this["set".concat(action, "Access")](this._getRoleName(role), allowed);
  280. };
  281. };
  282. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  283. _loop();
  284. } // Initialize permissions Map with default extended permissions
  285. } catch (err) {
  286. _iterator.e(err);
  287. } finally {
  288. _iterator.f();
  289. }
  290. var _iterator2 = _createForOfIteratorHelper((0, _entries.default)(VALID_PERMISSIONS_EXTENDED).call(VALID_PERMISSIONS_EXTENDED)),
  291. _step2;
  292. try {
  293. for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
  294. var _step2$value = (0, _slicedToArray2.default)(_step2.value, 2),
  295. operation = _step2$value[0],
  296. group = _step2$value[1];
  297. this.permissionsMap[operation] = (0, _assign.default)({}, group);
  298. }
  299. } catch (err) {
  300. _iterator2.e(err);
  301. } finally {
  302. _iterator2.f();
  303. }
  304. if (userId && (0, _typeof2.default)(userId) === 'object') {
  305. if (userId instanceof _ParseUser.default) {
  306. this.setReadAccess(userId, true);
  307. this.setWriteAccess(userId, true);
  308. } else if (userId instanceof _ParseRole.default) {
  309. this.setRoleReadAccess(userId, true);
  310. this.setRoleWriteAccess(userId, true);
  311. } else {
  312. for (var _permission in userId) {
  313. var _context;
  314. var users = userId[_permission];
  315. var isValidPermission = !!VALID_PERMISSIONS.get(_permission);
  316. var isValidPermissionExtended = !!VALID_PERMISSIONS_EXTENDED.get(_permission);
  317. var isValidGroupPermission = (0, _includes.default)(_context = ['readUserFields', 'writeUserFields']).call(_context, _permission);
  318. if (typeof _permission !== 'string' || !(isValidPermission || isValidPermissionExtended || isValidGroupPermission)) {
  319. throw new TypeError('Tried to create an CLP with an invalid permission type.');
  320. }
  321. if (isValidGroupPermission) {
  322. if ((0, _every.default)(users).call(users, function (pointer) {
  323. return typeof pointer === 'string';
  324. })) {
  325. this.permissionsMap[_permission] = users;
  326. continue;
  327. } else {
  328. throw new TypeError('Tried to create an CLP with an invalid permission value.');
  329. }
  330. }
  331. for (var user in users) {
  332. var allowed = users[user];
  333. if (typeof allowed !== 'boolean' && !isValidPermissionExtended && user !== 'pointerFields') {
  334. throw new TypeError('Tried to create an CLP with an invalid permission value.');
  335. }
  336. this.permissionsMap[_permission][user] = allowed;
  337. }
  338. }
  339. }
  340. } else if (typeof userId === 'function') {
  341. throw new TypeError('ParseCLP constructed with a function. Did you forget ()?');
  342. }
  343. }
  344. /**
  345. * Returns a JSON-encoded version of the CLP.
  346. *
  347. * @returns {object}
  348. */
  349. (0, _createClass2.default)(ParseCLP, [{
  350. key: "toJSON",
  351. value: function toJSON()
  352. /*: PermissionsMap*/
  353. {
  354. return _objectSpread({}, this.permissionsMap);
  355. }
  356. /**
  357. * Returns whether this CLP is equal to another object
  358. *
  359. * @param other The other object to compare to
  360. * @returns {boolean}
  361. */
  362. }, {
  363. key: "equals",
  364. value: function equals(other
  365. /*: ParseCLP*/
  366. )
  367. /*: boolean*/
  368. {
  369. if (!(other instanceof ParseCLP)) {
  370. return false;
  371. }
  372. var permissions = (0, _keys.default)(this.permissionsMap);
  373. var otherPermissions = (0, _keys.default)(other.permissionsMap);
  374. if (permissions.length !== otherPermissions.length) {
  375. return false;
  376. }
  377. for (var _permission2 in this.permissionsMap) {
  378. if (!other.permissionsMap[_permission2]) {
  379. return false;
  380. }
  381. var users = (0, _keys.default)(this.permissionsMap[_permission2]);
  382. var otherUsers = (0, _keys.default)(other.permissionsMap[_permission2]);
  383. if (users.length !== otherUsers.length) {
  384. return false;
  385. }
  386. for (var user in this.permissionsMap[_permission2]) {
  387. if (!other.permissionsMap[_permission2][user]) {
  388. return false;
  389. }
  390. if (this.permissionsMap[_permission2][user] !== other.permissionsMap[_permission2][user]) {
  391. return false;
  392. }
  393. }
  394. }
  395. return true;
  396. }
  397. }, {
  398. key: "_getRoleName",
  399. value: function _getRoleName(role
  400. /*: ParseRole | string*/
  401. )
  402. /*: string*/
  403. {
  404. var name = role;
  405. if (role instanceof _ParseRole.default) {
  406. // Normalize to the String name
  407. name = role.getName();
  408. }
  409. if (typeof name !== 'string') {
  410. throw new TypeError('role must be a Parse.Role or a String');
  411. }
  412. return "role:".concat(name);
  413. }
  414. }, {
  415. key: "_parseEntity",
  416. value: function _parseEntity(entity
  417. /*: Entity*/
  418. ) {
  419. var userId = entity;
  420. if (userId instanceof _ParseUser.default) {
  421. userId = userId.id;
  422. if (!userId) {
  423. throw new Error('Cannot get access for a Parse.User without an id.');
  424. }
  425. } else if (userId instanceof _ParseRole.default) {
  426. userId = this._getRoleName(userId);
  427. }
  428. if (typeof userId !== 'string') {
  429. throw new TypeError('userId must be a string.');
  430. }
  431. return userId;
  432. }
  433. }, {
  434. key: "_setAccess",
  435. value: function _setAccess(permission
  436. /*: string*/
  437. , userId
  438. /*: Entity*/
  439. , allowed
  440. /*: boolean*/
  441. ) {
  442. userId = this._parseEntity(userId);
  443. if (typeof allowed !== 'boolean') {
  444. throw new TypeError('allowed must be either true or false.');
  445. }
  446. var permissions = this.permissionsMap[permission][userId];
  447. if (!permissions) {
  448. if (!allowed) {
  449. // The user already doesn't have this permission, so no action is needed
  450. return;
  451. } else {
  452. this.permissionsMap[permission][userId] = {};
  453. }
  454. }
  455. if (allowed) {
  456. this.permissionsMap[permission][userId] = true;
  457. } else {
  458. delete this.permissionsMap[permission][userId];
  459. }
  460. }
  461. }, {
  462. key: "_getAccess",
  463. value: function _getAccess(permission
  464. /*: string*/
  465. , userId
  466. /*: Entity*/
  467. )
  468. /*: boolean | string[]*/
  469. {
  470. var returnBoolean = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  471. userId = this._parseEntity(userId);
  472. var permissions = this.permissionsMap[permission][userId];
  473. if (returnBoolean) {
  474. if (!permissions) {
  475. return false;
  476. }
  477. return !!this.permissionsMap[permission][userId];
  478. }
  479. return permissions;
  480. }
  481. }, {
  482. key: "_setArrayAccess",
  483. value: function _setArrayAccess(permission
  484. /*: string*/
  485. , userId
  486. /*: Entity*/
  487. , fields
  488. /*: string*/
  489. ) {
  490. userId = this._parseEntity(userId);
  491. var permissions = this.permissionsMap[permission][userId];
  492. if (!permissions) {
  493. this.permissionsMap[permission][userId] = [];
  494. }
  495. if (!fields || (0, _isArray.default)(fields) && fields.length === 0) {
  496. delete this.permissionsMap[permission][userId];
  497. } else if ((0, _isArray.default)(fields) && (0, _every.default)(fields).call(fields, function (field) {
  498. return typeof field === 'string';
  499. })) {
  500. this.permissionsMap[permission][userId] = fields;
  501. } else {
  502. throw new TypeError('fields must be an array of strings or undefined.');
  503. }
  504. }
  505. }, {
  506. key: "_setGroupPointerPermission",
  507. value: function _setGroupPointerPermission(operation
  508. /*: string*/
  509. , pointerFields
  510. /*: string[]*/
  511. ) {
  512. var fields = this.permissionsMap[operation];
  513. if (!fields) {
  514. this.permissionsMap[operation] = [];
  515. }
  516. if (!pointerFields || (0, _isArray.default)(pointerFields) && pointerFields.length === 0) {
  517. delete this.permissionsMap[operation];
  518. } else if ((0, _isArray.default)(pointerFields) && (0, _every.default)(pointerFields).call(pointerFields, function (field) {
  519. return typeof field === 'string';
  520. })) {
  521. this.permissionsMap[operation] = pointerFields;
  522. } else {
  523. throw new TypeError("".concat(operation, ".pointerFields must be an array of strings or undefined."));
  524. }
  525. }
  526. }, {
  527. key: "_getGroupPointerPermissions",
  528. value: function _getGroupPointerPermissions(operation
  529. /*: string*/
  530. )
  531. /*: string[]*/
  532. {
  533. return this.permissionsMap[operation];
  534. }
  535. /**
  536. * Sets user pointer fields to allow permission for get/count/find operations.
  537. *
  538. * @param {string[]} pointerFields User pointer fields
  539. */
  540. }, {
  541. key: "setReadUserFields",
  542. value: function setReadUserFields(pointerFields
  543. /*: string[]*/
  544. ) {
  545. this._setGroupPointerPermission('readUserFields', pointerFields);
  546. }
  547. /**
  548. * @returns {string[]} User pointer fields
  549. */
  550. }, {
  551. key: "getReadUserFields",
  552. value: function getReadUserFields()
  553. /*: string[]*/
  554. {
  555. return this._getGroupPointerPermissions('readUserFields');
  556. }
  557. /**
  558. * Sets user pointer fields to allow permission for create/delete/update/addField operations
  559. *
  560. * @param {string[]} pointerFields User pointer fields
  561. */
  562. }, {
  563. key: "setWriteUserFields",
  564. value: function setWriteUserFields(pointerFields
  565. /*: string[]*/
  566. ) {
  567. this._setGroupPointerPermission('writeUserFields', pointerFields);
  568. }
  569. /**
  570. * @returns {string[]} User pointer fields
  571. */
  572. }, {
  573. key: "getWriteUserFields",
  574. value: function getWriteUserFields()
  575. /*: string[]*/
  576. {
  577. return this._getGroupPointerPermissions('writeUserFields');
  578. }
  579. /**
  580. * Sets whether the given user is allowed to retrieve fields from this class.
  581. *
  582. * @param userId An instance of Parse.User or its objectId.
  583. * @param {string[]} fields fields to be protected
  584. */
  585. }, {
  586. key: "setProtectedFields",
  587. value: function setProtectedFields(userId
  588. /*: Entity*/
  589. , fields
  590. /*: string[]*/
  591. ) {
  592. this._setArrayAccess('protectedFields', userId, fields);
  593. }
  594. /**
  595. * Returns array of fields are accessable to this user.
  596. *
  597. * @param userId An instance of Parse.User or its objectId, or a Parse.Role.
  598. * @returns {string[]}
  599. */
  600. }, {
  601. key: "getProtectedFields",
  602. value: function getProtectedFields(userId
  603. /*: Entity*/
  604. )
  605. /*: string[]*/
  606. {
  607. return this._getAccess('protectedFields', userId, false);
  608. }
  609. /**
  610. * Sets whether the given user is allowed to read from this class.
  611. *
  612. * @param userId An instance of Parse.User or its objectId.
  613. * @param {boolean} allowed whether that user should have read access.
  614. */
  615. }, {
  616. key: "setReadAccess",
  617. value: function setReadAccess(userId
  618. /*: Entity*/
  619. , allowed
  620. /*: boolean*/
  621. ) {
  622. this._setAccess('find', userId, allowed);
  623. this._setAccess('get', userId, allowed);
  624. this._setAccess('count', userId, allowed);
  625. }
  626. /**
  627. * Get whether the given user id is *explicitly* allowed to read from this class.
  628. * Even if this returns false, the user may still be able to access it if
  629. * getPublicReadAccess returns true or a role that the user belongs to has
  630. * write access.
  631. *
  632. * @param userId An instance of Parse.User or its objectId, or a Parse.Role.
  633. * @returns {boolean}
  634. */
  635. }, {
  636. key: "getReadAccess",
  637. value: function getReadAccess(userId
  638. /*: Entity*/
  639. )
  640. /*: boolean*/
  641. {
  642. return this._getAccess('find', userId) && this._getAccess('get', userId) && this._getAccess('count', userId);
  643. }
  644. /**
  645. * Sets whether the given user id is allowed to write to this class.
  646. *
  647. * @param userId An instance of Parse.User or its objectId, or a Parse.Role..
  648. * @param {boolean} allowed Whether that user should have write access.
  649. */
  650. }, {
  651. key: "setWriteAccess",
  652. value: function setWriteAccess(userId
  653. /*: Entity*/
  654. , allowed
  655. /*: boolean*/
  656. ) {
  657. this._setAccess('create', userId, allowed);
  658. this._setAccess('update', userId, allowed);
  659. this._setAccess('delete', userId, allowed);
  660. this._setAccess('addField', userId, allowed);
  661. }
  662. /**
  663. * Gets whether the given user id is *explicitly* allowed to write to this class.
  664. * Even if this returns false, the user may still be able to write it if
  665. * getPublicWriteAccess returns true or a role that the user belongs to has
  666. * write access.
  667. *
  668. * @param userId An instance of Parse.User or its objectId, or a Parse.Role.
  669. * @returns {boolean}
  670. */
  671. }, {
  672. key: "getWriteAccess",
  673. value: function getWriteAccess(userId
  674. /*: Entity*/
  675. )
  676. /*: boolean*/
  677. {
  678. return this._getAccess('create', userId) && this._getAccess('update', userId) && this._getAccess('delete', userId) && this._getAccess('addField', userId);
  679. }
  680. /**
  681. * Sets whether the public is allowed to read from this class.
  682. *
  683. * @param {boolean} allowed
  684. */
  685. }, {
  686. key: "setPublicReadAccess",
  687. value: function setPublicReadAccess(allowed
  688. /*: boolean*/
  689. ) {
  690. this.setReadAccess(PUBLIC_KEY, allowed);
  691. }
  692. /**
  693. * Gets whether the public is allowed to read from this class.
  694. *
  695. * @returns {boolean}
  696. */
  697. }, {
  698. key: "getPublicReadAccess",
  699. value: function getPublicReadAccess()
  700. /*: boolean*/
  701. {
  702. return this.getReadAccess(PUBLIC_KEY);
  703. }
  704. /**
  705. * Sets whether the public is allowed to write to this class.
  706. *
  707. * @param {boolean} allowed
  708. */
  709. }, {
  710. key: "setPublicWriteAccess",
  711. value: function setPublicWriteAccess(allowed
  712. /*: boolean*/
  713. ) {
  714. this.setWriteAccess(PUBLIC_KEY, allowed);
  715. }
  716. /**
  717. * Gets whether the public is allowed to write to this class.
  718. *
  719. * @returns {boolean}
  720. */
  721. }, {
  722. key: "getPublicWriteAccess",
  723. value: function getPublicWriteAccess()
  724. /*: boolean*/
  725. {
  726. return this.getWriteAccess(PUBLIC_KEY);
  727. }
  728. /**
  729. * Sets whether the public is allowed to protect fields in this class.
  730. *
  731. * @param {string[]} fields
  732. */
  733. }, {
  734. key: "setPublicProtectedFields",
  735. value: function setPublicProtectedFields(fields
  736. /*: string[]*/
  737. ) {
  738. this.setProtectedFields(PUBLIC_KEY, fields);
  739. }
  740. /**
  741. * Gets whether the public is allowed to read fields from this class.
  742. *
  743. * @returns {string[]}
  744. */
  745. }, {
  746. key: "getPublicProtectedFields",
  747. value: function getPublicProtectedFields()
  748. /*: string[]*/
  749. {
  750. return this.getProtectedFields(PUBLIC_KEY);
  751. }
  752. /**
  753. * Gets whether users belonging to the given role are allowed
  754. * to read from this class. Even if this returns false, the role may
  755. * still be able to write it if a parent role has read access.
  756. *
  757. * @param role The name of the role, or a Parse.Role object.
  758. * @returns {boolean} true if the role has read access. false otherwise.
  759. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  760. */
  761. }, {
  762. key: "getRoleReadAccess",
  763. value: function getRoleReadAccess(role
  764. /*: ParseRole | string*/
  765. )
  766. /*: boolean*/
  767. {
  768. return this.getReadAccess(this._getRoleName(role));
  769. }
  770. /**
  771. * Gets whether users belonging to the given role are allowed
  772. * to write to this user. Even if this returns false, the role may
  773. * still be able to write it if a parent role has write access.
  774. *
  775. * @param role The name of the role, or a Parse.Role object.
  776. * @returns {boolean} true if the role has write access. false otherwise.
  777. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  778. */
  779. }, {
  780. key: "getRoleWriteAccess",
  781. value: function getRoleWriteAccess(role
  782. /*: ParseRole | string*/
  783. )
  784. /*: boolean*/
  785. {
  786. return this.getWriteAccess(this._getRoleName(role));
  787. }
  788. /**
  789. * Sets whether users belonging to the given role are allowed
  790. * to read from this class.
  791. *
  792. * @param role The name of the role, or a Parse.Role object.
  793. * @param {boolean} allowed Whether the given role can read this object.
  794. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  795. */
  796. }, {
  797. key: "setRoleReadAccess",
  798. value: function setRoleReadAccess(role
  799. /*: ParseRole | string*/
  800. , allowed
  801. /*: boolean*/
  802. ) {
  803. this.setReadAccess(this._getRoleName(role), allowed);
  804. }
  805. /**
  806. * Sets whether users belonging to the given role are allowed
  807. * to write to this class.
  808. *
  809. * @param role The name of the role, or a Parse.Role object.
  810. * @param {boolean} allowed Whether the given role can write this object.
  811. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  812. */
  813. }, {
  814. key: "setRoleWriteAccess",
  815. value: function setRoleWriteAccess(role
  816. /*: ParseRole | string*/
  817. , allowed
  818. /*: boolean*/
  819. ) {
  820. this.setWriteAccess(this._getRoleName(role), allowed);
  821. }
  822. /**
  823. * Gets whether users belonging to the given role are allowed
  824. * to count to this user. Even if this returns false, the role may
  825. * still be able to count it if a parent role has count access.
  826. *
  827. * @param role The name of the role, or a Parse.Role object.
  828. * @returns {string[]}
  829. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  830. */
  831. }, {
  832. key: "getRoleProtectedFields",
  833. value: function getRoleProtectedFields(role
  834. /*: ParseRole | string*/
  835. )
  836. /*: string[]*/
  837. {
  838. return this.getProtectedFields(this._getRoleName(role));
  839. }
  840. /**
  841. * Sets whether users belonging to the given role are allowed
  842. * to set access field in this class.
  843. *
  844. * @param role The name of the role, or a Parse.Role object.
  845. * @param {string[]} fields Fields to be protected by Role.
  846. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  847. */
  848. }, {
  849. key: "setRoleProtectedFields",
  850. value: function setRoleProtectedFields(role
  851. /*: ParseRole | string*/
  852. , fields
  853. /*: string[]*/
  854. ) {
  855. this.setProtectedFields(this._getRoleName(role), fields);
  856. }
  857. }]);
  858. return ParseCLP;
  859. }();
  860. var _default = ParseCLP;
  861. exports.default = _default;