ParseCLP.js 22 KB

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