ParseCLP.js 30 KB

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