ParseACL.js 11 KB

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