ParseACL.js 10 KB

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