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. /**
  156. * @param {(Parse.User | Parse.Role | object)} userId The user to initialize the CLP for
  157. */
  158. constructor(userId
  159. /*: ParseUser | ParseRole | PermissionsMap*/
  160. ) {
  161. _defineProperty(this, "permissionsMap", void 0);
  162. this.permissionsMap = {}; // Initialize permissions Map with default permissions
  163. for (const [operation, group] of VALID_PERMISSIONS.entries()) {
  164. this.permissionsMap[operation] = _objectSpread({}, 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] = _objectSpread({}, 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. }
  335. this.permissionsMap[permission][userId] = {};
  336. }
  337. if (allowed) {
  338. this.permissionsMap[permission][userId] = true;
  339. } else {
  340. delete this.permissionsMap[permission][userId];
  341. }
  342. }
  343. _getAccess(permission
  344. /*: string*/
  345. , userId
  346. /*: Entity*/
  347. , returnBoolean = true)
  348. /*: boolean | string[]*/
  349. {
  350. userId = this._parseEntity(userId);
  351. const permissions = this.permissionsMap[permission][userId];
  352. if (returnBoolean) {
  353. if (!permissions) {
  354. return false;
  355. }
  356. return !!this.permissionsMap[permission][userId];
  357. }
  358. return permissions;
  359. }
  360. _setArrayAccess(permission
  361. /*: string*/
  362. , userId
  363. /*: Entity*/
  364. , fields
  365. /*: string*/
  366. ) {
  367. userId = this._parseEntity(userId);
  368. const permissions = this.permissionsMap[permission][userId];
  369. if (!permissions) {
  370. this.permissionsMap[permission][userId] = [];
  371. }
  372. if (!fields || Array.isArray(fields) && fields.length === 0) {
  373. delete this.permissionsMap[permission][userId];
  374. } else if (Array.isArray(fields) && fields.every(field => typeof field === 'string')) {
  375. this.permissionsMap[permission][userId] = fields;
  376. } else {
  377. throw new TypeError('fields must be an array of strings or undefined.');
  378. }
  379. }
  380. _setGroupPointerPermission(operation
  381. /*: string*/
  382. , pointerFields
  383. /*: string[]*/
  384. ) {
  385. const fields = this.permissionsMap[operation];
  386. if (!fields) {
  387. this.permissionsMap[operation] = [];
  388. }
  389. if (!pointerFields || Array.isArray(pointerFields) && pointerFields.length === 0) {
  390. delete this.permissionsMap[operation];
  391. } else if (Array.isArray(pointerFields) && pointerFields.every(field => typeof field === 'string')) {
  392. this.permissionsMap[operation] = pointerFields;
  393. } else {
  394. throw new TypeError(`${operation}.pointerFields must be an array of strings or undefined.`);
  395. }
  396. }
  397. _getGroupPointerPermissions(operation
  398. /*: string*/
  399. )
  400. /*: string[]*/
  401. {
  402. return this.permissionsMap[operation];
  403. }
  404. /**
  405. * Sets user pointer fields to allow permission for get/count/find operations.
  406. *
  407. * @param {string[]} pointerFields User pointer fields
  408. */
  409. setReadUserFields(pointerFields
  410. /*: string[]*/
  411. ) {
  412. this._setGroupPointerPermission('readUserFields', pointerFields);
  413. }
  414. /**
  415. * @returns {string[]} User pointer fields
  416. */
  417. getReadUserFields()
  418. /*: string[]*/
  419. {
  420. return this._getGroupPointerPermissions('readUserFields');
  421. }
  422. /**
  423. * Sets user pointer fields to allow permission for create/delete/update/addField operations
  424. *
  425. * @param {string[]} pointerFields User pointer fields
  426. */
  427. setWriteUserFields(pointerFields
  428. /*: string[]*/
  429. ) {
  430. this._setGroupPointerPermission('writeUserFields', pointerFields);
  431. }
  432. /**
  433. * @returns {string[]} User pointer fields
  434. */
  435. getWriteUserFields()
  436. /*: string[]*/
  437. {
  438. return this._getGroupPointerPermissions('writeUserFields');
  439. }
  440. /**
  441. * Sets whether the given user is allowed to retrieve fields from this class.
  442. *
  443. * @param userId An instance of Parse.User or its objectId.
  444. * @param {string[]} fields fields to be protected
  445. */
  446. setProtectedFields(userId
  447. /*: Entity*/
  448. , fields
  449. /*: string[]*/
  450. ) {
  451. this._setArrayAccess('protectedFields', userId, fields);
  452. }
  453. /**
  454. * Returns array of fields are accessable to this user.
  455. *
  456. * @param userId An instance of Parse.User or its objectId, or a Parse.Role.
  457. * @returns {string[]}
  458. */
  459. getProtectedFields(userId
  460. /*: Entity*/
  461. )
  462. /*: string[]*/
  463. {
  464. return this._getAccess('protectedFields', userId, false);
  465. }
  466. /**
  467. * Sets whether the given user is allowed to read from this class.
  468. *
  469. * @param userId An instance of Parse.User or its objectId.
  470. * @param {boolean} allowed whether that user should have read access.
  471. */
  472. setReadAccess(userId
  473. /*: Entity*/
  474. , allowed
  475. /*: boolean*/
  476. ) {
  477. this._setAccess('find', userId, allowed);
  478. this._setAccess('get', userId, allowed);
  479. this._setAccess('count', userId, allowed);
  480. }
  481. /**
  482. * Get whether the given user id is *explicitly* allowed to read from this class.
  483. * Even if this returns false, the user may still be able to access it if
  484. * getPublicReadAccess returns true or a role that the user belongs to has
  485. * write access.
  486. *
  487. * @param userId An instance of Parse.User or its objectId, or a Parse.Role.
  488. * @returns {boolean}
  489. */
  490. getReadAccess(userId
  491. /*: Entity*/
  492. )
  493. /*: boolean*/
  494. {
  495. return this._getAccess('find', userId) && this._getAccess('get', userId) && this._getAccess('count', userId);
  496. }
  497. /**
  498. * Sets whether the given user id is allowed to write to this class.
  499. *
  500. * @param userId An instance of Parse.User or its objectId, or a Parse.Role..
  501. * @param {boolean} allowed Whether that user should have write access.
  502. */
  503. setWriteAccess(userId
  504. /*: Entity*/
  505. , allowed
  506. /*: boolean*/
  507. ) {
  508. this._setAccess('create', userId, allowed);
  509. this._setAccess('update', userId, allowed);
  510. this._setAccess('delete', userId, allowed);
  511. this._setAccess('addField', userId, allowed);
  512. }
  513. /**
  514. * Gets whether the given user id is *explicitly* allowed to write to this class.
  515. * Even if this returns false, the user may still be able to write it if
  516. * getPublicWriteAccess returns true or a role that the user belongs to has
  517. * write access.
  518. *
  519. * @param userId An instance of Parse.User or its objectId, or a Parse.Role.
  520. * @returns {boolean}
  521. */
  522. getWriteAccess(userId
  523. /*: Entity*/
  524. )
  525. /*: boolean*/
  526. {
  527. return this._getAccess('create', userId) && this._getAccess('update', userId) && this._getAccess('delete', userId) && this._getAccess('addField', userId);
  528. }
  529. /**
  530. * Sets whether the public is allowed to read from this class.
  531. *
  532. * @param {boolean} allowed
  533. */
  534. setPublicReadAccess(allowed
  535. /*: boolean*/
  536. ) {
  537. this.setReadAccess(PUBLIC_KEY, allowed);
  538. }
  539. /**
  540. * Gets whether the public is allowed to read from this class.
  541. *
  542. * @returns {boolean}
  543. */
  544. getPublicReadAccess()
  545. /*: boolean*/
  546. {
  547. return this.getReadAccess(PUBLIC_KEY);
  548. }
  549. /**
  550. * Sets whether the public is allowed to write to this class.
  551. *
  552. * @param {boolean} allowed
  553. */
  554. setPublicWriteAccess(allowed
  555. /*: boolean*/
  556. ) {
  557. this.setWriteAccess(PUBLIC_KEY, allowed);
  558. }
  559. /**
  560. * Gets whether the public is allowed to write to this class.
  561. *
  562. * @returns {boolean}
  563. */
  564. getPublicWriteAccess()
  565. /*: boolean*/
  566. {
  567. return this.getWriteAccess(PUBLIC_KEY);
  568. }
  569. /**
  570. * Sets whether the public is allowed to protect fields in this class.
  571. *
  572. * @param {string[]} fields
  573. */
  574. setPublicProtectedFields(fields
  575. /*: string[]*/
  576. ) {
  577. this.setProtectedFields(PUBLIC_KEY, fields);
  578. }
  579. /**
  580. * Gets whether the public is allowed to read fields from this class.
  581. *
  582. * @returns {string[]}
  583. */
  584. getPublicProtectedFields()
  585. /*: string[]*/
  586. {
  587. return this.getProtectedFields(PUBLIC_KEY);
  588. }
  589. /**
  590. * Gets whether users belonging to the given role are allowed
  591. * to read from this class. Even if this returns false, the role may
  592. * still be able to write it if a parent role has read access.
  593. *
  594. * @param role The name of the role, or a Parse.Role object.
  595. * @returns {boolean} true if the role has read access. false otherwise.
  596. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  597. */
  598. getRoleReadAccess(role
  599. /*: ParseRole | string*/
  600. )
  601. /*: boolean*/
  602. {
  603. return this.getReadAccess(this._getRoleName(role));
  604. }
  605. /**
  606. * Gets whether users belonging to the given role are allowed
  607. * to write to this user. Even if this returns false, the role may
  608. * still be able to write it if a parent role has write access.
  609. *
  610. * @param role The name of the role, or a Parse.Role object.
  611. * @returns {boolean} true if the role has write access. false otherwise.
  612. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  613. */
  614. getRoleWriteAccess(role
  615. /*: ParseRole | string*/
  616. )
  617. /*: boolean*/
  618. {
  619. return this.getWriteAccess(this._getRoleName(role));
  620. }
  621. /**
  622. * Sets whether users belonging to the given role are allowed
  623. * to read from this class.
  624. *
  625. * @param role The name of the role, or a Parse.Role object.
  626. * @param {boolean} allowed Whether the given role can read this object.
  627. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  628. */
  629. setRoleReadAccess(role
  630. /*: ParseRole | string*/
  631. , allowed
  632. /*: boolean*/
  633. ) {
  634. this.setReadAccess(this._getRoleName(role), allowed);
  635. }
  636. /**
  637. * Sets whether users belonging to the given role are allowed
  638. * to write to this class.
  639. *
  640. * @param role The name of the role, or a Parse.Role object.
  641. * @param {boolean} allowed Whether the given role can write this object.
  642. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  643. */
  644. setRoleWriteAccess(role
  645. /*: ParseRole | string*/
  646. , allowed
  647. /*: boolean*/
  648. ) {
  649. this.setWriteAccess(this._getRoleName(role), allowed);
  650. }
  651. /**
  652. * Gets whether users belonging to the given role are allowed
  653. * to count to this user. Even if this returns false, the role may
  654. * still be able to count it if a parent role has count access.
  655. *
  656. * @param role The name of the role, or a Parse.Role object.
  657. * @returns {string[]}
  658. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  659. */
  660. getRoleProtectedFields(role
  661. /*: ParseRole | string*/
  662. )
  663. /*: string[]*/
  664. {
  665. return this.getProtectedFields(this._getRoleName(role));
  666. }
  667. /**
  668. * Sets whether users belonging to the given role are allowed
  669. * to set access field in this class.
  670. *
  671. * @param role The name of the role, or a Parse.Role object.
  672. * @param {string[]} fields Fields to be protected by Role.
  673. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  674. */
  675. setRoleProtectedFields(role
  676. /*: ParseRole | string*/
  677. , fields
  678. /*: string[]*/
  679. ) {
  680. this.setProtectedFields(this._getRoleName(role), fields);
  681. }
  682. }
  683. var _default = ParseCLP;
  684. exports.default = _default;