ParseACL.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. * @alias Parse.ACL
  36. */
  37. class ParseACL {
  38. /*:: permissionsById: ByIdMap;*/
  39. /**
  40. * @param {(Parse.User|Object)} user The user to initialize the ACL for
  41. */
  42. constructor(arg1
  43. /*: ParseUser | ByIdMap*/
  44. ) {
  45. this.permissionsById = {};
  46. if (arg1 && typeof arg1 === 'object') {
  47. if (arg1 instanceof _ParseUser.default) {
  48. this.setReadAccess(arg1, true);
  49. this.setWriteAccess(arg1, true);
  50. } else {
  51. for (const userId in arg1) {
  52. const accessList = arg1[userId];
  53. if (typeof userId !== 'string') {
  54. throw new TypeError('Tried to create an ACL with an invalid user id.');
  55. }
  56. this.permissionsById[userId] = {};
  57. for (const permission in accessList) {
  58. const allowed = accessList[permission];
  59. if (permission !== 'read' && permission !== 'write') {
  60. throw new TypeError('Tried to create an ACL with an invalid permission type.');
  61. }
  62. if (typeof allowed !== 'boolean') {
  63. throw new TypeError('Tried to create an ACL with an invalid permission value.');
  64. }
  65. this.permissionsById[userId][permission] = allowed;
  66. }
  67. }
  68. }
  69. } else if (typeof arg1 === 'function') {
  70. throw new TypeError('ParseACL constructed with a function. Did you forget ()?');
  71. }
  72. }
  73. /**
  74. * Returns a JSON-encoded version of the ACL.
  75. * @return {Object}
  76. */
  77. toJSON()
  78. /*: ByIdMap*/
  79. {
  80. const permissions = {};
  81. for (const p in this.permissionsById) {
  82. permissions[p] = this.permissionsById[p];
  83. }
  84. return permissions;
  85. }
  86. /**
  87. * Returns whether this ACL is equal to another object
  88. * @param other The other object to compare to
  89. * @return {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. * @param userId An instance of Parse.User or its objectId.
  186. * @param {Boolean} allowed Whether that user should have read access.
  187. */
  188. setReadAccess(userId
  189. /*: ParseUser | ParseRole | string*/
  190. , allowed
  191. /*: boolean*/
  192. ) {
  193. this._setAccess('read', userId, allowed);
  194. }
  195. /**
  196. * Get whether the given user id is *explicitly* allowed to read this object.
  197. * Even if this returns false, the user may still be able to access it if
  198. * getPublicReadAccess returns true or a role that the user belongs to has
  199. * write access.
  200. * @param userId An instance of Parse.User or its objectId, or a Parse.Role.
  201. * @return {Boolean}
  202. */
  203. getReadAccess(userId
  204. /*: ParseUser | ParseRole | string*/
  205. )
  206. /*: boolean*/
  207. {
  208. return this._getAccess('read', userId);
  209. }
  210. /**
  211. * Sets whether the given user id is allowed to write this object.
  212. * @param userId An instance of Parse.User or its objectId, or a Parse.Role..
  213. * @param {Boolean} allowed Whether that user should have write access.
  214. */
  215. setWriteAccess(userId
  216. /*: ParseUser | ParseRole | string*/
  217. , allowed
  218. /*: boolean*/
  219. ) {
  220. this._setAccess('write', userId, allowed);
  221. }
  222. /**
  223. * Gets whether the given user id is *explicitly* allowed to write this object.
  224. * Even if this returns false, the user may still be able to write it if
  225. * getPublicWriteAccess returns true or a role that the user belongs to has
  226. * write access.
  227. * @param userId An instance of Parse.User or its objectId, or a Parse.Role.
  228. * @return {Boolean}
  229. */
  230. getWriteAccess(userId
  231. /*: ParseUser | ParseRole | string*/
  232. )
  233. /*: boolean*/
  234. {
  235. return this._getAccess('write', userId);
  236. }
  237. /**
  238. * Sets whether the public is allowed to read this object.
  239. * @param {Boolean} allowed
  240. */
  241. setPublicReadAccess(allowed
  242. /*: boolean*/
  243. ) {
  244. this.setReadAccess(PUBLIC_KEY, allowed);
  245. }
  246. /**
  247. * Gets whether the public is allowed to read this object.
  248. * @return {Boolean}
  249. */
  250. getPublicReadAccess()
  251. /*: boolean*/
  252. {
  253. return this.getReadAccess(PUBLIC_KEY);
  254. }
  255. /**
  256. * Sets whether the public is allowed to write this object.
  257. * @param {Boolean} allowed
  258. */
  259. setPublicWriteAccess(allowed
  260. /*: boolean*/
  261. ) {
  262. this.setWriteAccess(PUBLIC_KEY, allowed);
  263. }
  264. /**
  265. * Gets whether the public is allowed to write this object.
  266. * @return {Boolean}
  267. */
  268. getPublicWriteAccess()
  269. /*: boolean*/
  270. {
  271. return this.getWriteAccess(PUBLIC_KEY);
  272. }
  273. /**
  274. * Gets whether users belonging to the given role are allowed
  275. * to read this object. Even if this returns false, the role may
  276. * still be able to write it if a parent role has read access.
  277. *
  278. * @param role The name of the role, or a Parse.Role object.
  279. * @return {Boolean} true if the role has read access. false otherwise.
  280. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  281. */
  282. getRoleReadAccess(role
  283. /*: ParseRole | string*/
  284. )
  285. /*: boolean*/
  286. {
  287. if (role instanceof _ParseRole.default) {
  288. // Normalize to the String name
  289. role = role.getName();
  290. }
  291. if (typeof role !== 'string') {
  292. throw new TypeError('role must be a ParseRole or a String');
  293. }
  294. return this.getReadAccess('role:' + role);
  295. }
  296. /**
  297. * Gets whether users belonging to the given role are allowed
  298. * to write this object. Even if this returns false, the role may
  299. * still be able to write it if a parent role has write access.
  300. *
  301. * @param role The name of the role, or a Parse.Role object.
  302. * @return {Boolean} true if the role has write access. false otherwise.
  303. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  304. */
  305. getRoleWriteAccess(role
  306. /*: ParseRole | string*/
  307. )
  308. /*: boolean*/
  309. {
  310. if (role instanceof _ParseRole.default) {
  311. // Normalize to the String name
  312. role = role.getName();
  313. }
  314. if (typeof role !== 'string') {
  315. throw new TypeError('role must be a ParseRole or a String');
  316. }
  317. return this.getWriteAccess('role:' + role);
  318. }
  319. /**
  320. * Sets whether users belonging to the given role are allowed
  321. * to read this object.
  322. *
  323. * @param role The name of the role, or a Parse.Role object.
  324. * @param {Boolean} allowed Whether the given role can read this object.
  325. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  326. */
  327. setRoleReadAccess(role
  328. /*: ParseRole | string*/
  329. , allowed
  330. /*: boolean*/
  331. ) {
  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.setReadAccess('role:' + role, allowed);
  340. }
  341. /**
  342. * Sets whether users belonging to the given role are allowed
  343. * to write this object.
  344. *
  345. * @param role The name of the role, or a Parse.Role object.
  346. * @param {Boolean} allowed Whether the given role can write this object.
  347. * @throws {TypeError} If role is neither a Parse.Role nor a String.
  348. */
  349. setRoleWriteAccess(role
  350. /*: ParseRole | string*/
  351. , allowed
  352. /*: boolean*/
  353. ) {
  354. if (role instanceof _ParseRole.default) {
  355. // Normalize to the String name
  356. role = role.getName();
  357. }
  358. if (typeof role !== 'string') {
  359. throw new TypeError('role must be a ParseRole or a String');
  360. }
  361. this.setWriteAccess('role:' + role, allowed);
  362. }
  363. }
  364. var _default = ParseACL;
  365. exports.default = _default;