ParseACL.js 12 KB

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