ParseACL.js 12 KB

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