ParseCLP.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  7. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  8. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  9. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  10. var _ParseRole = _interopRequireDefault(require("./ParseRole"));
  11. var _ParseUser = _interopRequireDefault(require("./ParseUser"));
  12. function _createForOfIteratorHelperLoose(o, allowArrayLike) {
  13. var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
  14. if (it) return (it = it.call(o)).next.bind(it);
  15. if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
  16. if (it) o = it;
  17. var i = 0;
  18. return function () {
  19. if (i >= o.length) return {
  20. done: true
  21. };
  22. return {
  23. done: false,
  24. value: o[i++]
  25. };
  26. };
  27. }
  28. throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  29. }
  30. function _unsupportedIterableToArray(o, minLen) {
  31. if (!o) return;
  32. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  33. var n = Object.prototype.toString.call(o).slice(8, -1);
  34. if (n === "Object" && o.constructor) n = o.constructor.name;
  35. if (n === "Map" || n === "Set") return Array.from(o);
  36. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  37. }
  38. function _arrayLikeToArray(arr, len) {
  39. if (len == null || len > arr.length) len = arr.length;
  40. for (var i = 0, arr2 = new Array(len); i < len; i++) {
  41. arr2[i] = arr[i];
  42. }
  43. return arr2;
  44. }
  45. var PUBLIC_KEY = '*';
  46. var VALID_PERMISSIONS = new Map();
  47. VALID_PERMISSIONS.set('get', {});
  48. VALID_PERMISSIONS.set('find', {});
  49. VALID_PERMISSIONS.set('count', {});
  50. VALID_PERMISSIONS.set('create', {});
  51. VALID_PERMISSIONS.set('update', {});
  52. VALID_PERMISSIONS.set('delete', {});
  53. VALID_PERMISSIONS.set('addField', {});
  54. var VALID_PERMISSIONS_EXTENDED = new Map();
  55. VALID_PERMISSIONS_EXTENDED.set('protectedFields', {});
  56. var ParseCLP = function () {
  57. function ParseCLP(userId) {
  58. var _this = this;
  59. (0, _classCallCheck2.default)(this, ParseCLP);
  60. this.permissionsMap = {};
  61. var _loop = function (operation, group) {
  62. _this.permissionsMap[operation] = (0, _extends2.default)({}, group);
  63. var action = operation.charAt(0).toUpperCase() + operation.slice(1);
  64. _this["get" + action + "RequiresAuthentication"] = function () {
  65. return this._getAccess(operation, 'requiresAuthentication');
  66. };
  67. _this["set" + action + "RequiresAuthentication"] = function (allowed) {
  68. this._setAccess(operation, 'requiresAuthentication', allowed);
  69. };
  70. _this["get" + action + "PointerFields"] = function () {
  71. return this._getAccess(operation, 'pointerFields', false);
  72. };
  73. _this["set" + action + "PointerFields"] = function (pointerFields) {
  74. this._setArrayAccess(operation, 'pointerFields', pointerFields);
  75. };
  76. _this["get" + action + "Access"] = function (entity) {
  77. return this._getAccess(operation, entity);
  78. };
  79. _this["set" + action + "Access"] = function (entity, allowed) {
  80. this._setAccess(operation, entity, allowed);
  81. };
  82. _this["getPublic" + action + "Access"] = function () {
  83. return this["get" + action + "Access"](PUBLIC_KEY);
  84. };
  85. _this["setPublic" + action + "Access"] = function (allowed) {
  86. this["set" + action + "Access"](PUBLIC_KEY, allowed);
  87. };
  88. _this["getRole" + action + "Access"] = function (role) {
  89. return this["get" + action + "Access"](this._getRoleName(role));
  90. };
  91. _this["setRole" + action + "Access"] = function (role, allowed) {
  92. this["set" + action + "Access"](this._getRoleName(role), allowed);
  93. };
  94. };
  95. for (var _iterator = _createForOfIteratorHelperLoose(VALID_PERMISSIONS.entries()), _step; !(_step = _iterator()).done;) {
  96. var _ref = _step.value;
  97. var _ref2 = (0, _slicedToArray2.default)(_ref, 2);
  98. var operation = _ref2[0];
  99. var group = _ref2[1];
  100. _loop(operation, group);
  101. }
  102. for (var _iterator2 = _createForOfIteratorHelperLoose(VALID_PERMISSIONS_EXTENDED.entries()), _step2; !(_step2 = _iterator2()).done;) {
  103. var _ref3 = _step2.value;
  104. var _ref4 = (0, _slicedToArray2.default)(_ref3, 2);
  105. var operation = _ref4[0];
  106. var group = _ref4[1];
  107. this.permissionsMap[operation] = (0, _extends2.default)({}, group);
  108. }
  109. if (userId && typeof userId === 'object') {
  110. if (userId instanceof _ParseUser.default) {
  111. this.setReadAccess(userId, true);
  112. this.setWriteAccess(userId, true);
  113. } else if (userId instanceof _ParseRole.default) {
  114. this.setRoleReadAccess(userId, true);
  115. this.setRoleWriteAccess(userId, true);
  116. } else {
  117. for (var _permission in userId) {
  118. var users = userId[_permission];
  119. var isValidPermission = !!VALID_PERMISSIONS.get(_permission);
  120. var isValidPermissionExtended = !!VALID_PERMISSIONS_EXTENDED.get(_permission);
  121. var isValidGroupPermission = ['readUserFields', 'writeUserFields'].includes(_permission);
  122. if (typeof _permission !== 'string' || !(isValidPermission || isValidPermissionExtended || isValidGroupPermission)) {
  123. throw new TypeError('Tried to create an CLP with an invalid permission type.');
  124. }
  125. if (isValidGroupPermission) {
  126. if (users.every(function (pointer) {
  127. return typeof pointer === 'string';
  128. })) {
  129. this.permissionsMap[_permission] = users;
  130. continue;
  131. } else {
  132. throw new TypeError('Tried to create an CLP with an invalid permission value.');
  133. }
  134. }
  135. for (var user in users) {
  136. var allowed = users[user];
  137. if (typeof allowed !== 'boolean' && !isValidPermissionExtended && user !== 'pointerFields') {
  138. throw new TypeError('Tried to create an CLP with an invalid permission value.');
  139. }
  140. this.permissionsMap[_permission][user] = allowed;
  141. }
  142. }
  143. }
  144. } else if (typeof userId === 'function') {
  145. throw new TypeError('ParseCLP constructed with a function. Did you forget ()?');
  146. }
  147. }
  148. (0, _createClass2.default)(ParseCLP, [{
  149. key: "toJSON",
  150. value: function () {
  151. return (0, _extends2.default)({}, this.permissionsMap);
  152. }
  153. }, {
  154. key: "equals",
  155. value: function (other) {
  156. if (!(other instanceof ParseCLP)) {
  157. return false;
  158. }
  159. var permissions = Object.keys(this.permissionsMap);
  160. var otherPermissions = Object.keys(other.permissionsMap);
  161. if (permissions.length !== otherPermissions.length) {
  162. return false;
  163. }
  164. for (var _permission2 in this.permissionsMap) {
  165. if (!other.permissionsMap[_permission2]) {
  166. return false;
  167. }
  168. var users = Object.keys(this.permissionsMap[_permission2]);
  169. var otherUsers = Object.keys(other.permissionsMap[_permission2]);
  170. if (users.length !== otherUsers.length) {
  171. return false;
  172. }
  173. for (var user in this.permissionsMap[_permission2]) {
  174. if (!other.permissionsMap[_permission2][user]) {
  175. return false;
  176. }
  177. if (this.permissionsMap[_permission2][user] !== other.permissionsMap[_permission2][user]) {
  178. return false;
  179. }
  180. }
  181. }
  182. return true;
  183. }
  184. }, {
  185. key: "_getRoleName",
  186. value: function (role) {
  187. var name = role;
  188. if (role instanceof _ParseRole.default) {
  189. name = role.getName();
  190. }
  191. if (typeof name !== 'string') {
  192. throw new TypeError('role must be a Parse.Role or a String');
  193. }
  194. return "role:" + name;
  195. }
  196. }, {
  197. key: "_parseEntity",
  198. value: function (entity) {
  199. var userId = entity;
  200. if (userId instanceof _ParseUser.default) {
  201. userId = userId.id;
  202. if (!userId) {
  203. throw new Error('Cannot get access for a Parse.User without an id.');
  204. }
  205. } else if (userId instanceof _ParseRole.default) {
  206. userId = this._getRoleName(userId);
  207. }
  208. if (typeof userId !== 'string') {
  209. throw new TypeError('userId must be a string.');
  210. }
  211. return userId;
  212. }
  213. }, {
  214. key: "_setAccess",
  215. value: function (permission, userId, allowed) {
  216. userId = this._parseEntity(userId);
  217. if (typeof allowed !== 'boolean') {
  218. throw new TypeError('allowed must be either true or false.');
  219. }
  220. var permissions = this.permissionsMap[permission][userId];
  221. if (!permissions) {
  222. if (!allowed) {
  223. return;
  224. }
  225. this.permissionsMap[permission][userId] = {};
  226. }
  227. if (allowed) {
  228. this.permissionsMap[permission][userId] = true;
  229. } else {
  230. delete this.permissionsMap[permission][userId];
  231. }
  232. }
  233. }, {
  234. key: "_getAccess",
  235. value: function (permission, userId) {
  236. var returnBoolean = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  237. userId = this._parseEntity(userId);
  238. var permissions = this.permissionsMap[permission][userId];
  239. if (returnBoolean) {
  240. if (!permissions) {
  241. return false;
  242. }
  243. return !!this.permissionsMap[permission][userId];
  244. }
  245. return permissions;
  246. }
  247. }, {
  248. key: "_setArrayAccess",
  249. value: function (permission, userId, fields) {
  250. userId = this._parseEntity(userId);
  251. var permissions = this.permissionsMap[permission][userId];
  252. if (!permissions) {
  253. this.permissionsMap[permission][userId] = [];
  254. }
  255. if (!fields || Array.isArray(fields) && fields.length === 0) {
  256. delete this.permissionsMap[permission][userId];
  257. } else if (Array.isArray(fields) && fields.every(function (field) {
  258. return typeof field === 'string';
  259. })) {
  260. this.permissionsMap[permission][userId] = fields;
  261. } else {
  262. throw new TypeError('fields must be an array of strings or undefined.');
  263. }
  264. }
  265. }, {
  266. key: "_setGroupPointerPermission",
  267. value: function (operation, pointerFields) {
  268. var fields = this.permissionsMap[operation];
  269. if (!fields) {
  270. this.permissionsMap[operation] = [];
  271. }
  272. if (!pointerFields || Array.isArray(pointerFields) && pointerFields.length === 0) {
  273. delete this.permissionsMap[operation];
  274. } else if (Array.isArray(pointerFields) && pointerFields.every(function (field) {
  275. return typeof field === 'string';
  276. })) {
  277. this.permissionsMap[operation] = pointerFields;
  278. } else {
  279. throw new TypeError(operation + ".pointerFields must be an array of strings or undefined.");
  280. }
  281. }
  282. }, {
  283. key: "_getGroupPointerPermissions",
  284. value: function (operation) {
  285. return this.permissionsMap[operation];
  286. }
  287. }, {
  288. key: "setReadUserFields",
  289. value: function (pointerFields) {
  290. this._setGroupPointerPermission('readUserFields', pointerFields);
  291. }
  292. }, {
  293. key: "getReadUserFields",
  294. value: function () {
  295. return this._getGroupPointerPermissions('readUserFields');
  296. }
  297. }, {
  298. key: "setWriteUserFields",
  299. value: function (pointerFields) {
  300. this._setGroupPointerPermission('writeUserFields', pointerFields);
  301. }
  302. }, {
  303. key: "getWriteUserFields",
  304. value: function () {
  305. return this._getGroupPointerPermissions('writeUserFields');
  306. }
  307. }, {
  308. key: "setProtectedFields",
  309. value: function (userId, fields) {
  310. this._setArrayAccess('protectedFields', userId, fields);
  311. }
  312. }, {
  313. key: "getProtectedFields",
  314. value: function (userId) {
  315. return this._getAccess('protectedFields', userId, false);
  316. }
  317. }, {
  318. key: "setReadAccess",
  319. value: function (userId, allowed) {
  320. this._setAccess('find', userId, allowed);
  321. this._setAccess('get', userId, allowed);
  322. this._setAccess('count', userId, allowed);
  323. }
  324. }, {
  325. key: "getReadAccess",
  326. value: function (userId) {
  327. return this._getAccess('find', userId) && this._getAccess('get', userId) && this._getAccess('count', userId);
  328. }
  329. }, {
  330. key: "setWriteAccess",
  331. value: function (userId, allowed) {
  332. this._setAccess('create', userId, allowed);
  333. this._setAccess('update', userId, allowed);
  334. this._setAccess('delete', userId, allowed);
  335. this._setAccess('addField', userId, allowed);
  336. }
  337. }, {
  338. key: "getWriteAccess",
  339. value: function (userId) {
  340. return this._getAccess('create', userId) && this._getAccess('update', userId) && this._getAccess('delete', userId) && this._getAccess('addField', userId);
  341. }
  342. }, {
  343. key: "setPublicReadAccess",
  344. value: function (allowed) {
  345. this.setReadAccess(PUBLIC_KEY, allowed);
  346. }
  347. }, {
  348. key: "getPublicReadAccess",
  349. value: function () {
  350. return this.getReadAccess(PUBLIC_KEY);
  351. }
  352. }, {
  353. key: "setPublicWriteAccess",
  354. value: function (allowed) {
  355. this.setWriteAccess(PUBLIC_KEY, allowed);
  356. }
  357. }, {
  358. key: "getPublicWriteAccess",
  359. value: function () {
  360. return this.getWriteAccess(PUBLIC_KEY);
  361. }
  362. }, {
  363. key: "setPublicProtectedFields",
  364. value: function (fields) {
  365. this.setProtectedFields(PUBLIC_KEY, fields);
  366. }
  367. }, {
  368. key: "getPublicProtectedFields",
  369. value: function () {
  370. return this.getProtectedFields(PUBLIC_KEY);
  371. }
  372. }, {
  373. key: "getRoleReadAccess",
  374. value: function (role) {
  375. return this.getReadAccess(this._getRoleName(role));
  376. }
  377. }, {
  378. key: "getRoleWriteAccess",
  379. value: function (role) {
  380. return this.getWriteAccess(this._getRoleName(role));
  381. }
  382. }, {
  383. key: "setRoleReadAccess",
  384. value: function (role, allowed) {
  385. this.setReadAccess(this._getRoleName(role), allowed);
  386. }
  387. }, {
  388. key: "setRoleWriteAccess",
  389. value: function (role, allowed) {
  390. this.setWriteAccess(this._getRoleName(role), allowed);
  391. }
  392. }, {
  393. key: "getRoleProtectedFields",
  394. value: function (role) {
  395. return this.getProtectedFields(this._getRoleName(role));
  396. }
  397. }, {
  398. key: "setRoleProtectedFields",
  399. value: function (role, fields) {
  400. this.setProtectedFields(this._getRoleName(role), fields);
  401. }
  402. }]);
  403. return ParseCLP;
  404. }();
  405. var _default = ParseCLP;
  406. exports.default = _default;