ParseACL.js 11 KB

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