ParseCLP.js 30 KB

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