ParseCLP.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _ParseRole = _interopRequireDefault(require("./ParseRole"));
  7. var _ParseUser = _interopRequireDefault(require("./ParseUser"));
  8. function _interopRequireDefault(e) {
  9. return e && e.__esModule ? e : {
  10. default: e
  11. };
  12. }
  13. const PUBLIC_KEY = '*';
  14. const VALID_PERMISSIONS = new Map();
  15. VALID_PERMISSIONS.set('get', {});
  16. VALID_PERMISSIONS.set('find', {});
  17. VALID_PERMISSIONS.set('count', {});
  18. VALID_PERMISSIONS.set('create', {});
  19. VALID_PERMISSIONS.set('update', {});
  20. VALID_PERMISSIONS.set('delete', {});
  21. VALID_PERMISSIONS.set('addField', {});
  22. const VALID_PERMISSIONS_EXTENDED = new Map();
  23. VALID_PERMISSIONS_EXTENDED.set('protectedFields', {});
  24. /**
  25. * Creates a new CLP.
  26. * If no argument is given, the CLP has no permissions for anyone.
  27. * If the argument is a Parse.User or Parse.Role, the CLP will have read and write
  28. * permission for only that user or role.
  29. * If the argument is any other JSON object, that object will be interpretted
  30. * as a serialized CLP created with toJSON().
  31. *
  32. * <p>A CLP, or Class Level Permissions can be added to any
  33. * <code>Parse.Schema</code> to restrict access to only a subset of users
  34. * of your application.</p>
  35. *
  36. * <p>
  37. * For get/count/find/create/update/delete/addField using the following functions:
  38. *
  39. * Entity is type Parse.User or Parse.Role or string
  40. * Role is type Parse.Role or Name of Parse.Role
  41. *
  42. * getGetRequiresAuthentication()
  43. * setGetRequiresAuthentication(allowed: boolean)
  44. * getGetPointerFields()
  45. * setGetPointerFields(pointerFields: string[])
  46. * getGetAccess(entity: Entity)
  47. * setGetAccess(entity: Entity, allowed: boolean)
  48. * getPublicGetAccess()
  49. * setPublicGetAccess(allowed: boolean)
  50. * getRoleGetAccess(role: Role)
  51. * setRoleGetAccess(role: Role, allowed: boolean)
  52. * getFindRequiresAuthentication()
  53. * setFindRequiresAuthentication(allowed: boolean)
  54. * getFindPointerFields()
  55. * setFindPointerFields(pointerFields: string[])
  56. * getFindAccess(entity: Entity)
  57. * setFindAccess(entity: Entity, allowed: boolean)
  58. * getPublicFindAccess()
  59. * setPublicFindAccess(allowed: boolean)
  60. * getRoleFindAccess(role: Role)
  61. * setRoleFindAccess(role: Role, allowed: boolean)
  62. * getCountRequiresAuthentication()
  63. * setCountRequiresAuthentication(allowed: boolean)
  64. * getCountPointerFields()
  65. * setCountPointerFields(pointerFields: string[])
  66. * getCountAccess(entity: Entity)
  67. * setCountAccess(entity: Entity, allowed: boolean)
  68. * getPublicCountAccess()
  69. * setPublicCountAccess(allowed: boolean)
  70. * getRoleCountAccess(role: Role)
  71. * setRoleCountAccess(role: Role, allowed: boolean)
  72. * getCreateRequiresAuthentication()
  73. * setCreateRequiresAuthentication(allowed: boolean)
  74. * getCreatePointerFields()
  75. * setCreatePointerFields(pointerFields: string[])
  76. * getCreateAccess(entity: Entity)
  77. * setCreateAccess(entity: Entity, allowed: boolean)
  78. * getPublicCreateAccess()
  79. * setPublicCreateAccess(allowed: Boolean)
  80. * getRoleCreateAccess(role: Role)
  81. * setRoleCreateAccess(role: Role, allowed: boolean)
  82. * getUpdateRequiresAuthentication()
  83. * setUpdateRequiresAuthentication(allowed: boolean)
  84. * getUpdatePointerFields()
  85. * setUpdatePointerFields(pointerFields: string[])
  86. * getUpdateAccess(entity: Entity)
  87. * setUpdateAccess(entity: Entity, allowed: boolean)
  88. * getPublicUpdateAccess()
  89. * setPublicUpdateAccess(allowed: boolean)
  90. * getRoleUpdateAccess(role: Role)
  91. * setRoleUpdateAccess(role: Role, allowed: boolean)
  92. * getDeleteRequiresAuthentication()
  93. * setDeleteRequiresAuthentication(allowed: boolean)
  94. * getDeletePointerFields()
  95. * setDeletePointerFields(pointerFields: string[])
  96. * getDeleteAccess(entity: Entity)
  97. * setDeleteAccess(entity: Entity, allowed: boolean)
  98. * getPublicDeleteAccess()
  99. * setPublicDeleteAccess(allowed: boolean)
  100. * getRoleDeleteAccess(role: Role)
  101. * setRoleDeleteAccess(role: Role, allowed: boolean)
  102. * getAddFieldRequiresAuthentication()
  103. * setAddFieldRequiresAuthentication(allowed: boolean)
  104. * getAddFieldPointerFields()
  105. * setAddFieldPointerFields(pointerFields: string[])
  106. * getAddFieldAccess(entity: Entity)
  107. * setAddFieldAccess(entity: Entity, allowed: boolean)
  108. * getPublicAddFieldAccess()
  109. * setPublicAddFieldAccess(allowed: boolean)
  110. * getRoleAddFieldAccess(role: Role)
  111. * setRoleAddFieldAccess(role: Role, allowed: boolean)
  112. * </p>
  113. *
  114. * @alias Parse.CLP
  115. */
  116. class ParseCLP {
  117. /**
  118. * @param {(Parse.User | Parse.Role | object)} userId The user to initialize the CLP for
  119. */
  120. constructor(userId) {
  121. this.permissionsMap = {};
  122. // Initialize permissions Map with default permissions
  123. for (const [operation, group] of VALID_PERMISSIONS.entries()) {
  124. this.permissionsMap[operation] = Object.assign({}, group);
  125. const action = operation.charAt(0).toUpperCase() + operation.slice(1);
  126. this[`get${action}RequiresAuthentication`] = function () {
  127. return this._getAccess(operation, 'requiresAuthentication');
  128. };
  129. this[`set${action}RequiresAuthentication`] = function (allowed) {
  130. this._setAccess(operation, 'requiresAuthentication', allowed);
  131. };
  132. this[`get${action}PointerFields`] = function () {
  133. return this._getAccess(operation, 'pointerFields', false);
  134. };
  135. this[`set${action}PointerFields`] = function (pointerFields) {
  136. this._setArrayAccess(operation, 'pointerFields', pointerFields);
  137. };
  138. this[`get${action}Access`] = function (entity) {
  139. return this._getAccess(operation, entity);
  140. };
  141. this[`set${action}Access`] = function (entity, allowed) {
  142. this._setAccess(operation, entity, allowed);
  143. };
  144. this[`getPublic${action}Access`] = function () {
  145. return this[`get${action}Access`](PUBLIC_KEY);
  146. };
  147. this[`setPublic${action}Access`] = function (allowed) {
  148. this[`set${action}Access`](PUBLIC_KEY, allowed);
  149. };
  150. this[`getRole${action}Access`] = function (role) {
  151. return this[`get${action}Access`](this._getRoleName(role));
  152. };
  153. this[`setRole${action}Access`] = function (role, allowed) {
  154. this[`set${action}Access`](this._getRoleName(role), allowed);
  155. };
  156. }
  157. // Initialize permissions Map with default extended permissions
  158. for (const [operation, group] of VALID_PERMISSIONS_EXTENDED.entries()) {
  159. this.permissionsMap[operation] = Object.assign({}, group);
  160. }
  161. if (userId && typeof userId === 'object') {
  162. if (userId instanceof _ParseUser.default) {
  163. this.setReadAccess(userId, true);
  164. this.setWriteAccess(userId, true);
  165. } else if (userId instanceof _ParseRole.default) {
  166. this.setRoleReadAccess(userId, true);
  167. this.setRoleWriteAccess(userId, true);
  168. } else {
  169. for (const permission in userId) {
  170. const users = userId[permission];
  171. const isValidPermission = !!VALID_PERMISSIONS.get(permission);
  172. const isValidPermissionExtended = !!VALID_PERMISSIONS_EXTENDED.get(permission);
  173. const isValidGroupPermission = ['readUserFields', 'writeUserFields'].includes(permission);
  174. if (typeof permission !== 'string' || !(isValidPermission || isValidPermissionExtended || isValidGroupPermission)) {
  175. throw new TypeError('Tried to create an CLP with an invalid permission type.');
  176. }
  177. if (isValidGroupPermission) {
  178. if (users.every(pointer => typeof pointer === 'string')) {
  179. this.permissionsMap[permission] = users;
  180. continue;
  181. } else {
  182. throw new TypeError('Tried to create an CLP with an invalid permission value.');
  183. }
  184. }
  185. for (const user in users) {
  186. const allowed = users[user];
  187. if (typeof allowed !== 'boolean' && !isValidPermissionExtended && user !== 'pointerFields') {
  188. throw new TypeError('Tried to create an CLP with an invalid permission value.');
  189. }
  190. this.permissionsMap[permission][user] = allowed;
  191. }
  192. }
  193. }
  194. } else if (typeof userId === 'function') {
  195. throw new TypeError('ParseCLP constructed with a function. Did you forget ()?');
  196. }
  197. }
  198. /**
  199. * Returns a JSON-encoded version of the CLP.
  200. *
  201. * @returns {object}
  202. */
  203. toJSON() {
  204. return {
  205. ...this.permissionsMap
  206. };
  207. }
  208. /**
  209. * Returns whether this CLP is equal to another object
  210. *
  211. * @param other The other object to compare to
  212. * @returns {boolean}
  213. */
  214. equals(other) {
  215. if (!(other instanceof ParseCLP)) {
  216. return false;
  217. }
  218. const permissions = Object.keys(this.permissionsMap);
  219. const otherPermissions = Object.keys(other.permissionsMap);
  220. if (permissions.length !== otherPermissions.length) {
  221. return false;
  222. }
  223. for (const permission in this.permissionsMap) {
  224. if (!other.permissionsMap[permission]) {
  225. return false;
  226. }
  227. const users = Object.keys(this.permissionsMap[permission]);
  228. const otherUsers = Object.keys(other.permissionsMap[permission]);
  229. if (users.length !== otherUsers.length) {
  230. return false;
  231. }
  232. for (const user in this.permissionsMap[permission]) {
  233. if (!other.permissionsMap[permission][user]) {
  234. return false;
  235. }
  236. if (this.permissionsMap[permission][user] !== other.permissionsMap[permission][user]) {
  237. return false;
  238. }
  239. }
  240. }
  241. return true;
  242. }
  243. _getRoleName(role) {
  244. let name = role;
  245. if (role instanceof _ParseRole.default) {
  246. // Normalize to the String name
  247. name = role.getName();
  248. }
  249. if (typeof name !== 'string') {
  250. throw new TypeError('role must be a Parse.Role or a String');
  251. }
  252. return `role:${name}`;
  253. }
  254. _parseEntity(entity) {
  255. let userId = entity;
  256. if (userId instanceof _ParseUser.default) {
  257. userId = userId.id;
  258. if (!userId) {
  259. throw new Error('Cannot get access for a Parse.User without an id.');
  260. }
  261. } else if (userId instanceof _ParseRole.default) {
  262. userId = this._getRoleName(userId);
  263. }
  264. if (typeof userId !== 'string') {
  265. throw new TypeError('userId must be a string.');
  266. }
  267. return userId;
  268. }
  269. _setAccess(permission, userId, allowed) {
  270. userId = this._parseEntity(userId);
  271. if (typeof allowed !== 'boolean') {
  272. throw new TypeError('allowed must be either true or false.');
  273. }
  274. const permissions = this.permissionsMap[permission][userId];
  275. if (!permissions) {
  276. if (!allowed) {
  277. // The user already doesn't have this permission, so no action is needed
  278. return;
  279. } else {
  280. this.permissionsMap[permission][userId] = {};
  281. }
  282. }
  283. if (allowed) {
  284. this.permissionsMap[permission][userId] = true;
  285. } else {
  286. delete this.permissionsMap[permission][userId];
  287. }
  288. }
  289. _getAccess(permission, userId, returnBoolean = true) {
  290. userId = this._parseEntity(userId);
  291. const permissions = this.permissionsMap[permission][userId];
  292. if (returnBoolean) {
  293. if (!permissions) {
  294. return false;
  295. }
  296. return !!this.permissionsMap[permission][userId];
  297. }
  298. return permissions;
  299. }
  300. _setArrayAccess(permission, userId, fields) {
  301. userId = this._parseEntity(userId);
  302. const permissions = this.permissionsMap[permission][userId];
  303. if (!permissions) {
  304. this.permissionsMap[permission][userId] = [];
  305. }
  306. if (!fields || Array.isArray(fields) && fields.length === 0) {
  307. delete this.permissionsMap[permission][userId];
  308. } else if (Array.isArray(fields) && fields.every(field => typeof field === 'string')) {
  309. this.permissionsMap[permission][userId] = fields;
  310. } else {
  311. throw new TypeError('fields must be an array of strings or undefined.');
  312. }
  313. }
  314. _setGroupPointerPermission(operation, pointerFields) {
  315. const fields = this.permissionsMap[operation];
  316. if (!fields) {
  317. this.permissionsMap[operation] = [];
  318. }
  319. if (!pointerFields || Array.isArray(pointerFields) && pointerFields.length === 0) {
  320. delete this.permissionsMap[operation];
  321. } else if (Array.isArray(pointerFields) && pointerFields.every(field => typeof field === 'string')) {
  322. this.permissionsMap[operation] = pointerFields;
  323. } else {
  324. throw new TypeError(`${operation}.pointerFields must be an array of strings or undefined.`);
  325. }
  326. }
  327. _getGroupPointerPermissions(operation) {
  328. return this.permissionsMap[operation] || [];
  329. }
  330. /**
  331. * Sets user pointer fields to allow permission for get/count/find operations.
  332. *
  333. * @param {string[]} pointerFields User pointer fields
  334. */
  335. setReadUserFields(pointerFields) {
  336. this._setGroupPointerPermission('readUserFields', pointerFields);
  337. }
  338. /**
  339. * @returns {string[]} User pointer fields
  340. */
  341. getReadUserFields() {
  342. return this._getGroupPointerPermissions('readUserFields') || [];
  343. }
  344. /**
  345. * Sets user pointer fields to allow permission for create/delete/update/addField operations
  346. *
  347. * @param {string[]} pointerFields User pointer fields
  348. */
  349. setWriteUserFields(pointerFields) {
  350. this._setGroupPointerPermission('writeUserFields', pointerFields);
  351. }
  352. /**
  353. * @returns {string[]} User pointer fields
  354. */
  355. getWriteUserFields() {
  356. return this._getGroupPointerPermissions('writeUserFields') || [];
  357. }
  358. /**
  359. * Sets whether the given user is allowed to retrieve fields from this class.
  360. *
  361. * @param userId An instance of Parse.User or its objectId.
  362. * @param {string[]} fields fields to be protected
  363. */
  364. setProtectedFields(userId, fields) {
  365. this._setArrayAccess('protectedFields', userId, fields);
  366. }
  367. /**
  368. * Returns array of fields are accessable to this user.
  369. *
  370. * @param userId An instance of Parse.User or its objectId, or a Parse.Role.
  371. * @returns {string[]}
  372. */
  373. getProtectedFields(userId) {
  374. return this._getAccess('protectedFields', userId, false);
  375. }
  376. /**
  377. * Sets whether the given user is allowed to read from this class.
  378. *
  379. * @param userId An instance of Parse.User or its objectId.
  380. * @param {boolean} allowed whether that user should have read access.
  381. */
  382. setReadAccess(userId, allowed) {
  383. this._setAccess('find', userId, allowed);
  384. this._setAccess('get', userId, allowed);
  385. this._setAccess('count', userId, allowed);
  386. }
  387. /**
  388. * Get whether the given user id is *explicitly* allowed to read from this class.
  389. * Even if this returns false, the user may still be able to access it if
  390. * getPublicReadAccess returns true or a role that the user belongs to has
  391. * write access.
  392. *
  393. * @param userId An instance of Parse.User or its objectId, or a Parse.Role.
  394. * @returns {boolean}
  395. */
  396. getReadAccess(userId) {
  397. return this._getAccess('find', userId) && this._getAccess('get', userId) && this._getAccess('count', userId);
  398. }
  399. /**
  400. * Sets whether the given user id is allowed to write to this class.
  401. *
  402. * @param userId An instance of Parse.User or its objectId, or a Parse.Role..
  403. * @param {boolean} allowed Whether that user should have write access.
  404. */
  405. setWriteAccess(userId, allowed) {
  406. this._setAccess('create', userId, allowed);
  407. this._setAccess('update', userId, allowed);
  408. this._setAccess('delete', userId, allowed);
  409. this._setAccess('addField', userId, allowed);
  410. }
  411. /**
  412. * Gets whether the given user id is *explicitly* allowed to write to this class.
  413. * Even if this returns false, the user may still be able to write it if
  414. * getPublicWriteAccess returns true or a role that the user belongs to has
  415. * write access.
  416. *
  417. * @param userId An instance of Parse.User or its objectId, or a Parse.Role.
  418. * @returns {boolean}
  419. */
  420. getWriteAccess(userId) {
  421. return this._getAccess('create', userId) && this._getAccess('update', userId) && this._getAccess('delete', userId) && this._getAccess('addField', userId);
  422. }
  423. /**
  424. * Sets whether the public is allowed to read from this class.
  425. *
  426. * @param {boolean} allowed
  427. */
  428. setPublicReadAccess(allowed) {
  429. this.setReadAccess(PUBLIC_KEY, allowed);
  430. }
  431. /**
  432. * Gets whether the public is allowed to read from this class.
  433. *
  434. * @returns {boolean}
  435. */
  436. getPublicReadAccess() {
  437. return this.getReadAccess(PUBLIC_KEY);
  438. }
  439. /**
  440. * Sets whether the public is allowed to write to this class.
  441. *
  442. * @param {boolean} allowed
  443. */
  444. setPublicWriteAccess(allowed) {
  445. this.setWriteAccess(PUBLIC_KEY, allowed);
  446. }
  447. /**
  448. * Gets whether the public is allowed to write to this class.
  449. *
  450. * @returns {boolean}
  451. */
  452. getPublicWriteAccess() {
  453. return this.getWriteAccess(PUBLIC_KEY);
  454. }
  455. /**
  456. * Sets whether the public is allowed to protect fields in this class.
  457. *
  458. * @param {string[]} fields
  459. */
  460. setPublicProtectedFields(fields) {
  461. this.setProtectedFields(PUBLIC_KEY, fields);
  462. }
  463. /**
  464. * Gets whether the public is allowed to read fields from this class.
  465. *
  466. * @returns {string[]}
  467. */
  468. getPublicProtectedFields() {
  469. return this.getProtectedFields(PUBLIC_KEY);
  470. }
  471. /**
  472. * Gets whether users belonging to the given role are allowed
  473. * to read from this class. Even if this returns false, the role may
  474. * still be able to write it if a parent role has read access.
  475. *
  476. * @param role The name of the role, or a Parse.Role object.
  477. * @returns {boolean} true if the role has read access. false otherwise.
  478. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  479. */
  480. getRoleReadAccess(role) {
  481. return this.getReadAccess(this._getRoleName(role));
  482. }
  483. /**
  484. * Gets whether users belonging to the given role are allowed
  485. * to write to this user. Even if this returns false, the role may
  486. * still be able to write it if a parent role has write access.
  487. *
  488. * @param role The name of the role, or a Parse.Role object.
  489. * @returns {boolean} true if the role has write access. false otherwise.
  490. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  491. */
  492. getRoleWriteAccess(role) {
  493. return this.getWriteAccess(this._getRoleName(role));
  494. }
  495. /**
  496. * Sets whether users belonging to the given role are allowed
  497. * to read from this class.
  498. *
  499. * @param role The name of the role, or a Parse.Role object.
  500. * @param {boolean} allowed Whether the given role can read this object.
  501. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  502. */
  503. setRoleReadAccess(role, allowed) {
  504. this.setReadAccess(this._getRoleName(role), allowed);
  505. }
  506. /**
  507. * Sets whether users belonging to the given role are allowed
  508. * to write to this class.
  509. *
  510. * @param role The name of the role, or a Parse.Role object.
  511. * @param {boolean} allowed Whether the given role can write this object.
  512. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  513. */
  514. setRoleWriteAccess(role, allowed) {
  515. this.setWriteAccess(this._getRoleName(role), allowed);
  516. }
  517. /**
  518. * Gets whether users belonging to the given role are allowed
  519. * to count to this user. Even if this returns false, the role may
  520. * still be able to count it if a parent role has count access.
  521. *
  522. * @param role The name of the role, or a Parse.Role object.
  523. * @returns {string[]}
  524. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  525. */
  526. getRoleProtectedFields(role) {
  527. return this.getProtectedFields(this._getRoleName(role));
  528. }
  529. /**
  530. * Sets whether users belonging to the given role are allowed
  531. * to set access field in this class.
  532. *
  533. * @param role The name of the role, or a Parse.Role object.
  534. * @param {string[]} fields Fields to be protected by Role.
  535. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  536. */
  537. setRoleProtectedFields(role, fields) {
  538. this.setProtectedFields(this._getRoleName(role), fields);
  539. }
  540. }
  541. var _default = exports.default = ParseCLP;