ParseACL.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. "use strict";
  2. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  3. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  4. _Object$defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.default = void 0;
  8. var _keys = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/keys"));
  9. var _typeof2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/typeof"));
  10. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
  11. var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
  12. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
  13. var _ParseRole = _interopRequireDefault(require("./ParseRole"));
  14. var _ParseUser = _interopRequireDefault(require("./ParseUser"));
  15. /**
  16. * Copyright (c) 2015-present, Parse, LLC.
  17. * All rights reserved.
  18. *
  19. * This source code is licensed under the BSD-style license found in the
  20. * LICENSE file in the root directory of this source tree. An additional grant
  21. * of patent rights can be found in the PATENTS file in the same directory.
  22. *
  23. * @flow
  24. */
  25. var PUBLIC_KEY = '*';
  26. /**
  27. * Creates a new ACL.
  28. * If no argument is given, the ACL has no permissions for anyone.
  29. * If the argument is a Parse.User, the ACL will have read and write
  30. * permission for only that user.
  31. * If the argument is any other JSON object, that object will be interpretted
  32. * as a serialized ACL created with toJSON().
  33. *
  34. * <p>An ACL, or Access Control List can be added to any
  35. * <code>Parse.Object</code> to restrict access to only a subset of users
  36. * of your application.</p>
  37. *
  38. * @alias Parse.ACL
  39. */
  40. var ParseACL = /*#__PURE__*/function () {
  41. /**
  42. * @param {(Parse.User | object)} arg1 The user to initialize the ACL for
  43. */
  44. function ParseACL(arg1
  45. /*: ParseUser | ByIdMap*/
  46. ) {
  47. (0, _classCallCheck2.default)(this, ParseACL);
  48. (0, _defineProperty2.default)(this, "permissionsById", void 0);
  49. this.permissionsById = {};
  50. if (arg1 && (0, _typeof2.default)(arg1) === 'object') {
  51. if (arg1 instanceof _ParseUser.default) {
  52. this.setReadAccess(arg1, true);
  53. this.setWriteAccess(arg1, true);
  54. } else {
  55. for (var _userId in arg1) {
  56. var accessList = arg1[_userId];
  57. this.permissionsById[_userId] = {};
  58. for (var _permission in accessList) {
  59. var allowed = accessList[_permission];
  60. if (_permission !== 'read' && _permission !== 'write') {
  61. throw new TypeError('Tried to create an ACL with an invalid permission type.');
  62. }
  63. if (typeof allowed !== 'boolean') {
  64. throw new TypeError('Tried to create an ACL with an invalid permission value.');
  65. }
  66. this.permissionsById[_userId][_permission] = allowed;
  67. }
  68. }
  69. }
  70. } else if (typeof arg1 === 'function') {
  71. throw new TypeError('ParseACL constructed with a function. Did you forget ()?');
  72. }
  73. }
  74. /**
  75. * Returns a JSON-encoded version of the ACL.
  76. *
  77. * @returns {object}
  78. */
  79. (0, _createClass2.default)(ParseACL, [{
  80. key: "toJSON",
  81. value: function ()
  82. /*: ByIdMap*/
  83. {
  84. var permissions = {};
  85. for (var p in this.permissionsById) {
  86. permissions[p] = this.permissionsById[p];
  87. }
  88. return permissions;
  89. }
  90. /**
  91. * Returns whether this ACL is equal to another object
  92. *
  93. * @param {ParseACL} other The other object's ACL to compare to
  94. * @returns {boolean}
  95. */
  96. }, {
  97. key: "equals",
  98. value: function (other
  99. /*: ParseACL*/
  100. )
  101. /*: boolean*/
  102. {
  103. if (!(other instanceof ParseACL)) {
  104. return false;
  105. }
  106. var users = (0, _keys.default)(this.permissionsById);
  107. var otherUsers = (0, _keys.default)(other.permissionsById);
  108. if (users.length !== otherUsers.length) {
  109. return false;
  110. }
  111. for (var u in this.permissionsById) {
  112. if (!other.permissionsById[u]) {
  113. return false;
  114. }
  115. if (this.permissionsById[u].read !== other.permissionsById[u].read) {
  116. return false;
  117. }
  118. if (this.permissionsById[u].write !== other.permissionsById[u].write) {
  119. return false;
  120. }
  121. }
  122. return true;
  123. }
  124. }, {
  125. key: "_setAccess",
  126. value: function (accessType
  127. /*: string*/
  128. , userId
  129. /*: ParseUser | ParseRole | string*/
  130. , allowed
  131. /*: boolean*/
  132. ) {
  133. if (userId instanceof _ParseUser.default) {
  134. userId = userId.id;
  135. } else if (userId instanceof _ParseRole.default) {
  136. var name = userId.getName();
  137. if (!name) {
  138. throw new TypeError('Role must have a name');
  139. }
  140. userId = "role:".concat(name);
  141. }
  142. if (typeof userId !== 'string') {
  143. throw new TypeError('userId must be a string.');
  144. }
  145. if (typeof allowed !== 'boolean') {
  146. throw new TypeError('allowed must be either true or false.');
  147. }
  148. var permissions = this.permissionsById[userId];
  149. if (!permissions) {
  150. if (!allowed) {
  151. // The user already doesn't have this permission, so no action is needed
  152. return;
  153. }
  154. permissions = {};
  155. this.permissionsById[userId] = permissions;
  156. }
  157. if (allowed) {
  158. this.permissionsById[userId][accessType] = true;
  159. } else {
  160. delete permissions[accessType];
  161. if ((0, _keys.default)(permissions).length === 0) {
  162. delete this.permissionsById[userId];
  163. }
  164. }
  165. }
  166. }, {
  167. key: "_getAccess",
  168. value: function (accessType
  169. /*: string*/
  170. , userId
  171. /*: ParseUser | ParseRole | string*/
  172. )
  173. /*: boolean*/
  174. {
  175. if (userId instanceof _ParseUser.default) {
  176. userId = userId.id;
  177. if (!userId) {
  178. throw new Error('Cannot get access for a ParseUser without an ID');
  179. }
  180. } else if (userId instanceof _ParseRole.default) {
  181. var name = userId.getName();
  182. if (!name) {
  183. throw new TypeError('Role must have a name');
  184. }
  185. userId = "role:".concat(name);
  186. }
  187. var permissions = this.permissionsById[userId];
  188. if (!permissions) {
  189. return false;
  190. }
  191. return !!permissions[accessType];
  192. }
  193. /**
  194. * Sets whether the given user is allowed to read this object.
  195. *
  196. * @param userId An instance of Parse.User or its objectId.
  197. * @param {boolean} allowed Whether that user should have read access.
  198. */
  199. }, {
  200. key: "setReadAccess",
  201. value: function (userId
  202. /*: ParseUser | ParseRole | string*/
  203. , allowed
  204. /*: boolean*/
  205. ) {
  206. this._setAccess('read', userId, allowed);
  207. }
  208. /**
  209. * Get whether the given user id is *explicitly* allowed to read this object.
  210. * Even if this returns false, the user may still be able to access it if
  211. * getPublicReadAccess returns true or a role that the user belongs to has
  212. * write access.
  213. *
  214. * @param userId An instance of Parse.User or its objectId, or a Parse.Role.
  215. * @returns {boolean}
  216. */
  217. }, {
  218. key: "getReadAccess",
  219. value: function (userId
  220. /*: ParseUser | ParseRole | string*/
  221. )
  222. /*: boolean*/
  223. {
  224. return this._getAccess('read', userId);
  225. }
  226. /**
  227. * Sets whether the given user id is allowed to write this object.
  228. *
  229. * @param userId An instance of Parse.User or its objectId, or a Parse.Role..
  230. * @param {boolean} allowed Whether that user should have write access.
  231. */
  232. }, {
  233. key: "setWriteAccess",
  234. value: function (userId
  235. /*: ParseUser | ParseRole | string*/
  236. , allowed
  237. /*: boolean*/
  238. ) {
  239. this._setAccess('write', userId, allowed);
  240. }
  241. /**
  242. * Gets whether the given user id is *explicitly* allowed to write this object.
  243. * Even if this returns false, the user may still be able to write it if
  244. * getPublicWriteAccess returns true or a role that the user belongs to has
  245. * write access.
  246. *
  247. * @param userId An instance of Parse.User or its objectId, or a Parse.Role.
  248. * @returns {boolean}
  249. */
  250. }, {
  251. key: "getWriteAccess",
  252. value: function (userId
  253. /*: ParseUser | ParseRole | string*/
  254. )
  255. /*: boolean*/
  256. {
  257. return this._getAccess('write', userId);
  258. }
  259. /**
  260. * Sets whether the public is allowed to read this object.
  261. *
  262. * @param {boolean} allowed
  263. */
  264. }, {
  265. key: "setPublicReadAccess",
  266. value: function (allowed
  267. /*: boolean*/
  268. ) {
  269. this.setReadAccess(PUBLIC_KEY, allowed);
  270. }
  271. /**
  272. * Gets whether the public is allowed to read this object.
  273. *
  274. * @returns {boolean}
  275. */
  276. }, {
  277. key: "getPublicReadAccess",
  278. value: function ()
  279. /*: boolean*/
  280. {
  281. return this.getReadAccess(PUBLIC_KEY);
  282. }
  283. /**
  284. * Sets whether the public is allowed to write this object.
  285. *
  286. * @param {boolean} allowed
  287. */
  288. }, {
  289. key: "setPublicWriteAccess",
  290. value: function (allowed
  291. /*: boolean*/
  292. ) {
  293. this.setWriteAccess(PUBLIC_KEY, allowed);
  294. }
  295. /**
  296. * Gets whether the public is allowed to write this object.
  297. *
  298. * @returns {boolean}
  299. */
  300. }, {
  301. key: "getPublicWriteAccess",
  302. value: function ()
  303. /*: boolean*/
  304. {
  305. return this.getWriteAccess(PUBLIC_KEY);
  306. }
  307. /**
  308. * Gets whether users belonging to the given role are allowed
  309. * to read this object. Even if this returns false, the role may
  310. * still be able to write it if a parent role has read access.
  311. *
  312. * @param role The name of the role, or a Parse.Role object.
  313. * @returns {boolean} true if the role has read access. false otherwise.
  314. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  315. */
  316. }, {
  317. key: "getRoleReadAccess",
  318. value: function (role
  319. /*: ParseRole | string*/
  320. )
  321. /*: boolean*/
  322. {
  323. if (role instanceof _ParseRole.default) {
  324. // Normalize to the String name
  325. role = role.getName();
  326. }
  327. if (typeof role !== 'string') {
  328. throw new TypeError('role must be a ParseRole or a String');
  329. }
  330. return this.getReadAccess("role:".concat(role));
  331. }
  332. /**
  333. * Gets whether users belonging to the given role are allowed
  334. * to write this object. Even if this returns false, the role may
  335. * still be able to write it if a parent role has write access.
  336. *
  337. * @param role The name of the role, or a Parse.Role object.
  338. * @returns {boolean} true if the role has write access. false otherwise.
  339. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  340. */
  341. }, {
  342. key: "getRoleWriteAccess",
  343. value: function (role
  344. /*: ParseRole | string*/
  345. )
  346. /*: boolean*/
  347. {
  348. if (role instanceof _ParseRole.default) {
  349. // Normalize to the String name
  350. role = role.getName();
  351. }
  352. if (typeof role !== 'string') {
  353. throw new TypeError('role must be a ParseRole or a String');
  354. }
  355. return this.getWriteAccess("role:".concat(role));
  356. }
  357. /**
  358. * Sets whether users belonging to the given role are allowed
  359. * to read this object.
  360. *
  361. * @param role The name of the role, or a Parse.Role object.
  362. * @param {boolean} allowed Whether the given role can read this object.
  363. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  364. */
  365. }, {
  366. key: "setRoleReadAccess",
  367. value: function (role
  368. /*: ParseRole | string*/
  369. , allowed
  370. /*: boolean*/
  371. ) {
  372. if (role instanceof _ParseRole.default) {
  373. // Normalize to the String name
  374. role = role.getName();
  375. }
  376. if (typeof role !== 'string') {
  377. throw new TypeError('role must be a ParseRole or a String');
  378. }
  379. this.setReadAccess("role:".concat(role), allowed);
  380. }
  381. /**
  382. * Sets whether users belonging to the given role are allowed
  383. * to write this object.
  384. *
  385. * @param role The name of the role, or a Parse.Role object.
  386. * @param {boolean} allowed Whether the given role can write this object.
  387. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  388. */
  389. }, {
  390. key: "setRoleWriteAccess",
  391. value: function (role
  392. /*: ParseRole | string*/
  393. , allowed
  394. /*: boolean*/
  395. ) {
  396. if (role instanceof _ParseRole.default) {
  397. // Normalize to the String name
  398. role = role.getName();
  399. }
  400. if (typeof role !== 'string') {
  401. throw new TypeError('role must be a ParseRole or a String');
  402. }
  403. this.setWriteAccess("role:".concat(role), allowed);
  404. }
  405. }]);
  406. return ParseACL;
  407. }();
  408. var _default = ParseACL;
  409. exports.default = _default;