ParseACL.js 9.9 KB

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